File indexing completed on 2024-04-21 16:29:27
0001 #!/usr/bin/env python3 0002 0003 # SPDX-License-Identifier: MIT 0004 # SPDX-FileCopyrightText: 2024 Fushan Wen <qydwhotmail@gmail.com> 0005 0006 import os 0007 import time 0008 import unittest 0009 0010 from appium import webdriver 0011 from appium.options.common.base import AppiumOptions 0012 from appium.webdriver.common.appiumby import AppiumBy 0013 from selenium.webdriver.common.actions.action_builder import ActionBuilder 0014 from selenium.webdriver.common.actions.interaction import (POINTER_MOUSE, POINTER_TOUCH) 0015 from selenium.webdriver.common.actions.mouse_button import MouseButton 0016 from selenium.webdriver.common.actions.pointer_input import PointerInput 0017 from selenium.webdriver.common.actions.wheel_actions import WheelActions 0018 0019 0020 class PointerInputTest(unittest.TestCase): 0021 0022 driver: webdriver.Remote 0023 0024 @classmethod 0025 def setUpClass(cls) -> None: 0026 options = AppiumOptions() 0027 # The app capability may be a command line or a desktop file id. 0028 options.set_capability("app", f"{os.getenv('QML_EXEC')} {os.path.dirname(os.path.realpath(__file__))}/pointerinput.qml") 0029 options.set_capability("timeouts", {'implicit': 10000}) 0030 # Boilerplate, always the same 0031 cls.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options) 0032 time.sleep(3) # Make sure the window is visible 0033 0034 @classmethod 0035 def tearDownClass(cls) -> None: 0036 # Make sure to terminate the driver again, lest it dangles. 0037 cls.driver.quit() 0038 0039 def test_touch(self) -> None: 0040 element = self.driver.find_element(AppiumBy.NAME, "result") 0041 0042 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_TOUCH, "finger")) 0043 action.pointer_action.move_to_location(100, 100).click() 0044 action.perform() 0045 self.assertEqual(element.text, "touchscreen") 0046 0047 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_TOUCH, "finger")) 0048 action.pointer_action.move_to_location(200, 200).pointer_down().move_by(200, 200).pointer_up() 0049 action.perform() 0050 self.assertEqual(element.text, "dragged") 0051 0052 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_TOUCH, "finger")) 0053 action.pointer_action.move_to_location(100, 100).pointer_down().pause(3).pointer_up() 0054 action.perform() 0055 self.assertEqual(element.text, "touchscreen longpressed") 0056 0057 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_TOUCH, "finger")) 0058 action.pointer_action.move_to_location(200, 200).pointer_down().move_to_location(400, 400).pointer_up() 0059 action.perform() 0060 self.assertEqual(element.text, "dragged") 0061 0062 def test_mouse(self) -> None: 0063 element = self.driver.find_element(AppiumBy.NAME, "result") 0064 0065 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_MOUSE, "mouse")) 0066 action.pointer_action.move_to_location(100, 100).click() 0067 action.perform() 0068 self.assertEqual(element.text, "mouse left") 0069 0070 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_MOUSE, "mouse")) 0071 action.pointer_action.move_to_location(200, 200).pointer_down().move_by(200, 200).pointer_up() 0072 action.perform() 0073 self.assertEqual(element.text, "dragged") 0074 0075 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_MOUSE, "mouse")) 0076 action.pointer_action.move_to_location(100, 100).click(None, MouseButton.MIDDLE) 0077 action.perform() 0078 self.assertEqual(element.text, "mouse middle") 0079 0080 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_MOUSE, "mouse")) 0081 action.pointer_action.move_to_location(200, 200).pointer_down().move_to_location(400, 400).pointer_up() 0082 action.perform() 0083 self.assertEqual(element.text, "dragged") 0084 0085 action = ActionBuilder(self.driver, mouse=PointerInput(POINTER_MOUSE, "mouse")) 0086 action.pointer_action.move_to_location(100, 100).click(None, MouseButton.RIGHT) 0087 action.perform() 0088 self.assertEqual(element.text, "mouse right") 0089 0090 def test_wheel(self) -> None: 0091 element = self.driver.find_element(AppiumBy.NAME, "result") 0092 0093 action = ActionBuilder(self.driver) 0094 action.wheel_action.scroll(100, 100, 0, -15) 0095 action.perform() 0096 self.assertEqual(element.text, "wheel 0 180") 0097 0098 action = ActionBuilder(self.driver) 0099 action.wheel_action.scroll(100, 100, -15, 0) 0100 action.perform() 0101 self.assertEqual(element.text, "wheel 180 0") 0102 0103 0104 if __name__ == '__main__': 0105 unittest.main()