互联网app测试经常遇到的问题:当前页面无法查找到某个元素,需要通过下拉操作到下一页或后续页面才能找到想要的内容
应用场景有:微信朋友圈查找某人发的几天前的朋友圈、微博等
可以使用如下方法:
举个例子,雪球app进入我的---下拉页面查找设置并点击
1 self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
2 'scrollable(true).instance(0)).'
3 'scrollIntoView(new UiSelector().text("设置").'
4 'instance(0));').click()
![]()
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()
41 def test_scrolldown(self):
42 self.driver.find_element_by_android_uiautomator('new UiSelector().text("我的")').click()
43 self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
44 'scrollable(true).instance(0)).'
45 'scrollIntoView(new UiSelector().text("设置").'
46 'instance(0));').click()