环境:
python3.7
appiumv1.18.0
模拟器网易mumu
安卓版本6.0.1
appium inspector是appium自带的定位插件,支持id、xpath等定位方式
uiautomator是 Android-SDK 目录下携带的定位插件,优点是定位速度快,支持多属性定位,缺点是语法稍复杂
appium inspector定位元素方式如下:
点击start session---填入被测app属性
如:
"platformName": "Android",
"platformVersion": "6.0.1",
"deviceName": "127.0.0.1:7555",
"appPackage": "com.xueqiu.android",
"appActivity": ".view.WelcomeActivityAlias"
再点击start session,界面上方按钮提供元素定位的多种方式,如点击,滑动等
如打雪球app,在搜索框输入alibaba,代码如下
1 # -*- coding:utf-8 -*- 2 # author:xjw 3 # date=2021/3/5 4 from time import sleep 5 6 from appium import webdriver 7 8 desire_cap={ 9 "platformName": "android", 10 "deviceName": "127.0.0.1:7555", 11 "appPackage": "com.xueqiu.android", 12 "appActivity": ".view.WelcomeActivityAlias", 13 "noReset":"True", 14 "dontStopAppOnReset":"True", #执行完不关闭app 15 "skipDeviceInitialization":"True" #跳过安装 权限设置等操作(提升允许速度) 16 } 17 18 driver=webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘,desire_cap) 19 driver.implicitly_wait(10) 20 21 el1 = driver.find_element_by_id("com.xueqiu.android:id/home_search") 22 el1.click() 23 el2 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text") 24 el2.send_keys("alibaba") 25 el3 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[1]") 26 el3.click() 27 driver.find_element_by_accessibility_id() 28 sleep(2) 29 driver.back() 30 driver.quit()
uiautomator定位方式如下:
cmd输入uiautomatorviewer开启uiautomator,进入待测app需要定位的界面---uiautomator刷新屏幕选择元素。
举个例子,进入雪球app,点击我的--用户名密码的登录--输入用户名密码并登陆;
1 # -*- coding:utf-8 -*- 2 # author:xjw 3 # date=2021/3/4 4 5 from appium import webdriver 6 from appium.webdriver.common.touch_action import TouchAction 7 8 9 class Testappium: 10 def setup(self): 11 desired = { 12 "platformName": "Android", 13 "platformVersion": "6.0.1", 14 "deviceName": "127.0.0.1:7555", 15 "appPackage": "com.xueqiu.android", 16 "appActivity": ".view.WelcomeActivityAlias" 17 } 18 19 self.driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub",desired_capabilities=desired) 20 self.driver.implicitly_wait(6) 21 # def teardown(self): 22 # self.driver.quit 23 24 #触屏操作滑动 25 def test_touchaction(self): 26 action=TouchAction(self.driver) 27 window_rect=self.driver.get_window_rect() 28 width=window_rect[‘width‘] 29 height=window_rect[‘height‘] 30 x1=int(width/2) 31 y_start=int(height*1/5) 32 y_end=int(height*4/5) 33 34 action.press(x=x1,y=y_start).wait(200).move_to(x=x1,y=y_end).release().perform() 35 def test_uiautomator(self): 36 self.driver.find_element_by_android_uiautomator(‘new UiSelector().text("我的")‘).click() 37 self.driver.find_element_by_android_uiautomator(‘new UiSelector().textContains("帐号密码登录")‘).click() 38 self.driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("com.xueqiu.android:id/login_account")‘).send_keys(‘1234‘) 39 self.driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("com.xueqiu.android:id/login_password")‘).send_keys(‘abcd‘) 40 self.driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("com.xueqiu.android:id/button_next")‘).click()