APP元素定位方式与Web元素定位方式大体相同,APP自动化测试中最重要的一部分是对元素进行定位,实现对APP的控制交互。Appium常用的定位方式有Accessibility ID、Class name、ID、Name、XPath、Android UiAutomator(UiAutomator2)等。
使用resource-id属性定位,iOS中使用name属性。
elem = driver.find_element_by_id("com.xueqiu.android:id/enter_stock_fund")
elem = driver.find_element(MobileBy.ID,"com.xueqiu.android:id/enter_stock_fund")
在写Android和iOS自动化测试用例时,可以使用这种定位方法,使代码可重用, 实现跨平台自动化测试。iOS的Accessibility ID为UI元素的名称, Android的Accessibility ID为“ content-desc”属性值。
elem = driver.find_element_by_accessibility_id("Accessibility")
elem = driver.find_element(MobileBy.ACCESSIBILITY_ID,"Accessibility")
使用控件的class属性
elem = driver.find_element_by_class_name("classname")
elem = driver.find_element(MobileBy.CLASS_NAME,"classname")
APP Xpath定位与Web元素Xpath定位一样,Xpath定位语法可参考文章 Web自动化测试:xpath & CSS Selector定位,可以通过父结点定位子结点、子结点定位父结点、子结点定位兄弟结点、 爷爷结点定位孙子结点。
Xpath定位语法参考:https://www.w3school.com.cn/xpath/xpath_syntax.asp
表达式 | 描述 |
---|---|
nodename | 选取此节点的所有子节点。 |
/ | 从根节点选取。 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
. | 选取当前节点。 |
.. | 选取当前节点的父节点。 |
@ | 选取属性。 |
点击雪球APP “行情”
elem = driver.find_element_by_xpath(‘//*[@text="行情"]‘).click()
elem.click()
测试步骤:
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
class TestToast():
# API Demos
def setup(self):
desired_caps = {
‘platformName‘: ‘android‘,
‘platformVersion‘: ‘6.0.1‘,
‘deviceName‘: ‘127.0.0.1:7555‘,
‘appPackage‘: ‘io.appium.android.apis‘,
‘appActivity‘: ‘io.appium.android.apis.view.PopupMenu1‘,
‘automationName‘: ‘Uiautomator2‘
}
self.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
def test_toast(self):
self.driver.find_element_by_class_name("android.widget.Button").click()
self.driver.find_element_by_xpath("//*[@text=‘Search‘]").click()
# print(self.driver.page_source)
print(self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,‘Clicked popup‘)]").text)
结果:
Launching pytest with arguments test_toast.py::TestToast::test_toast in D:\ProgramWorkspace\TestingDemo\test_appium
============================= test session starts =============================
platform win32 -- Python 3.7.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- D:\Anaconda3\python.exe
cachedir: .pytest_cache
hypothesis profile ‘default‘ -> database=DirectoryBasedExampleDatabase(‘D:\\ProgramWorkspace\\TestingDemo\\test_appium\\.hypothesis\\examples‘)
rootdir: D:\ProgramWorkspace\TestingDemo\test_appium
plugins: allure-pytest-2.8.12, hypothesis-5.5.4, arraydiff-0.3, assume-2.3.2, astropy-header-0.1.2, doctestplus-0.5.0, openfiles-0.4.0, remotedata-0.3.2, rerunfailures-9.1
collecting ... collected 1 item
test_toast.py::TestToast::test_toast
============================= 1 passed in 37.07s ==============================
Process finished with exit code 0
PASSED [100%]Clicked popup menu item Search
测试过程:
Note:
实例使用的是网易mumu浏览器,开启后,使用adb命令:adb connect 127.0.0.1:7555
连接模拟器
执行代码前需要开启appium服务器
python代码:
import pytest
from appium import webdriver
class TestXueQiu:
def setup(self):
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘
desired_caps[‘platformVersion‘] = ‘6.0.1‘
desired_caps[‘deviceName‘] = ‘127.0.0.1:7555‘
desired_caps[‘appPackage‘] = ‘com.xueqiu.android‘
desired_caps[‘automationName‘] = ‘Uiautomator2‘
desired_caps[‘appActivity‘] = ‘com.xueqiu.android.common.MainActivity‘
desired_caps[‘newCommandTimeout‘] = 3000
desired_caps[‘noReset‘] = True
desired_caps[‘dontStopAppOnReset‘] = True
desired_caps[‘skipDeviceInitialization‘] = True
desired_caps[‘unicodeKeyboard‘] = True
desired_caps[‘resetKeybBoard‘] = True
self.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)
self.driver.implicitly_wait(15)
def teardown_method(self):
self.driver.quit()
def test_search(self, searchkey, type, price):
self.driver.find_element_by_id("com.xueqiu.android:id/tv_search").click()
self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys("招商银行")
current_price = self.driver.find_element_by_xpath("//*[@text=‘SH600036‘]").click()
current_price = self.driver.find_element_by_xpath(
"//*[@text=‘SH600036‘]/../../..//*[@resource-id=‘com.xueqiu.android:id/current_price‘]").text
print(current_price)
assert float(current_price) > 40
执行结果:
文章标题:Appium元素定位(一)
本文作者:hiyo
本文链接:https://www.cnblogs.com/hiyong/p/14163690.html
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!
原文:https://www.cnblogs.com/hiyong/p/14163690.html