コード例 #1
0
 def test_find_multiple_elements_by_image_just_returns_one(self) -> None:
     wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, "App")
     image_path = desired_capabilities.PATH(
         'file/find_by_image_success.png')
     els = self.driver.find_elements_by_image(image_path)
     els[0].click()
     wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, "Alarm")
コード例 #2
0
    def test_open_notifications(self) -> None:
        for word in ['App', 'Notification', 'Status Bar', ':-|']:
            wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR,
                             f'new UiSelector().text("{word}")').click()

        self.driver.open_notifications()
        sleep(1)
        with pytest.raises(NoSuchElementException):
            self.driver.find_element_by_android_uiautomator, 'new UiSelector().text(":-|")'

        els = self.driver.find_elements_by_class_name(
            'android.widget.TextView')
        # sometimes numbers shift
        title = False
        body = False
        for el in els:
            text = el.text
            if text == 'Mood ring':
                title = True
            elif text == 'I am ok':
                body = True
        assert title
        assert body

        self.driver.keyevent(4)
        sleep(1)
        self.driver.find_element_by_android_uiautomator(
            'new UiSelector().text(":-|")')
コード例 #3
0
    def test_element_find_multiple_elements(self) -> None:
        wait_for_element(self.driver, MobileBy.CLASS_NAME,
                         'android.widget.ListView')
        el = self.driver.find_element_by_class_name('android.widget.ListView')

        sub_els = el.find_elements_by_accessibility_id('Animation')
        assert isinstance(sub_els, list)
コード例 #4
0
    def _move_to_custom_adapter(self) -> None:
        self._move_to_views()
        action = TouchAction(self.driver)

        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()
コード例 #5
0
 def test_find_single_element(self) -> None:
     wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR,
                      'new UiSelector().text("Accessibility")').click()
     wait_for_element(
         self.driver, MobileBy.ANDROID_UIAUTOMATOR,
         'new UiSelector().text("Accessibility Node Querying")').click()
     el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID,
                           'Task Take out Trash')
     assert el is not None
コード例 #6
0
    def test_press_and_wait(self) -> None:
        self._move_to_custom_adapter()
        action = TouchAction(self.driver)

        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")')
        assert el is not None
コード例 #7
0
    def test_send_keys(self) -> None:
        for text in ['App', 'Activity', 'Custom Title']:
            wait_for_element(
                self.driver, MobileBy.XPATH,
                f"//android.widget.TextView[@text='{text}']").click()

        el = wait_for_element(self.driver, MobileBy.ID,
                              '{}:id/left_text_edit'.format(APIDEMO_PKG_NAME))
        el.send_keys(' text')

        assert 'Left is best text' == el.text
コード例 #8
0
    def test_element_find_single_element(self) -> None:
        wait_for_element(self.driver, MobileBy.ANDROID_UIAUTOMATOR,
                         'new UiSelector().text("Accessibility")').click()
        wait_for_element(
            self.driver, MobileBy.ANDROID_UIAUTOMATOR,
            'new UiSelector().text("Accessibility Node Querying")').click()
        el = wait_for_element(self.driver, MobileBy.CLASS_NAME,
                              'android.widget.ListView')

        sub_el = el.find_element_by_accessibility_id('Task Take out Trash')
        assert sub_el is not None
コード例 #9
0
    def test_tap_twice(self) -> None:
        el = self.driver.find_element_by_accessibility_id('Text')
        action = TouchAction(self.driver)
        action.tap(el).perform()

        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'LogTextBox')
        action.tap(el).perform()

        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Add')
        action.tap(el, count=2).perform()

        els = self.driver.find_elements_by_class_name('android.widget.TextView')
        assert 'This is a test\nThis is a test\n' == els[1].get_attribute("text")
コード例 #10
0
    def test_find_throws_no_such_element(self) -> None:
        image_path = desired_capabilities.PATH(
            'file/find_by_image_failure.png')
        with open(image_path, 'rb') as png_file:
            b64_data = base64.b64encode(png_file.read()).decode('UTF-8')

        with pytest.raises(TimeoutException):
            wait_for_element(self.driver,
                             MobileBy.IMAGE,
                             b64_data,
                             timeout_sec=3)

        with pytest.raises(NoSuchElementException):
            self.driver.find_element_by_image(image_path)
コード例 #11
0
    def test_driver_drag_and_drop(self) -> None:
        self._move_to_views()
        action = TouchAction(self.driver)

        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Drag and Drop')
        action.tap(el).perform()

        dd3 = wait_for_element(self.driver, MobileBy.ID, '{}:id/drag_dot_3'.format(APIDEMO_PKG_NAME))
        dd2 = self.driver.find_element_by_id('{}:id/drag_dot_2'.format(APIDEMO_PKG_NAME))

        self.driver.drag_and_drop(dd3, dd2)

        el = wait_for_element(self.driver, MobileBy.ID, '{}:id/drag_result_text'.format(APIDEMO_PKG_NAME))
        assert 'Dropped!' in el.text
コード例 #12
0
    def test_drag_and_drop(self) -> None:
        self._move_to_views()
        action = TouchAction(self.driver)

        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Drag and Drop')
        action.tap(el).perform()

        dd3 = wait_for_element(self.driver, MobileBy.ID, '{}:id/drag_dot_3'.format(APIDEMO_PKG_NAME))
        dd2 = self.driver.find_element_by_id('{}:id/drag_dot_2'.format(APIDEMO_PKG_NAME))

        # dnd is stimulated by longpress-move_to-release
        action.long_press(dd3).move_to(dd2).release().perform()

        el = wait_for_element(self.driver, MobileBy.ID, '{}:id/drag_result_text'.format(APIDEMO_PKG_NAME))
        assert 'Dropped!' in el.text
コード例 #13
0
    def test_press_and_immediately_release_x_y(self) -> None:
        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')
        assert el is not None
コード例 #14
0
    def test_set_text(self) -> None:
        self.driver.find_element_by_android_uiautomator(
            'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));'
        ).click()

        wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID,
                         'Controls').click()
        wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID,
                         '1. Light Theme').click()

        el = wait_for_element(self.driver, MobileBy.CLASS_NAME,
                              'android.widget.EditText')
        el.send_keys('original text')
        el.set_text('new text')

        assert 'new text' == el.text
コード例 #15
0
    def _move_to_views(self) -> None:
        el1 = self.driver.find_element_by_accessibility_id('Content')
        el2 = self.driver.find_element_by_accessibility_id('Animation')
        self.driver.scroll(el1, el2)

        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Views')
        action = TouchAction(self.driver)
        action.tap(el).perform()
コード例 #16
0
    def test_press_and_moveto_x_y(self) -> None:
        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 = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Views')
        assert el is not None
コード例 #17
0
    def _move_to_splitting_touches_accros_views(self) -> None:
        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.ID, 'io.appium.android.apis:id/list1')
コード例 #18
0
    def test_driver_swipe(self) -> None:
        el = self.driver.find_element_by_accessibility_id('Views')
        action = TouchAction(self.driver)
        action.tap(el).perform()

        with pytest.raises(NoSuchElementException):
            self.driver.find_element_by_accessibility_id('ImageView')

        self.driver.swipe(100, 1000, 100, 100, 800)
        el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'ImageView')
        assert el is not None
コード例 #19
0
    def test_long_press_x_y(self) -> None:
        self._move_to_custom_adapter()
        action = TouchAction(self.driver)

        # 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")')
        assert el is not None
コード例 #20
0
    def test_find_based_on_image_template(self) -> None:
        image_path = desired_capabilities.PATH(
            'file/find_by_image_success.png')
        with open(image_path, 'rb') as png_file:
            b64_data = base64.b64encode(png_file.read()).decode('UTF-8')

        el = wait_for_element(self.driver, MobileBy.IMAGE, b64_data)
        size = el.size
        assert size['width'] is not None
        assert size['height'] is not None
        loc = el.location
        assert loc['x'] is not None
        assert loc['y'] is not None
        rect = el.rect
        assert rect['width'] is not None
        assert rect['height'] is not None
        assert rect['x'] is not None
        assert rect['y'] is not None
        assert el.is_displayed()
        el.click()
        wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, "Alarm")
コード例 #21
0
    def test_driver_multi_tap(self) -> None:
        el = self.driver.find_element_by_accessibility_id('Graphics')
        action = TouchAction(self.driver)
        action.tap(el).perform()

        wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.TextView')
        els = self.driver.find_elements_by_class_name('android.widget.TextView')
        self.driver.scroll(els[len(els) - 1], els[0])

        els = self.driver.find_elements_by_class_name('android.widget.TextView')
        if els[len(els) - 1].get_attribute('name') != 'Xfermodes':
            self.driver.scroll(els[len(els) - 1], els[0])

        el = self.driver.find_element_by_accessibility_id('Touch Paint')
        action.tap(el).perform()

        positions = [(100, 200), (100, 400)]

        # makes two dots in the paint program
        # THE TEST MUST BE WATCHED TO CHECK IF IT WORKS
        self.driver.tap(positions)
        sleep(10)
コード例 #22
0
 def test_sending_custom_keys(self) -> None:
     edit_field = wait_for_element(self.driver, MobileBy.CLASS_NAME,
                                   'XCUIElementTypeTextView')
     flagsShift = 1 << 1
     self.driver.execute_script(
         'macos: keys',
         {
             'keys': [
                 {
                     'key': 'h',
                     'modifierFlags': flagsShift,
                 },
                 {
                     'key': 'i',
                     'modifierFlags': flagsShift,
                 },
             ]
         },
     )
     assert edit_field.text == 'HI'
コード例 #23
0
 def test_tap(self) -> None:
     el = self.driver.find_element_by_accessibility_id('Animation')
     action = TouchAction(self.driver)
     action.tap(el).perform()
     el = wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Bouncing Balls')
     assert el is not None
コード例 #24
0
 def test_clear_text_field(self) -> None:
     edit_field = wait_for_element(self.driver, MobileBy.CLASS_NAME, 'XCUIElementTypeTextView')
     edit_field.send_keys('helloworld')
     assert edit_field.text == 'helloworld'
     edit_field.clear()
     assert edit_field.text == ''