def test_player_performance(start_kill_app): """ Tests the performance to find a unique path. """ old_mouse_move_duration = PlayerSettings.mouse_move_duration iterations = 22 result_wrapper = find("Calculator||Window->*->RegEx: Display is||Text") for mouse_move_duration, expected_duration in [(0, 10), (-1, 1)]: PlayerSettings.mouse_move_duration = mouse_move_duration start_time = time.time() with UIPath("Calculator||Window->*"): if result_wrapper.window_text() != "Display is 0": click("Clear entry||Button") for _ in range(iterations): click("One||Button") click("Zero||Button") click("Plus||Button") click("Equals||Button") assert result_wrapper.window_text() == "Display is " + str( 10 * iterations * 2) duration = time.time() - start_time message = "Duration of the loop using 'duration=" + str( mouse_move_duration) + "': " + str(duration) + " s." print(message) assert duration < expected_duration, message + " It must be lower than " + str( expected_duration) + " s." PlayerSettings.mouse_move_duration = old_mouse_move_duration
def test_asterisk(start_kill_app): """ It clicks the "1" button, then the "+" button, then the "2" button, then the "=" button. Then is does it again, but this time it searches within all the windows with UIPath("*"). """ for root_path in ["Calculator||Window->*", "*->*"]: with UIPath(root_path): click("One||Button") click("Plus||Button") click("Two||Button") click("Equals||Button")
def test_send_keys(start_kill_app): """ Opens Notepad, clicks on the text editor, and sends the keys "This is a error" followed by 5 backspaces, "test.", and an enter. """ with UIPath("Untitled - Notepad||Window"): # edit = click("Text Editor||Edit") # Windows 10 edit = click(u"Text editor||Document") # Windows 11 send_keys("This is a error{BACKSPACE}{BACKSPACE}{BACKSPACE}{BACKSPACE}{BACKSPACE}test.{ENTER}") result = edit.legacy_properties()['Value'] # assert result == 'This is a test.\r\n' # Windows 10 assert result == 'This is a test.\r' # Windows 11
def test_dictionary(start_kill_app): """ It opens the calculator, clicks 1, +, 2, and =, and then checks that the result is 3. """ load_dictionary("Calculator.key", "Calculator.def") with UIPath(shortcut("Calculator")): with UIPath(shortcut("Number pad")): click(shortcut("1")) with UIPath(shortcut("Standard operators")): click(shortcut("+")) with UIPath(shortcut("Number pad")): click(shortcut("2")) with UIPath(shortcut("Standard operators")): click(shortcut("=")) with UIPath(shortcut("Calculator")): wrapper = find("RegEx: Display is ?||Text") assert wrapper.window_text( ) == 'Display is 3', wrapper.window_text() + '. The expected result is 3.'
def test_recorder_click(start_kill_app, wait_recorder_ready): """ Tests the ability to record all clicks. """ hwnd = None while not hwnd: time.sleep(0.1) hwnd = win32gui.FindWindow(None, 'Calculator') print(hwnd) win32gui.MoveWindow(hwnd, 0, 100, 400, 400, True) # with UIPath("Calculator||Window"): # 'UIAWrapper' object had no 'move_window' until Pywinauto 0.7.0 # find().move_window(0, 100, 400, 400, True) recorder = Recorder() recorder.start_recording() start_time = time.time() with UIPath("Calculator||Window->*"): move("Zero||Button", duration=0) move("One||Button", duration=0) wait_recorder_ready(recorder, "One||Button") click("One||Button") move("Two||Button", duration=0) wait_recorder_ready(recorder, "Two||Button") double_click("Two||Button") move("Three||Button", duration=0) wait_recorder_ready(recorder, "Three||Button") triple_click("Three||Button") move("Four||Button", duration=0) wait_recorder_ready(recorder, "Four||Button") triple_click("Four||Button") click("Four||Button") move("Five||Button", duration=0) wait_recorder_ready(recorder, "Five||Button") triple_click("Five||Button") double_click("Five||Button") move("Six||Button", duration=0) wait_recorder_ready(recorder, "Six||Button") triple_click("Six||Button") triple_click("Six||Button") duration = time.time() - start_time record_file_name = recorder.stop_recording() recorder.quit() assert duration < 8, "The duration of this test is " + str( duration) + " s. It must be lower than 8 s" str2num = { "Zero": 0, "One": 1, "Two": 2, "Three": 3, "Four": 4, "Five": 5, "Six": 6 } click_count = [0, 0, 0, 0, 0, 0, 0] with open(record_file_name, 'r') as f: line = f.readline() while line: line = f.readline() for str_number, i_number in str2num.items(): if str_number in line: if "triple_click" in line: click_count[i_number] += 3 elif "double_click" in line: click_count[i_number] += 2 elif "click" in line: click_count[i_number] += 1 for i_number in str2num.values(): assert click_count[i_number] == i_number os.remove(record_file_name)
def test_recorder_mouse_move(start_kill_app): """ Tests the accuracy of the relative coordinates of an element. It records a series of drag-and-drops to draw lines and then plays them back to overlay the lines in white. """ with UIPath("Untitled - Paint||Window"): find().set_focus() find().maximize() click("*->Resize||Button") with UIPath("Resize and Skew||Window->*"): click("Pixels||RadioButton") set_text("Horizontal||Edit#[0,0]", "1900") # Resize the image to 1900 pixels wide click("OK||Button") click("*->Line||Button") click("*->Size||Group->Size||Button") menu_click("3px") # The thickness is set to 3px wrapper = move("*->Using Line tool on Canvas||Pane") time.sleep(1) recorder = Recorder() recorder.relative_coordinate_mode = True recorder.start_recording() time.sleep(2.0) for _ in range(9): x0 = random.randint(wrapper.rectangle().left + 22, wrapper.rectangle().right - 22) y0 = random.randint(wrapper.rectangle().top + 22, wrapper.rectangle().bottom - 22) move((x0, y0), duration=0) x, y = win32api.GetCursorPos() assert (x, y) == (x0, y0) send_keys( "{VK_CONTROL down}{VK_SHIFT down}f{VK_SHIFT up}{VK_CONTROL up}", vk_packet=False) time.sleep(0.5) code = pyperclip.paste() words = code.split("%(") words = words[1].split(')"') move("Untitled - Paint||Window->*->Using Line tool on Canvas||Pane%(" + words[0] + ")", duration=0) x, y = win32api.GetCursorPos() assert (x, y) == (x0, y0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) x1 = random.randint(wrapper.rectangle().left + 22, wrapper.rectangle().right - 22) y1 = random.randint(wrapper.rectangle().top + 22, wrapper.rectangle().bottom - 22) move((x1, y1), duration=0.4) # This duration is mandatory for the recorder time.sleep(0.6) # This pause is mandatory for the recorder win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(0.5) # This pause is mandatory for the recorder recorded_file = recorder.stop_recording() recorder.quit() # The recorded file is patched to remove all that is not needed data = "" with open(recorded_file) as text_file: for line in text_file: if not (("send_keys" in line) or ("wrapper" in line)): if "drag_and_drop" in line: data += line.replace("drag_and_drop", "right_drag_and_drop") else: data += line print(data) # Now the lines are overed in white using the previously recorded drag and drops compiled_code = compile(data, '<string>', 'exec') eval(compiled_code) os.remove(recorded_file) send_keys("{VK_CONTROL down}s{VK_CONTROL up}") recorded_image = str(recorded_file).replace('.py', '.png') select_file("Untitled - Paint||Window->Save As||Window", recorded_image) time.sleep(2) percentage = percentage_white_pixels(recorded_image) os.remove(recorded_image) assert percentage == 100, "All the pixels should be white"
############################################################ # Start google chrome 'GitHub - beuaaa/pywinauto_recorder' # ############################################################ chrome_dir = r'"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"' chrome = pywinauto.Application(backend='uia') chrome.start(chrome_dir + ' --force-renderer-accessibility --start-maximized --guest ') move((66, 66)) time.sleep(0.5) obs.start_recording() #screen_zoom_in() time.sleep(0.5) #screen_zoom_in() time.sleep(1.5) #screen_zoom_in() brian.say("Hi, let me introduce you to 'Pywinauto recorder'.") click("New Tab - Google Chrome (Guest)||Pane->*->Address and search bar||Edit%(-99,-23.33)") brian.say("Go to pywinauto-recorder dot read the docs dot io") send_keys("pywinauto-recorder.readthedocs.io{ENTER}", pause=0.15, vk_packet=True) #screen_zoom_out() brian.say(""" "Pywinauto recorder" is a great tool! It can record user actions and saves them in a Python script. Then the saved Python script can be run to replay the user actions previously recorded. """) ######################################################## # Wait page to be ready and click on the download link # ######################################################## pywinauto_recorder_documentation_page = "Description — pywinauto_recorder documentation - Google Chrome (Guest)||Pane"