import unittest import subprocess from appium import webdriver from appium.webdriver.common.mobileby import MobileBy import time import re from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.actions import interaction from selenium.webdriver.common.actions.pointer_input import PointerInput from selenium.webdriver.common.actions.action_builder import ActionBuilder from selenium.webdriver.common.actions.mouse_button import MouseButton class AlarmClockTest(unittest.TestCase): @classmethod def setUpClass(self): # Start WinAppDriver driver_dir = "C:\Hawk\ias_desktop_automation\driver\YWinAppDriver\WinAppDriver.exe" log = open('C:/Hawk/ias_desktop_automation/logs/WinAppDriverLog.log', 'a') self.driver_proc = subprocess.Popen(['WinAppDriver.exe'], stderr=log, stdout=log, executable=driver_dir, cwd="C:\\Hawk\\ias_desktop_automation\\driver\\YWinAppDriver\\") #set up appium desired_caps = {"app" : "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App"} self.driver = webdriver.Remote( command_executor='http://127.0.0.1:4723', desired_capabilities= desired_caps) @classmethod def tearDownClass(self): self.driver.quit() self.driver_proc.terminate() def get_element(self, by_type, value): ui = self.driver.find_element(by_type, value) if type(ui) is dict: first_element_value = list(ui.values())[0] ui = self.driver.create_web_element(element_id=first_element_value) return ui def right_click_old(self, ui): ActionChains(self.driver).context_click(ui).perform() def right_click_ui(self, ui): # Get values rect_string = ui.get_attribute("BoundingRectangle") m = re.match(r'{X=(\d+),Y=(\d+),Width=(\d+),Height=(\d+)}', rect_string) (x, y, width, height) = m.groups() if m else None if x or y: left_offset = int(width) / 2 top_offset = int(height) / 2 left = left_offset + int(x) top = top_offset + int(y) else: left = 0 top = 0 # Formulate the actions actions = ActionChains(self.driver) actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_PEN , "mouse")) actions.w3c_actions.pointer_action.move_to_location(int(left), int(top)) actions.w3c_actions.pointer_action.context_click() actions.perform() def move_mouse_to(self, x, y): actions = ActionChains(self.driver) actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_PEN , "mouse")) actions.w3c_actions.pointer_action.move_by(x,y) actions.perform() def test_initialize(self): first_timer = self.get_element(MobileBy.XPATH, "//ListItem[1]") source = self.driver.page_source with open("alarmtestsource.txt", 'w') as f: f.write(source) first_timer.click() time.sleep(3) self.get_element(MobileBy.ACCESSIBILITY_ID, "CancelButton").click() time.sleep(3) assert first_timer.is_displayed(), "Could not find first timer item" self.right_click_ui(first_timer) time.sleep(3) if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(AlarmClockTest) unittest.TextTestRunner(verbosity=2).run(suite)