appium+python开发windows平台应用自动化

环境配置:
1、安装appium
https://www.cnblogs.com/sjl179947253/p/11947246.html
2、安装python,建议3.6以上版本
3、pip install Appium-Python-Client
4、windows SDK最新版:https://developer.microsoft.com/zh-cn/windows/downloads/sdk-archive/
5、winAppDriver,类似于selenium测试chrome浏览器,需要有一个chromedriver一样,appium测试windows应用也要有一个driver
https://github.com/Microsoft/WinAppDriver/releases

说明:

1、C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\inspect.exe 查看元素信息(必须要安装了windows sdk之后才会有这个)
2、appium查看元素的方法,也就以下几种(比较少):
1)find_element_by_name() 针对按钮,可能使用这个方法,参数就是按钮的text属性
2)find_element_by_accessibility_id 对应的是automationid,很多元素没有这个属性
3)find_element_by_class_name 不好用
4)find_element_by_xpath() 有这一个就够了,理论上应该没有他找不到的

代码示例:

# -*- coding:utf-8 -*-
from appium import webdriver
from time import sleep


class Windows(object):
    def __init__(self, app, host='localhost', port=4723):
        self.desired_caps = {}
        self.desired_caps['platformName'] = 'Windows'
        self.desired_caps['app'] = app
        self.desired_caps['deviceName'] = 'WindowsPC'
        self.host = host
        self.port = port
        self.appVersion = None

        try:
            self.driver = webdriver.Remote('http://{}:{}/wd/hub'.format(self.host, self.port), self.desired_caps)
        except Exception as e:
            raise AssertionError(e)


if __name__ == '__main__':
    app = 'D:/Program Files/win/testDemo.exe'
    notepad = Windows(app)
    sleep(1)
    notepad.driver.find_element_by_name('立即登录').click()   # 按钮基本上可以使用此方法
    notepad.driver.find_element_by_accessibility_id('API_test_102300_003').click()            # 针对有AutomationId的元素
    eles = notepad.driver.find_elements_by_class_name('Chrome_RenderWidgetHostHWND')          # classname也可以找到部分元素,但是重复元素太多要注意
    notepad.driver.find_elements_by_xpath('//*[@LocalizedControlType="按钮"]')[1].click()      # xpath功能比较强一些

 

posted on 2021-01-18 17:07  愚哥  阅读(531)  评论(0)    收藏  举报

导航