class MultiActionTests(unittest.TestCase): def setUp(self): self._multi_action = MultiAction(DriverStub()) def test_json(self): self.maxDiff = None json = { 'actions': [ [ {'action': 'press', 'options': {'x': None, 'y': None, 'element': 1}}, {'action': 'moveTo', 'options': {'x': 10, 'y': 20}}, {'action': 'release', 'options': {}} ], [ {'action': 'press', 'options': {'x': 11, 'y': 30, 'element': 5}}, {'action': 'moveTo', 'options': {'x': 12, 'y': -300}}, {'action': 'release', 'options': {}} ] ], 'elementId': 0 } t1 = TouchAction(DriverStub()).press(ElementStub(1)).move_to(x=10, y=20).release() t2 = TouchAction(DriverStub()).press(ElementStub(5), 11, 30).move_to(x=12, y=-300).release() self._multi_action.add(t1, t2) self.assertEqual(json, self._multi_action.json_wire_gestures)
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 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_parallel_actions(self): el1 = self.driver.find_element_by_name('Content') el2 = self.driver.find_element_by_name('Animation') self.driver.scroll(el1, el2) el = self.driver.find_element_by_name('Views') action = TouchAction(self.driver) action.tap(el).perform() el = self.driver.find_element_by_name('Expandable Lists') # simulate a swipe/scroll action.press(el).move_to(x=100, y=-1000).release().perform() el = self.driver.find_element_by_name('Splitting Touches across Views') action.tap(el).perform() 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 MultiAction_add(self,action1,action2): """ MultiAction多点触控操作,需要获取self.TouchAction属性方法 :param action1: action1=TouchAction(driver) | action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release() :param action2: :return: """ self.multiaction = MultiAction(self.driver) self.multiaction.add(action1,action2) self.multiaction.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() zoom_action().add(action1, action2) print("start zoom") zoom_action.perform()
def test_services_map_multi_touch(self): enter_menu(self) self.driver.find_element_by_android_uiautomator('textContains("Map")').click() el = self.driver.find_element_by_accessibility_id('Google Map') els = el.find_elements_by_class_name('android.view.View') if len(els) > 3: action0 = TouchAction().tap(els[0]) action1 = TouchAction().tap(els[1]) action2 = TouchAction().tap(els[2]) ma = MultiAction(self.driver, els[0]) ma.add(action0, action1, action2) ma.perform() self.driver.back() self.driver.back()
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_multitouch(self): driver = self.driver driver.implicitly_wait(90) action1 = TouchAction(driver) action1.long_press(None,1181,665).wait(500).release() action2 = TouchAction(driver) action2.long_press(None,838,1104).wait(500).release() action3 = TouchAction(driver) action3.long_press(None,588,1652).wait(500).release() multi = MultiAction(driver) multi.add(action1,action2,action3) multi.perform()
def setUp(self): desired_caps=my_environment({}).set_my_environment("samsung galaxy j1","development version") self.driver = webdriver.Remote('http://localhost:4725/wd/hub', desired_caps) self.my_selenium_method = SeleniumMethods(self.driver) self.touch_action = TouchAction(self.driver) self.multi_action = MultiAction(self.driver)
def pinch(self, page_name): """ Pinch page view, If it is a image, it will be zoom ↘↖ :param page_name: :return: """ try: size = self.get_size(page_name) ma = MultiAction(self.driver) ta1 = TouchAction(self.driver) ta2 = TouchAction(self.driver) ta1.press(x=size["width"] * 0.2, y=size["height"] * 0.1).wait(200). \ move_to(x=size["width"] * 0.5 - 1, y=size["height"] * 0.5 - 1).wait(200).release() ta2.press(x=size["width"] * 0.8, y=size["height"] * 0.7).wait(200). \ move_to(x=size["width"] * 0.5 + 1, y=size["height"] * 0.5 + 1).wait(200).release() ma.add(ta1, ta2) ma.perform() except exceptions.WebDriverException as e: logger.error(f"Page: {page_name} Action:[zoom_out↘↖]\n" f"Msg: zoom_out operation failed! Track:{e}") raise e else: logger.info(f"Page: {page_name} Action:[zoom_out↘↖]\n" f"Msg: zoom_out operation successful!") return self
def usp_up(): cap = { "autoGrantPermissions": True, "platformName": "android", "deviceName": "whatever", "appActivity": ".MainActivity", "appPackage": "com.example.hyan6.pma_upgrade_app", "unicodeKeyboard": True } driver = webdriver.Remote('http://localhost:4723/wd/hub', cap) sleep(3) # size_dict = driver.get_window_size() # x = size_dict.get('width') # y = size_dict.get('height') # x, y = get_size() # print(x, y) action1 = TouchAction(driver) action2 = TouchAction(driver) add_action = MultiAction(driver) # 指定操作 action1.press(x=30, y=1883).wait(1000).move_to(x=30, y=1000).wait(1000).release() action2.press(x=80, y=1883).wait(1000).move_to(x=80, y=1000).wait(1000).release() add_action.add(action1, action2) # 执行操作 add_action.perform() driver.find_element_by_xpath( "/hierarchy/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.Button[1]" ).click()
def default_unpinch(self): window_info = self.window_info height = window_info[1] weight = window_info[2] # 起点横坐标为屏幕中点, 纵坐标为屏幕中点 head_x = int(weight * 0.5) head_y = int(height * 0.5) # 终点横坐标为屏幕中点(与起点横坐标一致), 纵坐标为屏幕下方三分之一 tail_x = head_x + 400 tail_y = head_y line_center_x = (head_x + tail_x) / 2 line_center_y = (head_y + tail_y) / 2 action_head = TouchAction(self.driver) action_tail = TouchAction(self.driver) # 放大 unpinch action_head.press(x=line_center_x, y=line_center_y).move_to(x=head_x, y=head_y).release() action_tail.press(x=line_center_x, y=line_center_y).move_to(x=tail_x, y=tail_y).release() multi_action = MultiAction(self.driver) multi_action.add(action_head) multi_action.add(action_tail) multi_action.perform()
def test_actions_with_waits(self): #pdb.set_trace() el1 = self.driver.find_element_by_xpath("//*[contains(@text,'Content')]") el2 = self.driver.find_element_by_xpath("//*[contains(@text,'Animation')]") self.driver.scroll(el1, el2) el = self.driver.find_element_by_xpath("//*[contains(@text,'View')]") action = TouchAction(self.driver) action.tap(el).perform() el = self.driver.find_element_by_xpath("//*[contains(@text,'Lists')]") # simulate a swipe/scroll action.press(x=100, y=1300).wait(1000).move_to(x=100, y=100).release().perform() sleep(2) el = self.driver.find_element_by_xpath("//*[contains(@text,'Splitting Touches across Views')]") action.tap(el).perform() els = self.driver.find_elements_by_class_name('android.widget.ListView') a1 = TouchAction() a1.press(els[0]).move_to(x=10,y=600).move_to(x=10,y=75).wait(1000).move_to(x=10,y=0).release() a2 = TouchAction() a2.press(els[1]).move_to(x=10, y=600).move_to(x=10, y=300).wait(500).move_to(x=10, y=10).release() ma = MultiAction(self.driver, els[0]) ma.add(a1, a2) ma.perform()
def pinch_action(driver): """ :param driver: :return: """ driver.implicitly_wait(3) try: choose_button = driver.find_element_by_id("com.baidu.BaiduMap:id/dj2") choose_button.click() except NoSuchElementException: print("Images choose element does not exists...") try: choose_button2 = driver.find_element_by_id("com.baidu.BaiduMap:id/byo") choose_button2.click() except NoSuchElementException: print("Images choose element does not exists...") action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) # get screen size relative position usage x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] action1.press(x=x * 0.2, y=y * 0.2).wait(1000).move_to( x=x * 0.4, y=y * 0.4).wait(1000).release() action2.press(x=x * 0.8, y=y * 0.8).wait(1000).move_to( x=x * 0.6, y=y * 0.6).wait(1000).release() print("start to pinch...") zoom_action.add(action1, action2) zoom_action.perform()
def zoom_screen(): desired_caps = { 'platformName': 'Android', 'platformVersion': '10.0', 'deviceName': 'HONOR 20s' } # 连接appium server,告诉appium,代码要操作哪个设备上的哪个APP driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 首先通过屏幕的尺寸获取一个基准坐标值 x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] # 定义两个touchAction,向相反的方向移动 # first_action = TouchAction(driver) # first_action.press(x=x*0.4, y=y*0.4).wait(500).move_to(x=x*0.2, y=y*0.2).release() # second_action = TouchAction(driver) # second_action.press(x=x*0.6, y=x*0.6).wait(500).move_to(x=x*0.8, y=y*0.8).release() # 缩小地图 first_action = TouchAction(driver) first_action.press(x=x * 0.2, y=y * 0.2).wait(500).move_to(x=x * 0.4, y=y * 0.4).release() second_action = TouchAction(driver) second_action.press(x=x * 0.8, y=x * 0.8).wait(500).move_to(x=x * 0.6, y=y * 0.6).release() # 定义多点触控 multi_action = MultiAction(driver) multi_action.add(first_action, second_action) multi_action.perform()
def zoom_action_demo(driver): """ :param driver: :return: """ driver.find_element_by_id('com.baidu.BaiduMap:id/dj2').click() driver.find_element_by_id('com.baidu.BaiduMap:id/byo').click() x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] 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 action...") # multiAction to load independent action zoom_action.add(action1, action2) zoom_action.perform()
def air_pinch(self, center=None, percent=0.5, duration=0.05, steps=1, in_or_out='in', element=None, **kwargs): x, y = cx, cy = (0, 0) if element: element_location = element.location x, y = element_location.get('x'), element_location.get('y') if isinstance(center, (list, tuple)): cx, cy = center width, height = self.get_current_resolution() if x == y == cx == cy == 0: x, y = width / 2, height / 2 elif cx and cy: x, y = cx, cy p1x, p1y = width * 0.2, y p2x, p2y = width * 0.8, y p1 = TouchAction(self.driver) p2 = TouchAction(self.driver) if in_or_out == 'out': p1.press(x=x, y=y).wait(500).move_to(x=p1x, y=p1y).wait( duration * 1000).release() p2.press(x=x, y=y).wait(500).move_to(x=p2x, y=p2y).wait( duration * 1000).release() else: p1.press(x=p1x, y=p1y).wait(500).move_to(x=x, y=y).wait( duration * 1000).release() p2.press(x=p2x, y=p2y).wait(500).move_to(x=x, y=y).wait( duration * 1000).release() for _ in range(steps): ma = MultiAction(self.driver) ma.add(p1, p2) ma.perform()
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_contact(self): # xpath contact_ele = self.driver.find_element( MobileBy.XPATH, "//*[@text='我' and @resource-id ='com.tencent.mm:id/cns']") contact_ele.click() contact_ele2 = self.driver.find_element( MobileBy.XPATH, "//*[@text='收藏' and @resource-id ='android:id/title']") contact_ele2.click() self.driver.implicitly_wait(20) contact_ele2 = self.driver.find_element( MobileBy.XPATH, "//*[@text='新国都集团开年大吉-2021接力跑挑战赛' and @resource-id ='com.tencent.mm:id/bwa']" ) contact_ele2.click() time.sleep(5) action0 = TouchAction(self.driver).tap(x=515, y=1419) action0.release() action0.perform() self.driver.implicitly_wait(10) var = 1 while var == 1: action1 = TouchAction(self.driver) action2 = TouchAction(self.driver) mul_action = MultiAction(self.driver) action1.tap(x=226, y=1677) action2.tap(x=824, y=1707) mul_action.add(action1, action2) mul_action.perform()
def test_actions_with_waits(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) \ .wait(1000) \ .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) \ .wait(500) \ .move_to(x=10, y=-600) \ .release() ma = MultiAction(self.driver, els[0]) ma.add(a1, a2) ma.perform()
def multi_touch(self, *actions): """ MultiTouch 多点触控 它只提供了两个方法 一个add 一个执行perform.官网例子为 action0 = TouchAction().tap(el1) action1 = TouchAction().tap(el2) MultiTouch().add(action0).add(action1).perform """ for i in len(*actions): if i == 0: touch_obj = MultiAction.add(actions[i]) else: touch_obj.add(actions[i]) touch_obj.perform()
def double_tap(self, x, y): action1 = TouchAction(self.driver).long_press(x=x, y=y, duration=500).release() action2 = TouchAction(self.driver).long_press(x=x, y=y, duration=500).release() m_action = MultiAction(self.driver) m_action.add(action1, action2) m_action.perform()
def zoom(self, x1, y1, x2, y2, a1, b1, a2, b2): size = Common(self.driver).size() action1 = TouchAction(self.driver) action2 = TouchAction(self.driver) zoom_action = MultiAction(self.driver) action1.press(x=size[0] * x1, y=size[1] * y1).wait(1000).move_to(x=size[0] * x2, y=size[1] * y2).wait(1000).release() action2.press(x=size[0] * a1, y=size[1] * b1).wait(1000).move_to(x=size[0] * a2, y=size[1] * b2).wait(1000).release() zoom_action.add(action1, action2) zoom_action.perform()
def multi_touch_actions(self, *action_chains): """ 传入多个触摸链,实现多点触控操作,配合action_chains属性使用 :param action_chains: 触摸链 :return: None """ multi_actions = MultiAction(self._driver) multi_actions.add(*action_chains) multi_actions.perform() self._log.info('执行多点触控操作')
def pinch(self): x = self.get_Size()[0] y = self.get_Size()[1] a1 = TouchAction(self.driver).long_press( x=x * 0.6, y=y * 0.83).move_to(x=-x * 0.5, y=0).release() a2 = TouchAction(self.driver).long_press( x=x * 0.4, y=y * 0.83).move_to(x=x * 0.5, y=0).release() ma = MultiAction(self.driver) ma.add(a1, a2) ma.perform()
def multi_touch_actions_perform(self, touch_actions): """ 多点触控执行 :param touch_actions: :return: """ multiActions = MultiAction(self._driver) for actions in touch_actions: multiActions.add(actions) multiActions.perform()
def zoom(self, offset): """放大""" action_1 = TouchAction(self.driver) action_1.press(x=self.width / 2, y=self.height / 2).move_to( x=self.width / 2, y=self.height / 2 - offset).release() action_2 = TouchAction(self.driver) action_2.press(x=self.width / 2, y=self.height / 2).move_to( x=self.width / 2, y=self.height / 2 + offset).release() m = MultiAction(self.driver) m.add(action_1, action_2) m.perform()
def zoom(self): action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) l = self.get_size() action1.press(x=l[0] * 0.4, y=l[1] * 0.4).wait(1000).move_to( x=l[0] * 0.2, y=l[1] * 0.2).wait(1000).release() action2.press(x=l[0] * 0.6, y=l[1] * 0.6).wait(1000).move_to( x=l[0] * 0.8, y=l[1] * 0.8).wait(1000).release() zoom_action.add(action1, action2) zoom_action.perform()
def pinch_or_zoom(driver, action): """ Zoom or Pinch the maximum :param driver: Appium driver :param action: String Type. Value can be 'zoom' or 'pinch' :return: """ # Get Mid point of the screen width_mid_pnt, height_mid_pnt = get_screen_mid_points(driver) # Variables finger1 = TouchAction() finger2 = TouchAction() multi_touch_action = MultiAction(driver) # Zoom if action is "zoom": LOGGER.info("Start zoom") finger1.press(None, width_mid_pnt, height_mid_pnt) finger1.move_to(None, width_mid_pnt, height_mid_pnt+200) finger1.release() # Note: Between the press and move one has to introduce delay as only then the element responds to multiperform finger2.press(None, width_mid_pnt, height_mid_pnt-10) finger2.wait(100) finger2.move_to(None, width_mid_pnt, 60) finger2.release() LOGGER.info("Zoom screen") # Pinch elif action is "pinch": LOGGER.info("Start pinch") finger1.press(None, width_mid_pnt, int(height_mid_pnt*2*.75)) finger1.move_to(None, width_mid_pnt, height_mid_pnt+5) finger1.release() # Note: Between the press and move one has to introduce delay as only then the element responds to multiperform finger2.press(None, width_mid_pnt, int(height_mid_pnt*.25)) finger2.wait(100) finger2.move_to(None, width_mid_pnt, height_mid_pnt-5) finger2.release() LOGGER.info("Pinch screen") # Throw error else: sys.exit("Incorrect action value! Action can be 'zoom' or 'pinch'") multi_touch_action.add(finger1, finger2) multi_touch_action.perform()
def pinch(self): """缩小操作""" action1 = TouchAction(self.driver) # 第一个手势 action2 = TouchAction(self.driver) # 第二个手势 pinch_action = MultiAction(self.driver) # 缩小手势 action1.press(self.width * 0.2, self.height * 0.2).wait(500). \ move_to(self.width * 0.4, self.height * 0.4).release() action2.press(self.width * 0.8, self.height * 0.8).wait(500). \ move_to(self.width * 0.6, self.height * 0.6).release() pinch_action.add(action1, action2) # 两个动作同时进行 pinch_action.perform() # 执行操作
def pinch(): action1=TouchAction(driver) action2=TouchAction(driver) pinch_action=MultiAction(driver) action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release() action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release() pinch_action.add(action1,action2) print('start pinch...') pinch_action.perform()
def toSmaller(): action1 = TouchAction(driver) action2 = TouchAction(driver) big_action = MultiAction(driver) action1.press(x=x * 0.2, y=y * 0.2).wait(1000).move_to( x=x * 0.4, y=y * 0.4).wait(1000).release() action2.press(x=x * 0.8, y=y * 0.8).wait(1000).move_to( x=x * 0.6, y=y * 0.6).wait(1000).release() print("开始缩小") big_action.add(action1, action2) big_action.perform()
def zoom_in(self): width = self.mobile.get_window_size()['width'] height = self.mobile.get_window_size()['height'] action0 = TouchAction(self.mobile).press( x=width / 3, y=height / 2 + 20).wait(ms=450).move_to( x=width / 3, y=height / 2 + 200).release() action1 = TouchAction(self.mobile).long_press( x=width / 3, y=height / 2 - 20).wait(ms=450).move_to( x=width / 3, y=height / 2 - 200).release() action3 = MultiAction(self.mobile) action3.add(action0, action1) action3.perform()
def pinch(self): # 缩小 logging.info('==========Pinch==========') action1 = TouchAction(self.driver) # 第一个手势 action2 = TouchAction(self.driver) # 第二个手势 pinch_action = MultiAction(self.driver) # 放大手势 x, y = self.get_size() action1.press(x=x * 0.2, y=y * 0.2).move_to(x=x * 0.4, y=y * 0.4).release() action2.press(x=x * 0.8, y=y * 0.8).move_to(x=x * 0.6, y=y * 0.6).release() pinch_action.add(action1, action2) # 加载 time.sleep(1) pinch_action.perform() # 执行
def zoom(): action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) action1.press(x=x * 0.5, y=y * 0.5).wait(1000).move_to( x=x * 0.3, y=y * 0.3).wait(1000).release() action2.press(x=x * 0.7, y=y * 0.7).wait(1000).move_to( x=x * 0.9, y=y * 0.9).wait(1000).release() zoom_action.add(action1, action2) print('start zoom...') zoom_action.perform()
def test_smiley_face(self): # just for the fun of it. # this doesn't really assert anything. # paint eye1 = TouchAction() eye1.press(x=150, y=100).release() eye2 = TouchAction() eye2.press(x=250, y=100).release() smile = TouchAction() smile.press(x=110, y=200) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) smile.release() ma = MultiAction(self.driver) ma.add(eye1, eye2, smile) ma.perform() # so you can see it sleep(10)
def test_find_elements(self): # pause a moment, so xml generation can occur sleep(2) self.driver.get("http://m.nike.com/us/en_us/pd/air-zoom-pegasus-32-running-shoe/pid-10294427/pgid-10266840") #els = self.driver.find_elements_by_xpath('//android.widget.TextView') #Eself.assertEqual('API Demos', els[0].text) #el = self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "Animat")]') #self.assertEqual('Animation', el.text) #el = self.driver.find_element_by_accessibility_id("App") #el.click() #els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)') # there are more, but at least 10 visible #self.assertLess(10, len(els)) # the list includes 2 before the main visible elements #self.assertEqual('Action Bar', els[2].text) #els = self.driver.find_elements_by_xpath('//android.widget.TextView') #self.assertLess(10, len(els)) #self.assertEqual('Action Bar', els[1].text) smile = TouchAction() smile.press(x=110, y=200) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) smile.release() ma = MultiAction(self.driver) ma.add(smile) ma.perform() # so you can see it sleep(10)
def test_scroll(self): sleep(2) els = self.driver.find_element_by_class_name('android.widget.ImageButton') els.click() sleep(10) #btn = self.driver.find_element_by_class_name('android.webkit.WebView') #btn.click() # selfview = self.driver.contexts[0] webview = self.driver.contexts[1] print ('>>%s<<' % webview) #contexts = self.driver.contexts self.driver.switch_to.context(webview) self.driver.get("http://m.nike.com/us/en_us/pd/air-zoom-pegasus-32-running-shoe/pid-10294427/pgid-10266840") sleep(10) print ('>>%s<<' % selfview) self.driver.switch_to.context(selfview) print ('Now trying TouchAction') e1 = TouchAction() e1.press(x=150, y=100).release() e2 = TouchAction() e2.press(x=250, y=100).release() smile = TouchAction() smile.press(x=400, y=800) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) smile.release() ma = MultiAction(self.driver) ma.add(e1,e2,smile) ma.perform() sleep(10)
def setUp(self): self._multi_action = MultiAction(DriverStub())
def test_smiley_face(self): # just for the fun of it. # this doesn't really assert anything. self.driver.find_element_by_accessibility_id('Graphics').click() els = self.driver.find_elements_by_class_name('android.widget.TextView') self.driver.scroll(els[len(els)-1], els[0]) el = None try: el = self.driver.find_element_by_accessibility_id('Touch Paint') except Exception as e: els = self.driver.find_elements_by_class_name('android.widget.TextView') self.driver.scroll(els[len(els)-1], els[0]) if el is None: el = self.driver.find_element_by_accessibility_id('Touch Paint') el.click() # paint e1 = TouchAction() e1.press(x=150, y=100).release() e2 = TouchAction() e2.press(x=250, y=100).release() smile = TouchAction() smile.press(x=110, y=200) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=1, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=2, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=3, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=4, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=1) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=0) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=5, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=4, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=3, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=2, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) \ .move_to(x=1, y=-1) smile.release() ma = MultiAction(self.driver) ma.add(e1, e2, smile) ma.perform() # so you can see it sleep(10)
def test_scroll(self): sleep(2) els = self.driver.find_element_by_class_name('android.widget.ImageButton') els.click() sleep(10) btn = self.driver.find_element_by_class_name('android.webkit.WebView') btn.click() sleep(10) webview = self.driver.contexts print ('>>%s<<' % len(webview)) contexts = self.driver.contexts self.driver.switch_to.context('WEBVIEW') e1 = TouchAction() e1.press(x=150, y=100).release() e2 = TouchAction() e2.press(x=250, y=100).release() smile = TouchAction() smile.press(x=400, y=800) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-1, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-2, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-3, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-4, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=1) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=0) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-5, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-4, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-3, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-2, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) \ .move_to(x=-1, y=-1) smile.release() ma = MultiAction(self.driver) ma.add(e1,e2,smile) ma.perform() sleep(10)