【python】windows客户端的ui自动化框架搭建及使用(winappdriver)

0.环境准备

  1. 安装jdk(8以上版本)及其环境配置
  2. 安装python3.10.4及其对应的三方包
  3. 安装winappdriver,下载地址:https://github.com/microsoft/WinAppDriver/releases/tag/v1.2.1
  4. windows开启开发者模式,本地开启服务:https://jingyan.baidu.com/article/91f5db1bfc57df5d7e05e31d.html

1.思路

整体思路:
1、封装appdriver服务、客户端启动关闭
2、封装鼠标基础行为 bat
3、封装元素定位基础行为 baseFunc
4、conftest中调用appdriver服务、客户端启动关闭,将driver暴露
5、调用

2.初试用

# -*- encoding: utf-8 -*-"
"""
@File: driver_win.py
@Author: cyl
@Desc: 对winappdriver服务、app的开启关闭进行封装
"""
import os
import time
import psutil
import platform
from selenium import webdriver
from settings import op_dir

WinAppDriver: str = "WinAppDriver.exe"
MyAppEXE: str = "xxx.exe"


# noinspection PyMethodMayBeStatic
class WinModelDriver:
    def __init__(self, host='localhost', port=4723) -> None:
        self.open_app_driver()
        # 配置信息
        self.desired_caps: dict = {
   
   
            'platform_fame': platform.system(),   # 平台名: 'Linux', 'Windows' or 'Java'
            'deviceName': 'WindowsPC',            # 系统
            'app': op_dir,                        # 应用程序绝对路径
            'pageLoadStrategy': 'none',           # 加载策略
            'disable-popup-blocking': True,       # 禁用弹窗
        }
        self.host = host
        self.port = port
        self.driver = None

    def open_app_driver(self) -> None:
   		"""启用winappdriver服务"""
        result = os.system(r'start "" /d "C:\Program Files (x86)\Windows Application Driver\"  "WinAppDriver.exe"')
        if result == 1:
            raise "开启服务失败, 确保启动WinAppDriver服务的地址正确!"

    # noinspection HttpUrlsUsage
    def open_platform(self) -> webdriver:
        """打开"""
        try:
            self.driver = webdriver.Remote('http://{}:{}'.format(self.host, self.port), self.desired_caps)
        except Exception as e:
            raise AssertionError(e)
        # self.driver.implicitly_wait(3)
        # self.driver.maximize_window()
        # print("全部元素-> ", self.driver.page_source)
        return self.driver

    def close_platform(self) -> None:
        """关闭"""
        self.driver.close()
        self.driver.quit()

        # 关闭所有客户端进程
        time.sleep(1.5)
        for proc in psutil.process_iter()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值