appium python实例_Appium Python 常用元素定位方法&测试小米计算器实例

常用的元素定位方法

Uiautomator 定位

image.png

text属性的方法

#text

driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View")').click()

#textContains

driver.find_element_by_android_uiautomator('new UiSelector().textContains("View")').click()

#textStartsWith

driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("Custom")').click()

#textMatches

driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^Custom.*")').click()

class属性的方法

#className

driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")').click()

#classNameMatches

driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches(".*TextView$").text("Custom View")').click()

resourceId属性的方法

#resourceId

driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")')

#resourceIdMatches

driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".*id/text1$")')

元素的其他属性

driver.find_element_by_android_uiautomator('new UiSelector().clickable(true).text("Custom View")').click()

ID定位

# resourceId属性的方法

driver.find_element_by_id("com.miui.calculator:id/btn_plus").click()

#以accessibility_id进行定位,对Android而言,就是content-description属性

driver.find_element_by_accessibility_id('push_button').click()

ClassName 定位

# 定位唯一元素

self.driver.find_element_by_class_name("android.widget.EditText")

# 找到所有android.widget.EditText并定位第一个

self.driver.find_elements_by_class_name("android.widget.EditText")[0]

Name定位

#根据name进行定位,对于android来说,就是text属性

driver.find_element_by_name("1").click()

举例 小米自带计算器测试1

#coding=utf-8

from appium import webdriver

import sys

print "here is :",__file__,sys._getframe().f_lineno #打印行数,调试用

desired_caps = {}

desired_caps['platformName'] = 'Android'

desired_caps['platformVersion'] = "7.0"

desired_caps['deviceName'] = '777ddad30504'

desired_caps['appPackage'] = 'com.miui.calculator'

desired_caps['appActivity'] = '.cal.CalculatorActivity'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.find_element_by_android_uiautomator('new UiSelector().text("1")').click()

driver.find_element_by_id("com.miui.calculator:id/btn_plus").click()

driver.find_element_by_name("5").click()

driver.find_element_by_id('com.miui.calculator:id/btn_equal').click()

driver.quit()

定位方法比较

1· driver.find_element_by_name("5").click()

2· driver.find_element_by_id("com.miui.calculator:id/btn_5").click()

3· driver.find_element_by_android_uiautomator('new UiSelector().text("5")').click()

比较三种定位方法,第二种by_id最可靠,推荐。

原因分析: 可能有重复的text值,如下图,已经输入的1和数字键盘的1的text值都是1,因此按照 by_name和text查找会出错、

举例 小米计算器测试2

#coding=utf-8

from appium import webdriver

import unittest

class TesXiaomiCalc(unittest.TestCase):

def setUp(self):

# set up appium

desired_caps = {}

desired_caps['platformName'] = 'Android'

desired_caps['platformVersion'] = "7.0"

desired_caps['deviceName'] = '777ddad30504'

desired_caps['appPackage'] = 'com.miui.calculator'

desired_caps['appActivity'] = '.cal.CalculatorActivity'

self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

def teartDown(self):

self.driver.quit()

def test_plus(self):

#清空环境

self.driver.find_element_by_id("com.miui.calculator:id/btn_c_1").click()

calc = '159+95+5=259'

ca, res = calc.split('=')

for c in ca:

if c == '+':

c = 'plus'

if c == '0':

c = '0_p'

self.driver.find_element_by_id("btn_%s" % c).click() # 依次输入159 + 95 + 5 =

self.driver.find_element_by_id("btn_equal").click()

realStr = self.driver.find_element_by_android_uiautomator(\

'new UiSelector().className("android.widget.TextView").instance(6)').text #6为259的位置

self.assertEqual(res, realStr)

if __name__ == '__main__':

suite = unittest.TestLoader().loadTestsFromTestCase(TesXiaomiCalc)

unittest.TextTestRunner(verbosity=2).run(suite)

遇到的问题: 计算结果使用下面方法无法定位,计算结果数值,无id,className不唯一,作者使用了下面方法,失败。

realStr=self.driver.find_elements_by_class_name("android.widget.TextView")[-2].text

解决办法:

realStr = self.driver.find_element_by_android_uiautomator(\

'new UiSelector().className("android.widget.TextView").instance(6)').text

关于instance(6) 中“6”定位方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值