Skip to content

Commit

Permalink
Modified dictionary file format
Browse files Browse the repository at this point in the history
  • Loading branch information
David Pratmarty committed Apr 29, 2021
1 parent 3293a1e commit 722a9dd
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 77 deletions.
1 change: 0 additions & 1 deletion Tutorials/Tutorial_01/Tutoriel.bat

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
time.sleep(1.5)
with Window(u"Pywinauto recorder Setup ||Window"):
left_click("Install||Button")
left_click("Pywinauto recorder Setup||Window->OK||Button")
left_click("Show details||Button")
left_click("Pywinauto recorder Setup||Window->Yes||Button")
time.sleep(1)
send_keys("{ENTER}")
speech = """
Expand Down Expand Up @@ -139,17 +140,12 @@
time.sleep(1)
move("||TitleBar->Close||Button")
time.sleep(1)
move("Installation Complete||Text")
time.sleep(1)
move("Setup was completed successfully.||Text")
time.sleep(1)
move("Setup was completed successfully.||Image")
wrapper = find("*->Completed||List").get_item(-2)
move(wrapper)
time.sleep(1)
move("||ProgressBar") # get value 100%
time.sleep(1)
brian.say("For instance this progress bar is interesting because Pywinauto recorder informs you that you can use a Pywinauto method to retrieve the progress percentage.", wait_until_the_end_of_the_sentence=True)
left_click("Show details||Button")
move("Completed||List->Completed||ListItem->Completed||Text", duration=3)
move("< Back||Button")
time.sleep(1)
move("Close||Button")
Expand Down Expand Up @@ -221,17 +217,18 @@
brian.say("I press 'alt' 'control' 'r' to stop recording.")
send_keys("{VK_CONTROL down}""{VK_MENU down}""r""{VK_MENU up}""{VK_CONTROL up}")
brian.say("And click on Start playing in the tray menu.")
time.sleep(1.5)
send_keys("{VK_LWIN}")
#time.sleep(1.5)
#send_keys("{VK_LWIN}")
time.sleep(1.5)
with Window(u"Taskbar||Pane"):
with Region(u"||Pane"):
left_click(u"Notification Chevron||Button")
with Window(u"Notification Overflow||Pane"):
with Region(u"Overflow Notification Area||ToolBar"):
right_click(u"Pywinauto recorder||Button")
time.sleep(0.5)
click(button='right')
right_click(u"Pywinauto recorder||Button")
#time.sleep(0.5)
#click(button='right')
with Window(u"Context||Menu"):
left_click(u"Start replaying clipboard||MenuItem")
time.sleep(1)
Expand All @@ -242,23 +239,25 @@
# Open 'Pywinauto Recorder' folder #
####################################
brian.say("The generated file is in 'Pywinauto Recorder' folder under your home folder.")
time.sleep(1.5)
send_keys("{VK_LWIN}")
#time.sleep(1.5)
#send_keys("{VK_LWIN}")
time.sleep(1.5)
with Window(u"Taskbar||Pane"):
with Region(u"||Pane"):
left_click(u"Notification Chevron||Button")
with Window(u"Notification Overflow||Pane"):
with Region(u"Overflow Notification Area||ToolBar"):
right_click(u"Pywinauto recorder||Button")
time.sleep(0.5)
click(button='right')
right_click(u"Pywinauto recorder||Button")
#time.sleep(0.5)
#click(button='right')
with Window(u"Context||Menu"):
left_click(u"Open output folder||MenuItem")
with Window(u"C:\\\\Users\\\\.*\\\\Pywinauto recorder||Window", regex_title=True):
left_click(u"||TitleBar")
time.sleep(0.5)
send_keys("{LWIN down}""{VK_RIGHT}""{LWIN up}")
time.sleep(0.5)
with Window(u"Snap Assist||Window"):
left_click("*->Description — pywinauto_recorder documentation - Google Chrome||ListItem->Close||Button")
left_click("Dismiss Task Switching Window||Button%(95,-98)")
Expand Down
57 changes: 57 additions & 0 deletions Tutorials/Tutorial_02/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# coding: utf-8
import time
import os
import shutil
from dp_toolbox import *
from pywinauto_recorder.player import *
from pathlib import Path


if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))

brian = Voice(name='Brian')
obs = OBSStudio()

#####################################################################
# Copy script in Data to 'Pywinauto Recorder' folder in Home folder #
#####################################################################
shutil.copy(Path("Data")/Path("recorded Mon Apr 5 16_28_28 2021.py"), Path.home()/Path("Pywinauto recorder"))


####################################
# Open 'Pywinauto Recorder' folder #
####################################
brian.say("The generated file is in 'Pywinauto Recorder' folder under your home folder.")
time.sleep(1.5)
with Window(u"||List"):
double_left_click("Home||ListItem")
time.sleep(1.5)
send_keys("{LWIN down}""{VK_RIGHT}""{LWIN up}")
with Window("C:\\Users\\d_pra||Window"):
double_left_click("*->Shell Folder View||Pane->Items View||List->Pywinauto recorder||ListItem->Name||Edit")

#############################################################
# Drag and drop the recorded file on pywinauto_recorder.exe #
#############################################################
with Window("C:\\\\Users\\\\.*\\\\Pywinauto recorder||Window", regex_title=True):
drag_and_drop_start = find("*->Shell Folder View||Pane->Items View||List->||ListItem->Name||Edit#[0,0]")
drag_and_drop_start.draw_outline(colour='blue')
with Window("||List"):
drag_and_drop_end = find(u"Pywinauto recorder||ListItem")
drag_and_drop_end.draw_outline()
#with Window(u"Program Manager||Pane"):
# drag_and_drop_end = find(u"Desktop||List->Pywinauto recorder||ListItem")
# drag_and_drop_end.draw_outline()

time.sleep(1)
drag_and_drop(drag_and_drop_start, drag_and_drop_end)
brian.say("Thanks for watching! In the next tutorial, you will see how to make a robust recorded script. See you soon!", wait_until_the_end_of_the_sentence=True)

obs.stop_recording()
time.sleep(1)
obs.quit()


exit(0)

40 changes: 22 additions & 18 deletions pywinauto_recorder/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ class MoveMode(Enum):
w_rOLD = None


def load_dictionary(filename: str, encoding: str = 'utf8') -> None:
def load_dictionary(filename_key: str, filename_def: str,encoding: str = 'utf8') -> None:
"""
Loads a dictionary
:param filename: filename of the dictionary
:param filename_key: filename of the key file
:param filename_def: filename of the definition file
:param encoding: encoding of the dictionary file
"""
abs_path = [x for x in range(99)]
with open(filename, encoding=encoding) as fp:
for line in fp:
words = line.split("\t")
word = words[0]
i = 0
while words[i] != ':':
i += 1
level = len(words)-2-i
#print(level)
with open(filename_key, encoding=encoding) as fp_key, open(filename_def, encoding=encoding) as fp_def:
for line_key, line_def in zip(fp_key, fp_def):
words = line_key.split("\t")
word = words[-1].translate(str.maketrans('', '', '\n\t\r'))

words = line_def.split("\t")
definition = words[-1].translate(str.maketrans('', '', '\n\t\r'))
level = len(words) - 1
#print(level)
abs_path[level] = definition
abs_definition = abs_path[0]
for i in range(1, level):
abs_definition += path_separator + abs_path[i]
abs_definition += path_separator + abs_path[i]
#print(abs_definition + path_separator + definition)
_dictionary[word] = (abs_definition, definition)

Expand Down Expand Up @@ -339,11 +339,16 @@ def click(
:return: Pywinauto wrapper of clicked element
"""
if element_path:
unique_element = move(element_path, duration=duration, mode=mode, timeout=timeout)
if wait_ready and isinstance(element_path, str):
wait_is_ready_try1(unique_element, timeout=timeout)
if duration == 0:
wrapper = find(element_path)
wrapper.click()
return wrapper
else:
unique_element = None
wrapper = move(element_path, duration=duration, mode=mode, timeout=timeout)
if wait_ready and isinstance(element_path, str):
wait_is_ready_try1(wrapper, timeout=timeout)
else:
wrapper = None
if button == 'left' or button == 'double_left' or button == 'triple_left':
win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0)
time.sleep(.01)
Expand All @@ -364,7 +369,7 @@ def click(
win32api_mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0)
time.sleep(.01)
if element_path:
return unique_element
return wrapper
else:
return None

Expand All @@ -389,7 +394,6 @@ def left_click(
duration = PlayerSettings.mouse_move_duration
return click(element_path, duration=duration, mode=mode, button='left', timeout=timeout, wait_ready=wait_ready)


def right_click(
element_path: UI_Element,
duration: Optional[float] = None,
Expand Down
59 changes: 49 additions & 10 deletions pywinauto_recorder/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,18 +516,32 @@ def get_send_keys_strings(keyboard_events):
return get_typed_keys(keyboard_events)


t0_progress_icon_timings = time.time()
progress_icon_timings = [0, 0, 0, 0, 0, 0, 0]


def overlay_add_progress_icon(main_overlay, i, x, y):
global t0_progress_icon_timings
main_overlay.add(
geometry=oaam.Shape.rectangle, x=x, y=y, width=52, height=52,
color=(0, 0, 0), thickness=1, brush=oaam.Brush.solid, brush_color=(255, 255, 254))
main_overlay.add(
geometry=oaam.Shape.triangle,
xyrgb_array=((x + 1, y + 1, 255, 255, 254), (x + 1, y + 52, 128, 128, 128), (x + 51, y + 52, 255, 255, 254)),
thickness=0)
dt = time.time() - t0_progress_icon_timings
nb_dt = int(dt/0.01)
if nb_dt > 255:
nb_dt = int(255)
progress_icon_timings[i % 6 -1] = nb_dt

for b in range(i % 6):
c = progress_icon_timings[b]
main_overlay.add(
geometry=oaam.Shape.rectangle, x=x + 6, y=y + 6 + b * 8, width=40, height=6,
color=(0, 255, 0), thickness=1, brush=oaam.Brush.solid, brush_color=(0, 200, 0))
# color=(0, 255, 0), thickness=1, brush=oaam.Brush.solid, brush_color=(0, 200, 0))
color=(c, int(255 - c/2), c ), thickness=1, brush=oaam.Brush.solid, brush_color=(c, int(255 - c), 0))
t0_progress_icon_timings = time.time()


def overlay_add_mode_icon(main_overlay, hicon, x, y):
Expand Down Expand Up @@ -730,7 +744,12 @@ def __display_info_tip(self, x, y, wrapper):
text2 = text2 + '\n'
text_width = 0
tooltip2_height = tooltip2_height + 16
dx, dy = (x - r.left) / r.width(), (y - r.top) / r.height()
if r.width() == 0:
dx = 0
if r.height() == 0:
dy = 0
else:
dx, dy = (x - r.left) / r.width(), (y - r.top) / r.height()
info_left = dx * (self.screen_width - tooltip_width)
info_top = dy * (self.screen_height - (tooltip_height+tooltip2_height))
if x>self.screen_width /2:
Expand Down Expand Up @@ -772,11 +791,13 @@ def __display_info_tip(self, x, y, wrapper):
except:
has_get_value = False
pass
if wrapper.legacy_properties()['Value']:
if not has_get_value or (has_get_value and wrapper.get_value() != wrapper.legacy_properties()['Value']):
if text:
text = text + "\n"
text = text + " wrapper.legacy_properties()['Value']: " + wrapper.legacy_properties()['Value']
has_legacy_properties = getattr(wrapper, "legacy_properties", None)
if callable(has_legacy_properties):
if wrapper.legacy_properties()['Value']:
if not has_get_value or (has_get_value and wrapper.get_value() != wrapper.legacy_properties()['Value']):
if text:
text = text + "\n"
text = text + " wrapper.legacy_properties()['Value']: " + wrapper.legacy_properties()['Value']
str_name, str_type, _, _ = get_entry(end_path.split(path_separator)[-1])
try:
if str_type in ["Button", "CheckBox", "RadioButton", "GroupBox"]:
Expand Down Expand Up @@ -862,6 +883,7 @@ def run(self):
strategies = [Strategy.unique_path, Strategy.array_2D, Strategy.array_1D]
i_strategy = 0
while self.mode != "Quit":
i = i + 1
try:
self._loop_t0 = time.time()
self.main_overlay.clear_all()
Expand All @@ -888,10 +910,16 @@ def run(self):
unique_wrapper_path = None
# *** ----> this block of code must start a new while iteration if mouse cursor is outside wrapper rectangle
# => add tests to leave if mouse cursor is outside wrapper rectangle
wrapper_rectangle = wrapper.rectangle()
if strategy == Strategy.unique_path:
x_new, y_new = win32api.GetCursorPos()
if not ((wrapper_rectangle.left < x_new < wrapper_rectangle.right) and (
wrapper_rectangle.top < y_new < wrapper_rectangle.bottom)):
i_strategy = 0
continue
if unique_candidate is not None:
unique_wrapper_path = get_wrapper_path(unique_candidate)
r = wrapper.rectangle()
r = wrapper_rectangle
self.main_overlay.add(
geometry=oaam.Shape.rectangle, x=r.left, y=r.top, width=r.width(), height=r.height(),
thickness=1, color=(0, 128, 0), brush=oaam.Brush.solid, brush_color=(0, 255, 0))
Expand All @@ -902,12 +930,22 @@ def run(self):
geometry=oaam.Shape.rectangle, x=r.left, y=r.top, width=r.width(), height=r.height(),
thickness=1, color=(0, 128, 0), brush=oaam.Brush.solid, brush_color=(255, 0, 0))
if strategy == Strategy.array_1D and elements:
x_new, y_new = win32api.GetCursorPos()
if not ((wrapper_rectangle.left < x_new < wrapper_rectangle.right) and (
wrapper_rectangle.top < y_new < wrapper_rectangle.bottom)):
i_strategy = 0
continue
unique_array_1d = self.__find_unique_element_array_1d(wrapper.rectangle(), elements)
if unique_array_1d is not None:
unique_wrapper_path = wrapper_path + unique_array_1d
else:
strategy = Strategy.array_2D
if strategy == Strategy.array_2D and elements:
x_new, y_new = win32api.GetCursorPos()
if not ((wrapper_rectangle.left < x_new < wrapper_rectangle.right) and (
wrapper_rectangle.top < y_new < wrapper_rectangle.bottom)):
i_strategy = 0
continue
unique_array_2d = self.__find_unique_element_array_2d(wrapper.rectangle(), elements)
if unique_array_2d is not None:
unique_wrapper_path = wrapper_path + unique_array_2d
Expand Down Expand Up @@ -950,7 +988,7 @@ def run(self):
overlay_add_mode_icon(self.main_overlay, IconSet.hicon_clipboard, 10 + 60 * nb_icons, 10)
nb_icons = nb_icons + 1
self._copy_count = self._copy_count - 1
i = i + 1

self.main_overlay.refresh()
if self.mode == "Info":
self.__display_info_tip(x, y, wrapper)
Expand All @@ -965,7 +1003,8 @@ def run(self):
self.stop_recording()
mouse.unhook_all()
keyboard.unhook_all()
#self.quit()
self.main_overlay.quit()
self.info_overlay.quit()
print("Run end")

@property
Expand Down
18 changes: 18 additions & 0 deletions tests/Calculator.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Calculator||Window->Calculator||Window->||Group
Number pad||Group
Zero||Button
One||Button
Two||Button
Three||Button
Four||Button
Five||Button
Six||Button
Seven||Button
Eight||Button
Nine||Button
Standard operators
Divide by||Button
Multiply by||Button
Minus||Button
Plus||Button
Equals||Button
18 changes: 0 additions & 18 deletions tests/Calculator.dic

This file was deleted.

0 comments on commit 722a9dd

Please sign in to comment.