Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
beuaaa committed Mar 14, 2024
2 parents dbe82da + 887235c commit ffa8e20
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pywinauto_recorder/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ def find_elements(full_element_path=None):
return []
window_candidates = filter_window_candidates(window_candidates)
if len(entry_list) == 1 and len(window_candidates) == 1:
return [window_candidates[0]]
title, control_type, _, _ = get_entry(entry_list[0])
if window_candidates[0].element_info.name == title and window_candidates[0].element_info.control_type == control_type:
return [window_candidates[0]]

candidates = []
title, control_type, _, _ = get_entry(entry_list[-1])
Expand Down
38 changes: 26 additions & 12 deletions pywinauto_recorder/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
from win32gui import LoadCursor as win32gui_LoadCursor
from win32gui import GetCursorInfo as win32gui_GetCursorInfo
from win32gui import MoveWindow as win32gui_MoveWindow
from win32gui import SetForegroundWindow as win32gui_SetForegroundWindow
from win32gui import ShowWindow as win32gui_ShowWindow
from win32gui import IsIconic as win32gui_IsIconic
from win32gui import SetWindowPos as win32gui_SetWindowPos
from win32con import IDC_WAIT, MOUSEEVENTF_MOVE, MOUSEEVENTF_ABSOLUTE, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP, \
MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP, MOUSEEVENTF_WHEEL, \
WHEEL_DELTA, SW_RESTORE
WHEEL_DELTA, SW_RESTORE,HWND_NOTOPMOST, HWND_TOPMOST
from .core import type_separator, path_separator, get_entry, get_entry_list, find_elements, get_sorted_region, \
get_wrapper_path, is_int, is_absolute_path, set_native_window_handle, get_native_window_handle
from functools import partial, update_wrapper
from cachetools import func
import math
from .ocr_wrapper import OCRWrapper
import sys


UI_Coordinates = NewType('UI_Coordinates', (float, float))
Expand Down Expand Up @@ -317,6 +319,8 @@ def _find(
When the [] operator is used and only one element is found, the row and column indices are not tested and the element is returned.
"""
print("🔎", end="", file=sys.stdout)
sys.stdout.flush()
_, _, y_x, _ = get_entry(get_entry_list(full_element_path)[-1])
elements = []
t0 = time.time()
Expand All @@ -328,14 +332,16 @@ def _find(
try:
elements = find_elements(full_element_path)
if not elements:
#print("🔴", end="")
print("🔴", end="", file=sys.stdout)
sys.stdout.flush()
time.sleep(2.0)
except Exception:
#print("🟢", end="")
print("🟢", end="", file=sys.stdout)
sys.stdout.flush()
pass

if len(elements) == 1:
return elements[0]
return elements[0]

if y_x is not None:
nb_y, _, candidates = get_sorted_region(elements)
Expand Down Expand Up @@ -478,8 +484,7 @@ def find_all(
except Exception:
pass
time.sleep(0.1)
msg = "No element found with the UIPath '" + full_element_path + "' after " + str(timeout) + " s of searching."
raise FailedSearch(msg)
return []


def move_window(element_path: Optional[UI_Selector] = None,
Expand Down Expand Up @@ -797,7 +802,9 @@ def distance(pt_1, pt_2):

if duration not in [None, -1]:
duration = float(duration)/2


native_window_handle_before_menu = get_native_window_handle()
set_native_window_handle(None)
menu_entry_list = menu_path.split(path_separator)
str_menu_item = 'MenuItem~Absolute_UIPath' if absolute_path else 'MenuItem'
for i, menu_entry in enumerate(menu_entry_list):
Expand All @@ -818,6 +825,7 @@ def distance(pt_1, pt_2):
time.sleep(0.1) # wait for the menu to open (it is not always instantaneous depending on the animation settings)
if i>0:
UIPath._path_list, UIPath._regex_list = SAV_UIPath_path_list, SAV_UIPath_regex_list
set_native_window_handle(native_window_handle_before_menu)
return w_to_return


Expand Down Expand Up @@ -1048,7 +1056,7 @@ def __init__(self, app, native_window_handle=None):
self.native_window_handle = native_window_handle


def start_application(cmd_line, timeout=10):
def start_application(cmd_line, timeout=10, wait_for_idle=True):
"""
This function starts an application
Expand All @@ -1059,10 +1067,14 @@ def start_application(cmd_line, timeout=10):
desktop = pywinauto.Desktop(backend='uia', allow_magic_lookup=False)
window_candidates_1 = desktop.windows()
app = pywinauto.Application(backend="win32")
app.start(cmd_line=cmd_line, timeout=timeout)
app.start(cmd_line=cmd_line, timeout=timeout, wait_for_idle=wait_for_idle)
time.sleep(2)
window_candidates_2 = desktop.windows()
diff = set(window_candidates_2) - set(window_candidates_1)
while not diff:
time.sleep(1)
window_candidates_2 = desktop.windows()
diff = set(window_candidates_2) - set(window_candidates_1)
native_window_handle = list(diff)[0].handle # We assume that there is only one window
set_native_window_handle(native_window_handle)
return UIApplication(app, native_window_handle)
Expand Down Expand Up @@ -1105,8 +1117,10 @@ def focus_on_application(application=None):
else:
set_native_window_handle(application.native_window_handle)
time.sleep(1)
win32gui_ShowWindow(application.native_window_handle, SW_RESTORE)
win32gui_SetForegroundWindow(application.native_window_handle)
if win32gui_IsIconic(application.native_window_handle):
win32gui_ShowWindow(application.native_window_handle, SW_RESTORE)
win32gui_SetWindowPos(application.native_window_handle, HWND_TOPMOST, 0, 0, 0, 0, 3)
win32gui_SetWindowPos(application.native_window_handle, HWND_NOTOPMOST, 0, 0, 0, 0, 3)


def kill_application(application, timeout=10):
Expand Down

0 comments on commit ffa8e20

Please sign in to comment.