-
Notifications
You must be signed in to change notification settings - Fork 23
ENH: Added methods to support pywinauto features #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hhslepicka
merged 7 commits into
botcity-dev:main
from
joao-voltarelli:enh_xpath_desktop
Oct 10, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b13dfb
ENH: Added methods to support pywinauto features
joao-voltarelli 2bfe9eb
Fix flake8 issues
joao-voltarelli 083b5cf
Refactoring to include 'application' package.
joao-voltarelli d14b278
Testing pywinauto import handling
joao-voltarelli 077357c
Adjustment of return type and methods signature
joao-voltarelli fae9fdd
Fix methods return type
joao-voltarelli b2f068e
Added property for app
joao-voltarelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import time | ||
| from pywinauto.timings import TimeoutError | ||
| from pywinauto.findwindows import ElementNotFoundError | ||
| from pywinauto.application import Application, WindowSpecification | ||
| from .utils import Backend | ||
| from .. import config | ||
|
|
||
|
|
||
| def connect(backend=Backend.WIN_32, timeout=60000, **connection_selectors) -> Application: | ||
| """ | ||
| Connects to an instance of an open application. | ||
| Use this method to be able to access application windows and elements. | ||
|
|
||
| Args: | ||
| backend (Backend, optional): The accessibility technology defined in the Backend class | ||
| that could be used for your application. Defaults to Backend.WIN_32 ('win32'). | ||
| timeout (int, optional): Maximum wait time (ms) to wait for connection. | ||
| Defaults to 60000ms (60s). | ||
| **connection_selectors: Attributes that can be used to connect to an application. | ||
| [See more details about the available selectors](https://documentation.botcity.dev). | ||
|
|
||
| Returns | ||
| app (Application): The Application instance. | ||
| """ | ||
| connect_exception = None | ||
| start_time = time.time() | ||
| while True: | ||
| elapsed_time = (time.time() - start_time) * 1000 | ||
| if elapsed_time > timeout: | ||
| if connect_exception: | ||
| raise connect_exception | ||
| return None | ||
| try: | ||
| app = Application(backend=backend).connect(**connection_selectors) | ||
| return app | ||
| except Exception as e: | ||
| connect_exception = e | ||
| time.sleep(config.DEFAULT_SLEEP_AFTER_ACTION/1000.0) | ||
|
|
||
|
|
||
| def find_window(app: Application, waiting_time=10000, **selectors) -> WindowSpecification: | ||
| """ | ||
| Find a window of the currently connected application using the available selectors. | ||
|
|
||
| Args: | ||
| app (Application): The connected application. | ||
| waiting_time (int, optional): Maximum wait time (ms) to search for a hit. | ||
| Defaults to 10000ms (10s). | ||
| **selectors: Attributes that can be used to filter an element. | ||
| [See more details about the available selectors](https://documentation.botcity.dev). | ||
|
|
||
| Returns | ||
| dialog (WindowSpecification): The window or control found. | ||
| """ | ||
| try: | ||
| dialog = app.window(**selectors) | ||
| dialog.wait(wait_for='exists ready', timeout=waiting_time / 1000.0) | ||
| return dialog | ||
| except (TimeoutError, ElementNotFoundError): | ||
| return None | ||
|
|
||
|
|
||
| def find_element(app: Application, from_parent_window: WindowSpecification = None, | ||
| waiting_time=10000, **selectors) -> WindowSpecification: | ||
| """ | ||
| Find a element of the currently connected application using the available selectors. | ||
| You can pass the context window where the element is contained. | ||
|
|
||
| Args: | ||
| app (Application): The connected application. | ||
| from_parent_window (WindowSpecification, optional): The element's parent window. | ||
| waiting_time (int, optional): Maximum wait time (ms) to search for a hit. | ||
| Defaults to 10000ms (10s). | ||
| **selectors: Attributes that can be used to filter an element. | ||
| [See more details about the available selectors](https://documentation.botcity.dev). | ||
|
|
||
| Returns | ||
| element (WindowSpecification): The element/control found. | ||
| """ | ||
| try: | ||
| if not from_parent_window: | ||
| element = find_window(app, waiting_time, **selectors) | ||
| else: | ||
| element = from_parent_window.child_window(**selectors) | ||
| element.wait(wait_for='exists ready', timeout=waiting_time / 1000.0) | ||
| return element | ||
| except (TimeoutError, ElementNotFoundError): | ||
| return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import enum | ||
| import platform | ||
| from functools import wraps | ||
|
|
||
|
|
||
| class Backend(str, enum.Enum): | ||
| """ | ||
| Supported accessibility technologies. | ||
|
|
||
| Attributes: | ||
| WIN_32 (str): 'win32' backend | ||
| UIA (str): 'uia' backend | ||
| """ | ||
| WIN_32 = "win32" | ||
| UIA = "uia" | ||
|
|
||
|
|
||
| def if_windows_os(func): | ||
| """ | ||
| Decorator which raises if the OS is not Windows. | ||
|
|
||
| Args: | ||
| func (callable): The function to be wrapped | ||
|
|
||
| Returns: | ||
| wrapper (callable): The decorated function | ||
| """ | ||
| @wraps(func) | ||
| def wrapper(self, *args, **kwargs): | ||
| if platform.system() == "Windows": | ||
| return func(self, *args, **kwargs) | ||
| raise ValueError( | ||
| f'You can connect to an application on Windows OS only. Cannot invoke {func.__name__}.' | ||
| ) | ||
| return wrapper | ||
|
|
||
|
|
||
| def if_app_connected(func): | ||
| """ | ||
| Decorator which raises if no apps connected. | ||
|
|
||
| Args: | ||
| func (callable): The function to be wrapped | ||
|
|
||
| Returns: | ||
| wrapper (callable): The decorated function | ||
| """ | ||
| @wraps(func) | ||
| def wrapper(self, *args, **kwargs): | ||
| if self.app is None: | ||
| raise ValueError('No applications connected. Invoke connect_to_app first.') | ||
| return func(self, *args, **kwargs) | ||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ pyscreeze==0.1.27 | |
| pyperclip | ||
| opencv-python | ||
| python-xlib | ||
| pywinauto | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.