appium脚本定位
测试环境
- Win 10 64bit/Mac
- Python 3.6
- Appium 1.5.1
- Android 7.1.1 Max2
- 测试App:考研帮Android版
测试场景
自动安装考研帮App(kaoyan3.1.0.apk),然后启动App
测试步骤
- 获取待测试app的packageName和Activity
- 配置Capability
- 连接设备
- 编辑脚本并运行
- 查看结果
运行前检查事项
- 检查设备是否连接
- 检查Appium server是否启动
- 检查Capability配置信息是否正确
测试脚本
1 from appium import webdriver 2 desired_cap={ 3 'platformName':'Android', 4 'platformVerion':'7.1.1', 5 'deviceName':'192.168.10.8:5555', 6 'app':r'/Users/zhangxinli/Downloads/com.stub.StubApp.apk', 7 'appPackage':'com.tal.kaoyan', 8 'appActivity':'com.tal.kaoyan.ui.activity.SplashActivity' 9 10 } 11 driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_cap)
首次启动Appium会在设备上安装2个守护app,Appium Settings和Unlock 部分设备系
统由于权限的问题(如:三星S6 edge+)需要用户手动确认安装,否则不安装守护App
会导致脚本运行失败,安装好后不要随意卸载这两个App。
- Unlock :用于解锁手机弹窗提示
- Appium Setting:Appium守护app
- from appium import webdriver 中的webdriber模块和selenium中的webdriver模块不一样!
元素定位
与Web自动化测试一样,app自动化测试过程中最重要一个环节就是元素定位,只有准确定位到了元素
才能进行相关元素的操作,如输入、点击、拖拽、滑动等。appium提供了许多元素定位的方法,如id定位
name定位、class定位、层级定位等等.... 接下来将会给大家来实践运用这些定位技巧。
元素定位方式
- id
- name
- class
- List定位
- 相对定位
- Xpath定位
- H5页面元素定位
- Uiautomator定位
id定位
日常生活中身边可能存在相同名字的人,但是每个人的身份证号码是唯一的
,在app界面元素中也可以使用id值来区分不同的元素,然后进行定位操作。
Appium中可以使用 find_element_by_id() 方法来进行id定位。
测试场景1
- 安装考研帮kaoyan.apk
- 点击允许访问的权限
- 点击引导页面的跳过按钮
- 同意条约
- 输入账号和密码
from appium import webdriver
import time
desired_cap={
'platformName':'Android',
'platformVerion':'7.1.1',
'deviceName':'192.168.10.8:5555',
#'app':r'/Users/zhangxinli/Downloads/com.stub.StubApp.apk',
'appPackage':'com.tal.kaoyan',
'appActivity':'com.tal.kaoyan.ui.activity.SplashActivity'
}
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_cap)
driver.implicitly_wait(5)
driver.find_element_by_id('android:id/button1').click()
time.sleep(1)
driver.find_element_by_id('android:id/button1').click()
driver.find_element_by_id('com.tal.kaoyan:id/tv_skip').click()
driver.find_element_by_id('com.tal.kaoyan:id/tip_commit').click()
driver.find_element_by_id('login_email_edittext').send_keys('zxl')
driver.find_element_by_id('login_password_edittext').send_keys('123')
思考
- 如果安装的版本包,没有这个权限的消息如何处理
- 跳过引导页面首次启动和非首次启动场景该如何处理?
方案探索1——if条件判断
有同学可能想到用if来做条件判断,判断元素是否存在,存在则点击,不存在则跳过。
1 from appium import webdriver 2 import time 3 desired_cap={ 4 'platformName':'Android', 5 'platformVerion':'7.1.1', 6 'deviceName':'192.168.10.8:5555', 7 'appPackage':'com.tal.kaoyan', 8 'appActivity':'com.tal.kaoyan.ui.activity.SplashActivity', 9 "noReset": True 10 11 } 12 driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_cap) 13 driver.implicitly_wait(5) 14 15 16 power1=driver.find_element_by_id('android:id/button1') 17 time.sleep(1) 18 power2=driver.find_element_by_id('android:id/button1') 19 agree=driver.find_element_by_id('com.tal.kaoyan:id/tv_skip') 20 skip=driver.find_element_by_id('com.tal.kaoyan:id/tip_commit') 21 22 if power1: 23 power1.click() 24 else: 25 print('no power1') 26 27 if power2: 28 power2.click() 29 else: 30 print('no power2') 31 32 if agree: 33 agree.click() 34 else: 35 print('no agree') 36 37 if skip: 38 skip.click() 39 else: 40 print('no skip')
方案探索2——异常捕捉
既然上面的if语句判断无法生效,但是我们发现一个突破口,那就是捕捉NoSuchElementException异常。
1 from appium import webdriver 2 from selenium.common.exceptions import NoSuchElementException 3 desired_cap={ 4 'platformName':'Android', 5 'platformVerion':'7.1.1', 6 'deviceName':'192.168.10.8:5555', 7 'appPackage':'com.tal.kaoyan', 8 'appActivity':'com.tal.kaoyan.ui.activity.SplashActivity', 9 "noReset": True 10 11 } 12 driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_cap) 13 driver.implicitly_wait(5) 14 15 def check_power1(): 16 try: 17 power1 = driver.find_element_by_id('android:id/button1') 18 except NoSuchElementException: 19 print('no power1') 20 else: 21 power1.click() 22 def check_power2(): 23 try: 24 power2 = driver.find_element_by_id('android:id/button1') 25 except NoSuchElementException: 26 print('no power2') 27 else: 28 power2.click() 29 def check_agree(): 30 try: 31 agree = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip') 32 except NoSuchElementException: 33 print('no agree') 34 else: 35 agree.click() 36 def check_skip(): 37 try: 38 skip = driver.find_element_by_id('com.tal.kaoyan:id/tip_commit') 39 except NoSuchElementException: 40 print('no skip') 41 else: 42 skip.click() 43 check_power1() 44 check_power2() 45 check_agree() 46 check_skip()
id定位综合实践——自动登录
测试场景
- 启动App,进入到登录界面
- 在登录页面输入用户名,密码 然后点击登录。
- 可以把启动后检测权限弹窗和引导页面的模块抽离作为独立的模块被其他模块调用,提高代码复用率。
- 获取用户名密码输入框和登录按钮的元素id属性,另外要考虑启动时App之前是否登录过账号,已经登录过和未登录场景流程不一样。
- 注意:send_keys()传入中文时需要在capability中配置如下内容:
需求分析
desired_caps['unicodeKeyboard']=True
desired_caps['resetKeyboard']=True
设置之后会有Appium的输入法守护来执行输入操作
代码实现
1 from find_element.capability import driver,NoSuchElementException 2 3 4 def login(): 5 driver.find_element_by_id('login_email_edittext').clear() 6 driver.find_element_by_id('login_email_edittext').send_keys('zxl643193936') 7 8 driver.find_element_by_id('login_password_edittext').send_keys('zxl169219') 9 driver.find_element_by_id('login_login_btn').click() 10 11 try: 12 driver.find_element_by_id('mainactivity_button_mysefl') 13 except NoSuchElementException: 14 login() 15 else: 16 driver.find_element_by_id('mainactivity_button_mysefl').click() 17 driver.find_element_by_id('activity_usercenter_username').click() 18 login()
注意:
使用Appium做了输入操作之后,手机如果出现输入法无法唤起,可以在系统设置——语言和输入法——将当前输入法替换为系统输入法或者其他输入法。
name定位
根据name进行定位,对于android来说,就是text属性
用法
from find_element.capability import *
driver.find_element_by_name('请输入用户名').send_keys('zxl')
driver.find_element_by_name('登录').click()
说明:由于text稳定性不是很好,所以appium 1.5开始废弃了该方法。
classname定位
classname定位是根据元素类型来进行定位,但是实际情况中很多元素的classname都是相同的,
如上例中登录页面中的用户名和密码都是clasName属性值都是:“android.widget.EditText” 因此只
能定位第一个元素也就是用户名,而密码输入框就需要使用其他方式来定位,这样其实很鸡肋.一般情况
下如果有id就不必使用classname定位。
1 from find_element.capability import driver 2 3 driver.find_element_by_class_name('android.widget.EditText').send_keys('zxl') 4 5 driver.find_element_by_class_name('android.widget.EditText').send_keys('123243') 6 7 driver.find_element_by_class_name('android.widget.Button').click()
相对定位
相对定位是先找到该元素的有对应属性的父元素节点,然后基于父元素进行元素定位。
测试案例
不使用id元素定位方式,在新用户注册界面点击添加头像按钮。
代码实现
1 from find_element.capability import driver 2 3 driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click() 4 root_elem=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_scrollview') 5 root_elem.find_element_by_class_name('android.widget.ImageView')
xpath定位
xpath定位是一种路径定位方式,主要是依赖于元素绝对路径或者相关属性来定位,但是绝对路径xpath执行效率比较低(特别是元素路径比较深的时候),
一般使用比较少。通常使用xpath相对路径和属性定位。
1.xpath路径表达式
|
表达式 |
描述 |
|
/ |
从根节点选取。 |
|
// |
从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
|
nodename |
选取此节点的所有子节点。 |
|
. |
选取当前节点。 |
|
.. |
选取当前节点的父节点。 |
|
@ |
选取属性。 |
|
通配符 |
描述 |
|
* |
匹配任何元素节点。 |
|
@* |
匹配任何属性节点。 |
|
node() |
匹配任何类型的节点。 |
实践案例
使用xpath定位元素来进行登录操作。
1 from find_element.capability import driver 2 ''' 3 driver.find_element_by_xpath('//android.widget.EditText[@text="请输入用户名"]').send_keys('zxl643193936') 4 driver.find_element_by_xpath('//android.widget.EditText[@text="请输入密码"]').send_keys('zxl169219') 5 #driver.find_element_by_xpath('//android.widget.Button').click() 6 driver.find_element_by_xpath('//*[@class="android.widget.Button"]').click() 7 8 ''' 9 driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.EditText[1]').send_keys('zxl643193936') 10 driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.EditText[2]').send_keys('zxl169219') 11 driver.find_ele
扩展资料:xpath语法
List定位
前面我们提到相同的classname属性值元素无法区分定位,那么在本节课将使用List定位来解决这个问题。List定位首先是使用find_elements_by_XX获取一组相同的class属性的元素,然后使用数组下标来区分标记不同元素进行相关操作。
测试案例1
在新用户注册界面点击添加头像按钮后,选择指定的图片保存作为头像。
1 from find_element.capability import driver,NoSuchElementException 2 3 driver.find_element_by_id('login_register_text').click() 4 driver.find_element_by_id('com.tal.kaoyan:id/activity_register_userheader').click() 5 6 def check_power3(): 7 print('check power3') 8 try: 9 power3=driver.find_element_by_id('android:id/button1') 10 except NoSuchElementException: 11 print('no power3') 12 else: 13 power3.click() 14 check_power3() 15 images=driver.find_elements_by_id('com.tal.kaoyan:id/check') 16 images[21].click() 17 driver.find_element_by_id('com.tal.kaoyan:id/picture_tv_ok').click() 18 driver.find_element_by_id('com.tal.kaoyan:id/menu_crop').click()
list定位综合案例——用户注册
测试场景
- 进入注册界面设置头像
- 输入注册信息:用户名、密码、邮箱
- 完善院校和专业信息 (院校:上海-同济大学 专业:经济学类-统计学-经济统计学)
- 完成注册
代码实现
1 from find_element.capability import driver,NoSuchElementException 2 import random 3 4 def into_register(): 5 '''进入注册页面''' 6 driver.find_element_by_id('login_register_text').click() 7 driver.find_element_by_id('com.tal.kaoyan:id/activity_register_userheader').click() 8 into_register() 9 def check_power3(): 10 '''判断是否有录像和录音权限''' 11 print('check power3') 12 try: 13 power3=driver.find_element_by_id('android:id/button1') 14 except NoSuchElementException: 15 print('no power3') 16 else: 17 power3.click() 18 check_power3() 19 def changes_vrew(): 20 '''更换头像''' 21 images=driver.find_elements_by_id('com.tal.kaoyan:id/check') 22 images[21].click() 23 driver.find_element_by_id('picture_tv_ok').click() 24 driver.find_element_by_id('menu_crop').click() 25 changes_vrew() 26 27 def product_name(): 28 '''随机产生账号''' 29 username='bowen123'+'zxl'+str(random.randint(1,2000)) 30 31 return username 32 name=product_name() 33 34 def product_passwd(): 35 '''随机产生密码''' 36 userpasswd = 'bowen123' + str(random.randint(1, 2000)) 37 return userpasswd 38 passwd=product_passwd() 39 40 def product_email(): 41 '''随机产生邮箱''' 42 useremail='bowen123'+'zxl'+str(random.randint(1,2000))+'@163.com' 43 return useremail 44 email=product_email() 45 46 def name_passwd(): 47 '''注册账号和密码和邮件''' 48 driver.find_element_by_id('activity_register_username_edittext').send_keys(name) 49 driver.find_element_by_id('activity_register_password_edittext').send_keys(passwd) 50 driver.find_element_by_id('activity_register_email_edittext').send_keys(email) 51 driver.find_element_by_id('activity_register_register_btn').click() 52 name_passwd() 53 54 def zhuanye(): 55 '''选择专业''' 56 driver.find_element_by_id('activity_perfectinfomation_major').click() 57 zhuan_images1=driver.find_elements_by_id('com.tal.kaoyan:id/major_subject_title') 58 zhuan_images1[3].click() 59 zhuan_images2=driver.find_elements_by_id('com.tal.kaoyan:id/major_group_title') 60 zhuan_images2[1].click() 61 zhuan_images3=driver.find_elements_by_id('com.tal.kaoyan:id/major_search_item_name') 62 zhuan_images3[3].click() 63 64 zhuanye() 65 66 def school(): 67 '''选择院校''' 68 driver.find_element_by_id('activity_perfectinfomation_school').click() 69 driver.tap([(116,1490),(240,1614)],100) 70 adress_images=driver.find_elements_by_id('more_forum_title') 71 adress_images[3].click() 72 school_images=driver.find_elements_by_id('university_search_item_name') 73 school_images[0].click() 74 driver.tap([(952,1312),(1018,1339)],100) 75 school() 76 77 def kyb_into(): 78 '''进入考研帮''' 79 driver.find_element_by_id('activity_perfectinfomation_goBtn').click() 80 kyb_into()
注意:运行前记得将noRest设置为:desired_caps['noReset']='False' 以免之前的注册残留信息干扰。
报错&解决方案
元素定位报错
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
【解决方案】检查元素id值是否写错。
浙公网安备 33010602011771号