首先进行元素定位这里使用的是:(SDK 软件开发库)uiautomatorviewer
手机模拟器和Appium
也可以通过appium服务器工具定位 Start inspector Session 定位元素
元素定位操作:
id(resource-id)class_name(class),
xpath(xpath表达式)例如: //*[@id='kw']//从html开始找 *任意元素 @通过属性来筛选 []表示筛选条件 ""查找的值
定位方法:driver.find_element_by_id() 通过uiautomatorviewer.bat获取id值
进行测试的时候
cmd使用adb devices命令确保已经连接成功
app_caps.yaml
platformName: Android platformVersion: 7.1.2 deviceName: 127.0.0.1:62001 appPackage: com.tencent.mobileqq appActivity: .activity.SplashActivity noRest: False ip: 127.0.0.1 port: 4723
desired_case.pyfrom time import sleep import yaml from appium import webdriver # 拿文件数据 r读取文件的内容 from selenium.webdriver.common.by import By # def appium_login(): stream = open('app_caps.yaml', 'r') # 数据拿处理 data = yaml.load(stream, Loader=yaml.FullLoader) info = {} info['platformName'] = data['platformName'] info['platformVersion'] = data['platformVersion'] info['deviceName'] = data['deviceName'] info['appPackage'] = data['appPackage'] info['appActivity'] = data['appActivity'] info['noRest'] = data['noRest'] driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', info) driver.find_element(By.ID, 'com.tencent.mobileqq:id/dialogRightBtn').click() sleep(15) driver.find_element(By.ID, 'com.tencent.mobileqq:id/btn_login').click() driver.implicitly_wait(2) driver.find_element(By.XPATH, "//*[@content-desc='请输入QQ号码或手机或邮箱']").clear() driver.find_element(By.XPATH, "//*[@content-desc='请输入QQ号码或手机或邮箱']").send_keys("测试用的QQ账号") driver.find_element(By.ID, 'com.tencent.mobileqq:id/password').clear() driver.find_element(By.ID, 'com.tencent.mobileqq:id/password').send_keys("测试用的QQ密码") driver.find_element(By.ID, 'com.tencent.mobileqq:id/login').click() driver.find_element(By.ID, 'com.tencent.mobileqq:id/dialogRightBtn').click() driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button').click() driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button').click() sleep(60) driver.quit() # return driver
运行结果
Appium自动化测试安装包下载链接