def click_exit(): driver = config.driver action = TouchAction(driver) ele1 = driver.find_element_by_id("sessionid") ele2 = driver.find_element_by_id("no_instance") action.press(ele2).move_to(ele1).release().perform() driver.find_element_by_id("close_control").click()
def test_long_press_x_y(self): el1 = self.driver.find_element_by_name('Content') el2 = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el1).move_to(el2).perform() el = self.driver.find_element_by_accessibility_id('Views') # self.assertIsNotNone(el) action.tap(el).perform() el = self.driver.find_element_by_accessibility_id('Expandable Lists') # self.assertIsNotNone(el) action.tap(el).perform() el = self.driver.find_element_by_accessibility_id('1. Custom Adapter') # self.assertIsNotNone(el) action.tap(el).perform() # the element "People Names" is located at 0:110 (top left corner) action.long_press(x=10, y=120).perform() # 'Sample menu' only comes up with a long press, not a tap el = self.driver.find_element_by_name('Sample menu') self.assertIsNotNone(el)
def test_find_elements(self): e1 = TouchAction() e1.press(x=110, y=200) \ .move_to(x=1, y=1) e1.release() sleep(8) elm_category = [] #elm.append(self.driver.find_element_by_xpath("//*[@text='穷游精选']")) textlist = ['分类','自由行'] for i in range (len(textlist)): #print(i) #elm.append(self.driver.find_element_by_xpath("//*[@text=%s"%textlist[i])) elm_category.append(self.driver.find_element_by_xpath("//*[@text='%s']"%textlist[i])) for i in range(len(elm_category)): elm_category[i].click() sleep(1) sleep(1) els_booking = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)') els_booking[6].click() #self.loop_for_list(els_booking) sleep(2) self.driver.find_element_by_xpath("//*[@text='立即预订']").click() sleep(2) self.book_detail() self.driver.find_element_by_xpath("//*[@text='提交订单']").click()
def target(**kwargs): try: driver = kwargs['driver'] startTag = kwargs['startTag'] temp_args = 'app-mitm.py %d' % kwargs['id'] log = kwargs['stdout'] err = kwargs['stderr'] args = ['mitmproxy','-s'] args.append(temp_args) print str(args) self.process = subprocess.Popen(args,stdout=log,stderr=err) if startTag == True: sleep(5) startTag = False else: sleep(2) els = driver.find_elements_by_id('com.cnn.mobile.android.phone:id/broadsheet_headline') el = els[0] location = el.location size = el.size el_x = location['x'] + size['width']/2 el_y = location['y'] + size['height']/2 action = TouchAction(driver) action.press(x=el_x, y=el_y).release().perform() self.process.wait() except: self.status = -1
def test_zoom_image(self): # 查找元素 操作 校验 self.driver.find_element_by_xpath("//UIAButton[@label='Gesture']").click() self.driver.find_element_by_accessibility_id("Image (Zoom and Pinch)").click() image = self.driver.find_element_by_accessibility_id("imageScrollView") location = image.location # 获取image的坐标,为(20,90) print location x = location["x"] y = location["y"] print x print y # 获取image的宽和高,分别为300,300 size = image.size print size a1 = TouchAction() a1.press(x=x, y=y).move_to(x=x, y=y+x).release() a2 = TouchAction() a2.press(x=x, y=y).move_to(x=x, y=y-x).release() # MultiAction(self.driver).add(a1, a2).perform()这个写法是错误的,必须分3行,不能合并到1行! multi_touch = MultiAction(self.driver) multi_touch.add(a1, a2) multi_touch.perform() sleep(3)
def test_press_and_wait(self): el1 = self.driver.find_element_by_name('Content') el2 = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el1).move_to(el2).perform() sleep(SLEEPY_TIME) el = self.driver.find_element_by_accessibility_id('Views') # self.assertIsNotNone(el) action.tap(el).perform() sleep(SLEEPY_TIME) el = self.driver.find_element_by_accessibility_id('Expandable Lists') # self.assertIsNotNone(el) action.tap(el).perform() sleep(SLEEPY_TIME) el = self.driver.find_element_by_accessibility_id('1. Custom Adapter') # self.assertIsNotNone(el) action.tap(el).perform() sleep(SLEEPY_TIME) el = self.driver.find_element_by_name('People Names') # self.assertIsNotNone(el) action.press(el).wait(2000).perform() sleep(SLEEPY_TIME) # 'Sample menu' only comes up with a long press, not a press el = self.driver.find_element_by_name('Sample menu') self.assertIsNotNone(el)
def tap(self, positions, duration=None): """Taps on an particular place with up to five fingers, holding for a certain time :Args: - positions - an array of tuples representing the x/y coordinates of the fingers to tap. Length can be up to five. - duration - (optional) length of time to tap, in ms :Usage: driver.tap([(100, 20), (100, 60), (100, 100)], 500) """ if len(positions) == 1: action = TouchAction(self) x = positions[0][0] y = positions[0][1] if duration: action.long_press(x=x, y=y, duration=duration).release() else: action.tap(x=x, y=y) action.perform() else: ma = MultiAction(self) for position in positions: x = position[0] y = position[1] action = TouchAction(self) if duration: action.long_press(x=x, y=y, duration=duration).release() else: action.press(x=x, y=y).release() ma.add(action) ma.perform() return self
def test_press_and_immediately_release_x_y(self): el = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el, 100, 10).release().perform() el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Bouncing Balls') self.assertIsNotNone(el)
def test_unlock(self): print "start" self.driver.find_element_by_id("com.paic.mo.client:id/pattern_to_login").click() common.wait_appear_by_id(self, "com.paic.mo.client:id/lockPattern", 10) action = TouchAction(self.driver) # 道理一样,就只写两个点了,用的test app的手势密码区域是一整张图片,只能使用坐标进行点击,效果见截图 action.press(x=298, y=1034).wait(1000).move_to(x=719, y=1034).release().perform()
def test_press_and_immediately_release_x_y(self): el = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el, 100, 10).release().perform() sleep(SLEEPY_TIME) el = self.driver.find_element_by_accessibility_id('Bouncing Balls') self.assertIsNotNone(el)
def test_scroll_element_position(self): els = self.driver.find_elements_by_class_name('android.widget.Button') self.assertEqual(4, len(els)) action = TouchAction(self.driver) action.press(els[-1]).move_to(x=10).release().perform() el = self.driver.find_element_by_name('Button7') self.assertIsNotNone(el)
def click_a_point(self, x=0, y=0, duration=100): """ Click on a point""" self._info("Clicking on a point (%s,%s)." % (x,y)) driver = self._current_application() action = TouchAction(driver) try: action.press(x=float(x), y=float(y)).wait(float(duration)).release().perform() except: assert False, "Can't click on a point at (%s,%s)" % (x,y)
def test_press_and_moveto_x_y(self): el1 = self.driver.find_element_by_accessibility_id('Content') el2 = self.driver.find_element_by_accessibility_id('App') action = TouchAction(self.driver) action.press(el1).move_to(el2, 100, 100).release().perform() el = self.driver.find_element_by_accessibility_id('Views') self.assertIsNotNone(el)
def tap(self, x = None, y = None, count = 1): # Tap a coordinates (x,y) if (x == None) | (y == None) | (x > self.X) | (y > self.Y): raise AssertionError('Please input a correct coordinate') while count > 0: action = TouchAction(self.driver) action.press(x = x, y = y).release() action.perform() count-=1 sleep(constant.INTERVAL_1)
def drag_name_to_coords(context, name, coords): coords = eval(coords) el = find_device_element_by_name_or_id(context, name) assert el, u'Element not found' action = TouchAction(context.device) action.press(el) for pair in coords: action.move_to(x=pair[0], y=pair[1]) action.release() action.perform()
def scroll(self, origin_el, destination_el): """Scrolls from one element to another :Args: - originalEl - the element from which to being scrolling - destinationEl - the element to scroll to :Usage: driver.scroll(el1, el2) """ action = TouchAction(self) action.press(origin_el).move_to(destination_el).release().perform() return self
def touchxy(self, el): p = el.location s = el.size p_x = int(p["x"] + s["width"] / 2) p_y = int(p["y"] + s["height"] / 2) # print p_x,p_y # self.dr.tap([(p_x, p_y)],10) action = TouchAction(self.dr) action.press(x=p_x, y=p_y) action.perform() sleep(2)
def test_gesture_lock(self): # go to gesture lock view self.driver.find_element_by_accessibility_id("Gesture Locker (TouchAction)").click() btn1 = self.driver.find_element_by_accessibility_id("Button1") btn2 = self.driver.find_element_by_accessibility_id("Button2") action = TouchAction(self.driver) # move from button 1 to button 2 action.press(btn1).wait(100).move_to(btn2).wait(100).release().perform() pass
def test_parallel_actions(self): el1 = self.driver.find_element_by_accessibility_id('Content') el2 = self.driver.find_element_by_accessibility_id('Animation') self.driver.scroll(el1, el2) el = self.driver.find_element_by_accessibility_id('Views') action = TouchAction(self.driver) action.tap(el).perform() # simulate a swipe/scroll el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Expandable Lists') action.press(el).move_to(x=100, y=-1000).release().perform() el = self.driver.find_element_by_accessibility_id('Layouts') action.press(el).move_to(x=100, y=-1000).release().perform() el = self.driver.find_element_by_accessibility_id('Splitting Touches across Views') action.tap(el).perform() wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.ListView') els = self.driver.find_elements_by_class_name('android.widget.ListView') a1 = TouchAction() a1.press(els[0]) \ .move_to(x=10, y=0).move_to(x=10, y=-75).move_to(x=10, y=-600).release() a2 = TouchAction() a2.press(els[1]) \ .move_to(x=10, y=10).move_to(x=10, y=-300).move_to(x=10, y=-600).release() ma = MultiAction(self.driver, els[0]) ma.add(a1, a2) ma.perform()
def test_by_multiaction(self): print "By multiaction" self.method_name = sys._getframe().f_code.co_name # self.driver.find_element_by_accessibility_id("Image (Zoom and Pinch)").click() self.driver.find_element_by_accessibility_id("Test Gesture").click() # 选取两个点 action1 = TouchAction(self.driver) action1.press(x=87, y=150).move_to(x=45, y=150).release() action2 = TouchAction(self.driver) action2.press(x=120, y=150).move_to(x=150, y=150).release() # action2.press(x=200, y=150).move_to(x=280, y=150).release() # 无法执行 ma = MultiAction(self.driver) ma.add(action1, action2) ma.perform() sleep(5)
def test_move(self): rect = self.driver.get_window_rect() self.driver.find_element_by_xpath("//*[contains(@resource-id,'indicator')]//*[@text='基金']").click() time.sleep(3) action = TouchAction(self.driver) for i in range(8): for j in range(5): # 上下滑动 action.press(x=rect['width']*0.5, y=rect['height']*0.5).move_to(x=rect['width'] *0.2, y=rect[ 'height']*0.2).release().perform() print(i) time.sleep(2) # 左右滑动 action.press(x=rect['width']*0.2, y=rect['height']*0.2).move_to(x=rect['width']*0.5, y=['height']*0.2).release().perform() time.sleep(3)
def by_TouchAction_dingwei(self, x, y): action1 = TouchAction(self.driver) #点击21 #li = action1.press(x=950, y=1400).wait(300).release().wait(300).perform() # return action1.press(x=x, y=y).wait(300).release().wait(300).perform() return action1.press(x=x, y=y).release().perform()
def test_touchaction1(self): action = TouchAction(self.driver) # 获取像素宽高 window_rect = self.driver.get_window_rect() width = window_rect['witdh'] height = window_rect['height'] # 定义滑动起点和终点 x1 = int(width / 2) y_start = int(height * 4 / 5) y_end = int(height * 1 / 5) # 滑动页面 action.press(x=1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()
def touch_move(self, x1, x2, y1, y2, timeout=200): logging.info(f"touch_move:{x1,x2,y1,y2}") ''' 獲取當前屏幕尺寸、坐標、像素,拿取百分比,換設備不容易出錯 :return: ''' action = TouchAction(self.driver) window_rect = self.driver.get_window_rect() width = window_rect['width'] height = window_rect['height'] x_start = int(width * x1) x_end = int(width * x2) y_start = int(height * y1) y_end = int(height * y2) action.press(x=x_start, y=y_start).wait(timeout).move_to( x=x_end, y=y_end).release().perform()
def test_taction11(self): action = TouchAction(self.driver) action.press(x=125, y=185).wait(10).move_to( x=355, y=185).wait(10).move_to(x=600, y=185).wait(10).move_to( x=600, y=415).wait(10).move_to(x=600, y=665).release().perform() window_rect = self.driver.get_window_rect() print(window_rect) width = window_rect['width'] height = window_rect['height'] x1 = int(width / 2) y_start = int(height * 4 / 5) y_end = int(height * 1 / 5) action.press(x=x1, y=y_start).wait(10).move_to(x=x1, y=y_end).release().perform()
def test10(self): '''验证图片删除功能''' startMethod.loginYiZhiBang(self, '13590283182', '123456') ActionMethod.Material(self) startMethod.action_Id(self, case06['图片id'], 'click') #点击选择图片 try: action01 = self.driver.find_elements_by_id(case06['已添加图片ids']) action = TouchAction(self.driver) action.press(action01[0]).wait(1000).release() action.perform() WebDriverWait(self, 3).until( lambda driver: self.driver.find_elements_by_id(case06['确定id'])) self.logger.info('长按第一个图片内容,弹出删除二次确定按钮') startMethod.action_Id(self, case06['确定id'], 'click') except: self.assertEqual(1, 2, msg='长按删除文章失败')
def test_scroll_by_touch_actions(self): self.driver.find_element_by_accessibility_id("Views").click() el = self.driver.find_element_by_accessibility_id("Animation") el2 = self.driver.find_element_by_accessibility_id("Grid") # 1. by TouchAction actions = TouchAction(self.driver) actions.press(el2).move_to(el).release().perform() # 2.或者直接用scroll方法,基于TouchAction的封装 # self.driver.scroll(el2, el) # 3.用drag_and_drop(基于TouchAction的封装),但是要执行多次,执行一次不会到最底层 # self.driver.drag_and_drop(el2, el) # 4. 用swipe、flick (基于TouchAction的封装),但是要执行多次,执行一次不会到最底层 # self.driver.swipe(1100, 1100, 300, 300) # self.driver.flick(1100, 1100, 300, 300) self.driver.find_element_by_accessibility_id("WebView").click() self.assertTrue(self.driver.find_element_by_android_uiautomator("new UiSelector().textContains(\"Hello World\")").is_displayed())
def Pinch(driver): # 定义缩小函数 screen = driver.get_window_size() x = screen["width"] y = screen["height"] print(screen, x, y) action1 = TouchAction(driver) action2 = TouchAction(driver) add_action = MultiAction(driver) # 指定操作 action1.press(x=x * 0.1, y=y * 0.1).wait(1000).move_to( x=x * 0.5, y=y * 0.5).wait(1000).release() action2.press(x=x * 0.9, y=y * 0.9).wait(1000).move_to( x=x * 0.6, y=y * 0.6).wait(1000).release() add_action.add(action1, action2) # 执行操作 add_action.perform()
def test_parallel_actions(self) -> None: self._move_to_splitting_touches_accros_views() els = self.driver.find_elements_by_class_name( 'android.widget.ListView') a1 = TouchAction() a1.press(els[0]) \ .move_to(x=10, y=0).move_to(x=10, y=-75).move_to(x=10, y=-600).release() a2 = TouchAction() a2.press(els[1]) \ .move_to(x=10, y=10).move_to(x=10, y=-300).move_to(x=10, y=-600).release() ma = MultiAction(self.driver, els[0]) ma.add(a1, a2) ma.perform()
def test_touchaction(self): action = TouchAction(self.driver) # 获取当前屏幕的尺寸 print(self.driver.get_window_rect() ) # {'width': 720, 'height': 1280, 'x': 0, 'y': 0} window_rect = self.driver.get_window_rect() width = window_rect["width"] height = window_rect["height"] x1 = int(width / 2) y_start = int(height * 4 / 5) y_end = int(height * 1 / 5) # 因为坐标容易改变,所以不甜建议用这种方法 # action.press(x=349,y=415).wait(200).move_to(x=349,y=516).release().perform() time.sleep(15) action.press(x=x1, y=y_start).wait(1000).move_to( x=x1, y=y_end).release().perform()
def zoom(): action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) #缩放处理 action1.press(x=x * 0.4, y=y * 0.4).wait(1000).move_to( x=x * 0.2, y=y * 0.2).wait(1000).release() action2.press(x=x * 0.6, y=y * 0.6).wait(1000).move_to( x=x * 0.8, y=y * 0.8).wait(1000).release() print('start zoom....') #MultiAction把放大处理追加进去,并且统一执行 zoom_action.add(action1, action2) zoom_action.perform()
def line_unlock(self, step): elements = step['elements'] duration = float(step['data'].get('持续时间', 0.3)) assert isinstance(elements, list) and len( elements) > 2, '坐标格式或数量不对,正确格式如:lock_pattern|1|4|7|8|9' location = self.locat(elements[0]) rect = location.rect w = rect['width'] / 6 h = rect['height'] / 6 key = {} key['1'] = (rect['x'] + 1 * w, rect['y'] + 1 * h) key['2'] = (rect['x'] + 3 * w, rect['y'] + 1 * h) key['3'] = (rect['x'] + 5 * w, rect['y'] + 1 * h) key['4'] = (rect['x'] + 1 * w, rect['y'] + 3 * h) key['5'] = (rect['x'] + 3 * w, rect['y'] + 3 * h) key['6'] = (rect['x'] + 5 * w, rect['y'] + 3 * h) key['7'] = (rect['x'] + 1 * w, rect['y'] + 5 * h) key['8'] = (rect['x'] + 3 * w, rect['y'] + 5 * h) key['9'] = (rect['x'] + 5 * w, rect['y'] + 5 * h) action = TouchAction(self.driver) for i in range(1, len(elements)): k = elements[i] if i == 1: action = action.press( x=key[k][0], y=key[k][1]).wait(duration * 1000) action.move_to(x=key[k][0], y=key[k][1]).wait(duration * 1000) action.release().perform()
def scratch(self, x, y): """ To scratch the scratch card: params: x,y coordinate of the scratch card in the screen """ play = TouchAction(self.driver) for x in range(0, 4): play.press(x=x - 400, y=y) play.move_to(x=x + 700, y=y) play.release().perform() xx = self.game.string_button_pos("CONTINUE") try: if (len(xx)): self.game.tap(x=xx[0], y=xx[1]) except: pass
def flick(self, start_x, start_y, end_x, end_y): """Flick from one point to another point. :Args: - start_x - x-coordinate at which to start - start_y - y-coordinate at which to start - end_x - x-coordinate at which to stop - end_y - y-coordinate at which to stop :Usage: driver.flick(100, 100, 100, 400) """ action = TouchAction(self) action.press(x=start_x, y=start_y).move_to(x=end_x, y=end_y).release() action.perform() return self
def swipe(self, points): automationName = self.driver.capabilities.get('automationName') last_x = 0 last_y = 0 if (automationName == 'Appium'): action_appium = TouchAction(self.driver) for i in range(0, len(points)): x = float(points[i][0]) * self.ratioX y = float(points[i][1]) * self.ratioY if (i == 0): action_appium = action_appium.press(None, x, y).wait(200) elif (i == (len(points) - 1)): action_appium = action_appium.move_to(None, x - last_x, y - last_y).wait(200).release() action_appium.perform() else: action_appium = action_appium.move_to(None, x - last_x, y - last_y).wait(200) last_x = x last_y = y else: action_selendroid = TouchActions(self.driver) for i in range(0, len(points)): x = float(points[i][0]) * self.ratioX y = float(points[i][1]) * self.ratioY if (i == 0): action_selendroid.tap_and_hold(x, y) elif (i == (len(points) - 1)): action_selendroid.move(x, y).release(x, y).perform() else: action_selendroid.move(x, y)
def sudoku(self, draw_el, points: list, duration=200): """ 绘制九宫格 :param duration: 滑动事件 :param draw_el: 九宫格的绘制区域的元素 :param points: 绘制的点列表的形式传入 :return: None """ app_action = TouchAction(self.driver) # rect能同时回获取元素的x、y、width、height,并以字典存储 start_x = draw_el.rect['x'] start_y = draw_el.rect['y'] width = draw_el.rect['width'] height = draw_el.rect['height'] # 设置点的坐标位置 static_point = [{ 'x': start_x + width * 1 / 6, 'y': start_y + height * 1 / 6 }, { 'x': start_x + width * 3 / 6, 'y': start_y + height * 1 / 6 }, { 'x': start_x + width * 5 / 6, 'y': start_y + height * 1 / 6 }, { 'x': start_x + width * 1 / 6, 'y': start_y + height * 3 / 6 }, { 'x': start_x + width * 3 / 6, 'y': start_y + height * 3 / 6 }, { 'x': start_x + width * 5 / 6, 'y': start_y + height * 3 / 6 }, { 'x': start_x + width * 1 / 6, 'y': start_y + height * 5 / 6 }, { 'x': start_x + width * 3 / 6, 'y': start_y + height * 5 / 6 }, { 'x': start_x + width * 5 / 6, 'y': start_y + height * 5 / 6 }] app_action.press(**static_point[points[0] - 1]).wait(duration) for p in points[1:]: app_action.move_to(**static_point[p - 1]).wait(duration) app_action.release().perform()
def hand_write(self, word, offsetx=1, offsety=1, style='ctt1', duration=25, interval=70, logtime=1.5): """ 全屏手写需要指定偏移坐标,目前推荐使用全屏手写,半屏手写目前存在bug,较短的笔画易被字母按键捕获,导致手写失败 | ARGS: | word: | 中文单字 | | | offsetx: | 坐标偏移量 | | | offsety: | 坐标偏移量 | | | style: | 手写体,请参考HandTrack目录uni文件,默认为ctt1 | | | duration: | 手写速度,默认25ms,单位ms | | | interval: | 笔画间隔,默认70ms,单位ms | example: | Hand Write | 来 | | Hand Write | 没 | 100 | 200 | ctt1 | """ if not offsetx: location = self.keyboard.get_keyboard_location() offsetx, offsety = location['x'], location['y'] + 45 else: offsetx, offsety = int(offsetx), int(offsety) movements = self._get_movements(word, style) for mov in movements: action = TouchAction(self._current_application()) lastloc = (mov[0][0], mov[0][1]) action.press(x=lastloc[0] + offsetx, y=lastloc[1] + offsety).wait(ms=duration) if len(mov) == 2: action.move_to(x=mov[1][0] + offsetx, y=mov[1][1] + offsety).wait(ms=duration) else: for loc in mov[1:]: action.move_to(x=loc[0] - lastloc[0], y=loc[1] - lastloc[1]).wait(ms=duration) lastloc = loc action.move_to(x=1, y=1) action.release().perform() time.sleep(float(interval) / 1000.0) logtime = float(logtime) if logtime != 0.0: time.sleep(logtime) self.update_keyboard()
def test_atp_wta(self): self.driver.start_recording_screen() accept_continue = self.driver.find_element_by_xpath( "//*[contains(@resource-id,'com.android.chrome:id/terms_accept') and @text='Accept & continue']" ) accept_continue.click() next_button = self.driver.find_element_by_xpath( "//*[contains(@resource-id,'com.android.chrome:id/next_button') and @text='Next']" ) next_button.click() positive_button = self.driver.find_element_by_xpath( "//*[contains(@resource-id,'com.android.chrome:id/positive_button')]" ) positive_button.click() wait = WebDriverWait(self.driver, 30) search_url = wait.until( EC.element_to_be_clickable(( By.XPATH, "//*[contains(@resource-id,'com.android.chrome:id/search_box_text') and @text='Search or type web address']" ))) search_url.send_keys("https://sonataservices.com") show_link = wait.until( EC.element_to_be_clickable(( By.XPATH, "//android.widget.TextView[@text='https://sonataservices.com']" ))) show_link.click() self.driver.implicitly_wait(5) accept_cookies = wait.until( EC.element_to_be_clickable( (By.XPATH, "//android.widget.Button[@text='ACCEPT']"))) accept_cookies.click() self.driver.implicitly_wait(5) action = TouchAction(self.driver) action.press(x=0, y=1832).move_to(x=0, y=534).release().perform() self.driver.implicitly_wait(5) el1 = self.driver.find_element_by_xpath( "//hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View[1]/android.view.View[1]" ) el1.click() video_recording = self.driver.stop_recording_screen() video_file = self.driver.current_activity + time.strftime( "%Y_%m_%d%H%M%S") filepath = os.path.join("/repository/python/appium", video_file + ".mp4") with open(filepath, "wb") as vd: vd.write(base64.b64decode(video_recording))
def zoom(self): try: x = self.driver.get_window_size()['width'] y = self.driver.get_window_size()['height'] action1 = TouchAction(self.driver) action2 = TouchAction(self.driver) zoom_action = MultiAction(self.driver) action1.press(x=x * 0.4, y=y * 0.4).wait(1000).move_to( x=x * 0.2, y=y * 0.2).wait(1000).release() action2.press(x=x * 0.6, y=y * 0.6).wait(1000).move_to( x=x * 0.8, y=y * 0.8).wait(1000).release() logging.info('Start zoom') zoom_action.add(action1, action2) zoom_action.perform() except: logging.error('Fail to zoom') self.get_Screen_Shot('Fail to zoom')
def case_play_item(self): """预览第一张照片或者视频""" self.case_if_base() self.test.find_byid(gallery_root_view).click() time.sleep(2) self.test.find_byid(action_bar_spinner).click() self.test.find_byclass(TextView, '幻灯片视图').click() time.sleep(5) actions = TouchAction(self.driver) actions.press(x=self.test.get_windows_width() / 2, y=self.test.get_windows_height() / 2).release().perform() time.sleep(1) actions.press(x=self.test.get_windows_width() / 2, y=self.test.get_windows_height() / 2).release().perform() time.sleep(5)
def test_touchaction(self): print("开始等待5秒") time.sleep(5) action = TouchAction(self.driver) #release 释放掉 perform 执行它 wait单位是毫秒 # action.press(x=731,y=2083).wait(200).move_to(x=731,y=484).release().perform() #获取屏幕尺寸 windows_rect = self.driver.get_window_rect()['width'] width = self.driver.get_window_rect()['width'] height = self.driver.get_window_rect()['height'] x1 = int(width / 2) y_start = int(height * 4 / 5) y_end = int(height * 1 / 5) action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()
def get_contacts(self): contacts = [] # 滑动回通讯录顶部 self.driver.find_element( MobileBy.ANDROID_UIAUTOMATOR, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(' 'new UiSelector().text("我的客户").instance(0))') # 添加成员名字到通讯录列表 n = 0 while True: n += 1 self.driver.implicitly_wait(0) action = TouchAction(self.driver) window_rect = self.driver.get_window_rect() width = window_rect['width'] height = window_rect['height'] x_start = int(width * 0.5) x_end = x_start y_start = int(height * 0.8) y_end = int(height * 0.2) try: if self.driver.find_element(MobileBy.XPATH, f"//*[@text='添加成员']"): eles = self.driver.find_elements( MobileBy.XPATH, '//*[@resource-id="com.tencent.wework:id/he1"]/android.widget.TextView' ) for i in eles: contacts.append(i.get_attribute('text')) break except Exception: eles = self.driver.find_elements( MobileBy.XPATH, '//*[@resource-id="com.tencent.wework:id/he1"]/android.widget.TextView' ) for i in eles: contacts.append(i.get_attribute('text')) if n == 8: print(f'当前通讯录名单过长,未打印完整通讯录,仅打印前{n}页') break action.press(x=x_start, y=y_start).move_to(x=x_end, y=y_end).release().perform() self.driver.implicitly_wait(10) print(f'当前页面成员名单为:{contacts}') return contacts
def test_touch(self): # time.sleep(5) # show_display = self.driver.find_element(By.XPATH, 'com.xueqiu.android:id/title_text and @text="推荐"') # print(show_display.is_displayed()) # if show_display == 'true': action = TouchAction(self.driver) print(self.driver.get_window_rect()) get_window_rect = self.driver.get_window_rect() width = get_window_rect['width'] height = get_window_rect['height'] x1 = int(width / 2) y_start = int(height * 0.8) y_end = int(height * 0.2) action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform() time.sleep(10)
def double_slide(driver, elements, a, b, c, d): title = driver.find_elements_by_id(elements)[0].location x = title["x"] y = title["y"] x1 = x + a x2 = x + b y1 = y + c y2 = y + d action1 = TouchAction(driver) action2 = TouchAction(driver) action3 = MultiAction(driver) action1.press(x=x1, y=y1).wait(500).move_to(x=x1, y=y2).wait(500).release() action2.press(x=x2, y=y1).wait(500).move_to(x=x2, y=y2).wait(500).release() action3.add(action1, action2) action3.perform() time.sleep(3)
def select_Model(self): action = TouchAction(self._driver) window_rect = self._driver.get_window_rect() width = window_rect['width'] height = window_rect['height'] x1 = width * 0.5 y1 = height * 0.6 y2 = height * 0.4 i = 0 while i < 10: try: self.find(by="xpath", locator='//*[@text="宝马5系"]').click() break except Exception as e: action.press(x=x1, y=y1).wait(200).move_to(x=x1, y=y2).release().perform() i += 1 return SummaryPage(self._driver)
def test_touchaction(self): """ 滑动操作 :return: """ action = TouchAction(self.driver) # 获取宽和高 window_rect = self.driver.get_window_rect() width = window_rect["width"] height = window_rect["height"] print(window_rect) x1 = int(width / 2) y_start = int(height * 4 / 5) y_end = int(height * 1 / 5) action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()
def yztp(self): """ 验证图片 """ if self.tm == '9': while True: if self.driver.find_elements_by_name('安全校验') != []: if self.skip_Code_fail() == False: return False if self.error_Three_Months() == '1': return '1' if self.driver.find_elements_by_id(self.element_json[u'短信内容ID']) != []: return True if self.driver.find_elements_by_android_uiautomator('new UiSelector().description("开始 ")') != []: os.popen('adb -s %s shell input swipe 50 1000 1000 1000 3000' % self.deviceid) if self.tm == '6': while True: try: for j in range(101, 200, 30): a = TouchAction(self.driver) a.press(x=250, y=1000) for i in range(0, 5): a.move_to(x=50, y=(random.randint(-500, 0))).wait(0) a.move_to(x=50, y=(random.randint(0, 500))).wait(0) for i in range(0, j / 10): a.move_to(x=10, y=0).wait(100) a.release().perform() self.Judgment_Devices() if self.driver.find_elements_by_name('安全校验') != []: if self.skip_Code_fail('Continue') == False: return False if self.error_Three_Months() == False: return False self.Judgment_Devices() if self.driver.find_elements_by_id(self.element_json[u'短信内容ID']) != []: return True if self.driver.find_elements_by_name('开始 ') != []: os.popen('adb -s %s shell input swipe 50 1000 1000 1000 3000' % self.deviceid) if self.driver.find_elements_by_android_uiautomator('new UiSelector().description("开始 ")') != []: os.popen('adb -s %s shell input swipe 50 1000 1000 1000 3000' % self.deviceid) if self.driver.find_elements_by_name('开始') != []: os.popen('adb -s %s shell input swipe 50 1000 1000 1000 3000' % self.deviceid) if self.driver.find_elements_by_android_uiautomator('new UiSelector().description("开始")') != []: os.popen('adb -s %s shell input swipe 50 1000 1000 1000 3000' % self.deviceid) except:pass
def assert_href_called(self, patterns): if not isinstance(patterns, list): patterns = [patterns] table_items = [ x.get_attribute("text") for x in self.driver.find_elements_by_class_name("android.widget.TextView") ] el = self.driver.find_element_by_class_name( "android.support.v7.widget.RecyclerView") for i in range(15): try: action = TouchAction(self.driver) action.press(el).move_to(x=0, y=-30).release().perform() table_items += [ x.get_attribute("text") for x in self.driver.find_elements_by_class_name( "android.widget.TextView") ] except: pass for i in range(15): try: action = TouchAction(self.driver) action.press(el).move_to(x=0, y=100).release().perform() except: pass table_items = list(set(table_items)) print(table_items) for pattern in patterns: found = False for table_item in table_items: if re.search(pattern, table_item): print("HREF called: %s" % pattern) found = True break if not found: self.assertFalse("HREF was not called: %s" % pattern)
def multi_swipeScreen(self): """ :return: None TouchAction对象包含(tab)、press(短按)、move_to(滑动到某个坐标)等方法 通过TouchAction对象,添加tap、move_to等操作,然后perform()执行,可以实现解锁屏幕等功能 规范中的可用事件有: * 短按 (press) * 释放 (release) * 移动到 (moveTo) * 点击 (tap) * 等待 (wait) * 长按 (longPress) * 取消 (cancel) * 执行 (perform) """ action = TouchAction(self.__driver) action.press(x=220, y=700).move_to(x=840, y=700).move_to(x=220, y=1530).move_to(x=840, y=1530).release().perform()
def test_swipe_loop(self): self.driver.find_element_by_xpath( "//*[contains(@resource-id,'title')]//*[@text='热门']") action = TouchAction(self.driver) rect = self.driver.get_window_rect() #怎么确认是否滑动到最后一个页面,没想到处理方案就把数据先写死了 for i in range(3): # self.driver.swipe(1000,1000,200,200) action.press(x=rect["width"] * 0.9, y=rect["height"] * 0.8).move_to( x=rect["width"] * 0.1, y=rect["height"] * 0.8).release().perform() time.sleep(2) for j in range(5): action.press(x=rect["width"] * 0.1, y=rect["height"] * 0.9).move_to( x=rect["width"] * 0.1, y=rect["height"] * 0.1).release().perform()
def unlock(password): # driver=tenxunxinwen() action = TouchAction(tenxunxinwen()) p=[ [230,960],[450,960],[660,960], [230,1180],[450,1180],[660,1180], [230,1390],[450,1390],[660,1390] ] # password=[1,2,3,5,7,8,9] #按住第一个键 action.press(x=p[password[0]-1][0],y=p[password[0]-1][1]) #移动 for i in password[1:]: action.move_to(x=p[i-1][0],y=p[i-1][1]) action.wait(500) action.release() # 提交 action.perform()
def window_scroll(self, car): action = TouchAction(self._driver) window_rect = self._driver.get_window_rect() width = window_rect['width'] height = window_rect['height'] x1 = width * 0.5 y1 = height * 0.6 y2 = height * 0.4 i = 0 while i < 100: try: self.find(by="xpath", locator=f'//*[@text="{car}"]').click() break except Exception as e: action.press(x=x1, y=y1).wait(200).move_to(x=x1, y=y2).release().perform() i += 1
def swipe(self, start_x, start_y, end_x, end_y, duration=None): """Swipe from one point to another point, for an optional duration. :Args: - start_x - x-coordinate at which to start - start_y - y-coordinate at which to start - end_x - x-coordinate at which to stop - end_y - y-coordinate at which to stop - duration - (optional) time to take the swipe, in ms. :Usage: driver.swipe(100, 100, 100, 400) """ # `swipe` is something like press-wait-move_to-release, which the server # will translate into the correct action action = TouchAction(self) action.press(x=start_x, y=start_y).wait(ms=duration).move_to(x=end_x, y=end_y).release() action.perform() return self
def test_unlock(self): self.method_name = sys._getframe().f_code.co_name # 点击gesture Tab self.driver.find_element_by_accessibility_id("Gesture").click() # 进入九宫格界面 self.driver.find_element_by_accessibility_id("Gesture Locker (TouchAction)").click() # 开始滑动 btn1 = self.driver.find_element_by_accessibility_id("Button1") btn3 = self.driver.find_element_by_accessibility_id("Button3") btn5 = self.driver.find_element_by_accessibility_id("Button5") btn6 = self.driver.find_element_by_accessibility_id("Button6") btn7 = self.driver.find_element_by_accessibility_id("Button7") # 按照3-6-5-7-1的顺序设置密码 action = TouchAction(self.driver) action.press(btn3).wait(200).press(btn6).wait(200).press(btn5).wait(200).press(btn7).wait(200).move_to(btn1).press().release().perform() sleep(2) action.press(btn3).wait(200).press(btn6).wait(200).press(btn5).wait(200).press(btn7).wait(200).move_to(btn1).press().release().perform() # 设置成功的提示校验 success_notice = self.driver.find_element_by_class_name("UIAStaticText") success_text = "password has been setup!" self.assertEqual(success_notice.text, success_text) print success_notice.text # 登录校验 action.press(btn3).wait(200).press(btn6).wait(200).press(btn5).wait(200).move_to(btn7).press().move_to(btn1).press().release().perform() self.assertEqual(success_notice.text, "login success!") print success_notice.text
def gesture(driver): """ 画手势密码,现在为第一排一横线,华为nexus6P手机,其他手机需要更换坐标点 :param driver: 初始化获取的webdriver :return:no """ action = TouchAction(driver) # move_to一定是相对坐标 unlock = action.press(x=240, y=915).wait(ms=100)\ .move_to(x=480, y=0).wait(ms=100)\ .move_to(x=480, y=0).wait(ms=100)\ .release() unlock.perform()
def target(**kwargs): try: driver = kwargs['driver'] startTag = kwargs['startTag'] temp_args = 'delay-mitm.py' log = kwargs['stdout'] err = kwargs['stderr'] args = ['mitmproxy','-s'] args.append(temp_args) self.process = subprocess.Popen(args,stdout=log,stderr=err) if startTag == True: sleep(5) startTag = False else: sleep(2) els = driver.find_elements_by_id('com.cnn.mobile.android.phone:id/broadsheet_headline') el = els[0] location = el.location size = el.size el_x = location['x'] + size['width']/2 el_y = location['y'] + size['height']/2 action = TouchAction(driver) action.press(x=el_x, y=el_y).release().perform() f = open(BASE_PATH + '/delay','a+') info = 'click: %d ' % int(time.time()) f.write(info+'\n') f.close() self.process.wait() except: self.status = -1
def scroll(self, origin_el, destination_el, duration=None): """Scrolls from one element to another :Args: - originalEl - the element from which to being scrolling - destinationEl - the element to scroll to - duration - a duration after pressing originalEl and move the element to destinationEl. Default is 600 ms for W3C spec. Zero for MJSONWP. :Usage: driver.scroll(el1, el2) """ # XCUITest x W3C spec has no duration by default in server side if self.w3c and duration is None: duration = 600 action = TouchAction(self) if duration is None: action.press(origin_el).move_to(destination_el).release().perform() else: action.press(origin_el).wait(duration).move_to(destination_el).release().perform() return self
def test_swipe(self): sleep(1) size = self.driver.get_window_size() width = size["width"] start_x = int(width/2) start_y = int(size["height"]/2) sleep(.5) el = self.has_card() while el is not None: swipe_x = width-10 if random.random() > 0.5: swipe_x = 10 action = TouchAction(self.driver) action.press(x=start_x, y=start_y).wait(300).move_to(x=swipe_x,y=start_y).release() action.perform() sleep(.5) el = self.has_card() sleep(.5)
def test_long_press_x_y(self): el1 = self.driver.find_element_by_accessibility_id('Content') el2 = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el1).move_to(el2).perform() el = self.driver.find_element_by_accessibility_id('Views') action.tap(el).perform() el = self.driver.find_element_by_accessibility_id('Expandable Lists') action.tap(el).perform() el = self.driver.find_element_by_accessibility_id('1. Custom Adapter') action.tap(el).perform() # the element "People Names" is located at 430:310 (top left corner) # location can be changed by phone resolusion, OS version action.long_press(x=430, y=310).perform() # 'Sample menu' only comes up with a long press, not a tap el = wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Sample menu")') self.assertIsNotNone(el)
def test_press_and_wait(self): el1 = self.driver.find_element_by_accessibility_id('Content') el2 = self.driver.find_element_by_accessibility_id('Animation') action = TouchAction(self.driver) action.press(el1).move_to(el2).perform() el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Views') action.tap(el).perform() el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Expandable Lists') action.tap(el).perform() el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Custom Adapter') action.tap(el).perform() el = wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("People Names")') action.press(el).wait(2000).perform() # 'Sample menu' only comes up with a long press, not a press el = wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Sample menu")') self.assertIsNotNone(el)