194 lines
5.8 KiB
Python
Raw Permalink Normal View History

import logging
import time
import typing
import allure
import configs
import driver
from driver import isFrozen
from scripts.tools.image import Image
LOG = logging.getLogger(__name__)
class QObject:
def __init__(self, real_name: [str, dict] = None):
self.real_name = real_name
self._image = Image(self.real_name)
@property
@allure.step('Get object {0}')
def object(self):
2024-10-24 18:45:38 +03:00
try:
obj = driver.waitForObject(self.real_name, configs.timeouts.UI_LOAD_TIMEOUT_MSEC)
except LookupError:
raise LookupError(f"Object {self.real_name} was not found within {configs.timeouts.UI_LOAD_TIMEOUT_MSEC}")
return obj
2024-09-12 17:28:43 +03:00
def set_text_property(self, text):
self.object.forceActiveFocus()
self.object.clear()
self.object.text = text
assert self.object.text == text, 'Text was not set'
@property
@allure.step('Get object exists {0}')
def exists(self) -> bool:
return driver.object.exists(self.real_name)
@property
@allure.step('Get bounds {0}')
def bounds(self):
return driver.object.globalBounds(self.object)
@property
@allure.step('Get "x" coordinate {0}')
def x(self) -> int:
return self.bounds.x
@property
@allure.step('Get "y" coordinate {0}')
def y(self) -> int:
return self.bounds.y
@property
@allure.step('Get width {0}')
def width(self) -> int:
return int(self.bounds.width)
@property
@allure.step('Get height {0}')
def height(self) -> int:
return int(self.bounds.height)
@property
@allure.step('Get central coordinate {0}')
def center(self):
return self.bounds.center()
@property
@allure.step('Get enabled {0}')
def is_enabled(self) -> bool:
return getattr(self.object, 'enabled')
@property
@allure.step('Get selected {0}')
def is_selected(self) -> bool:
return getattr(self.object, 'selected')
@property
@allure.step('Get checked {0}')
def is_checked(self) -> bool:
return getattr(self.object, 'checked')
2024-09-12 17:28:43 +03:00
@property
@allure.step('Get checkState {0}')
def checkState(self) -> int:
if hasattr(self.object, 'checkState'):
return getattr(self.object, 'checkState')
return 2 if self.is_checked else 0
@property
@allure.step('Get visible {0}')
def is_visible(self) -> bool:
try:
2023-10-02 11:21:12 +02:00
return driver.waitForObject(self.real_name, 0).visible
except (AttributeError, LookupError, RuntimeError):
return False
@property
@allure.step('Get image {0}')
def image(self):
self._image.update_view()
return self._image
@allure.step('Click {0}')
def click(
self,
x: int = None,
y: int = None,
button=None,
2024-07-02 14:24:03 +07:00
timeout=1
):
driver.mouseClick(
self.object,
x or int(self.object.width * 0.5),
y or int(self.object.height * 0.5),
button or driver.Qt.LeftButton
)
2024-03-13 11:44:28 +03:00
LOG.info('%s: is clicked with Qt.LeftButton', self)
2024-10-09 18:39:50 +03:00
# LOG.info("Checking if application context is frozen")
2024-07-02 14:24:03 +07:00
if timeout is None:
pass
2024-07-02 14:24:03 +07:00
if timeout is not None:
2024-10-09 18:39:50 +03:00
pass
# TODO: enable after fixing https://github.com/status-im/status-desktop/issues/15345
# if not isFrozen(timeout):
# pass
2024-07-02 14:24:03 +07:00
2024-10-09 18:39:50 +03:00
# else:
# LOG.info("Application context did not respond after click")
# raise Exception(f'Application UI is not responding within {timeout} second(s)')
2023-12-05 20:23:02 +07:00
@allure.step('Native click {0}')
2024-03-13 11:44:28 +03:00
def native_mouse_click(
2023-12-05 20:23:02 +07:00
self,
x: typing.Union[int, driver.UiTypes.ScreenPoint] = None,
y: typing.Union[int, driver.UiTypes.ScreenPoint] = None,
button: driver.MouseButton = None
):
driver.nativeMouseClick(
2024-03-13 11:44:28 +03:00
x or int(self.bounds.x + self.width // 2),
y or int(self.bounds.y + self.height // 2),
2023-12-05 20:23:02 +07:00
button or driver.MouseButton.LeftButton
)
LOG.info(f'{self}: native clicked')
@allure.step('Hover {0}')
def hover(self, timeout_msec: int = configs.timeouts.UI_LOAD_TIMEOUT_MSEC):
def _hover():
try:
driver.mouseMove(self.object)
LOG.info('%s: mouse hovered', self)
return getattr(self.object, 'hovered', True)
except RuntimeError as err:
LOG.error(err)
time.sleep(1)
return False
assert driver.waitFor(lambda: _hover(), timeout_msec)
return self
2024-03-13 11:44:28 +03:00
@allure.step('Right click on {0}')
def right_click(
self,
x: typing.Union[int, driver.UiTypes.ScreenPoint] = None,
y: typing.Union[int, driver.UiTypes.ScreenPoint] = None,
):
self.click(
2024-03-13 11:44:28 +03:00
x or int(self.width // 2),
y or int(self.height // 2),
driver.Qt.RightButton
)
2024-03-13 11:44:28 +03:00
LOG.info('%s: right clicked with Qt.RightButton', self)
@allure.step('Wait until appears {0}')
def wait_until_appears(self, timeout_msec: int = configs.timeouts.UI_LOAD_TIMEOUT_MSEC):
assert driver.waitFor(lambda: self.is_visible, timeout_msec), f'Object {self} is not visible'
LOG.info('%s: is visible', self)
return self
@allure.step('Wait until hidden {0}')
def wait_until_hidden(self, timeout_msec: int = configs.timeouts.UI_LOAD_TIMEOUT_MSEC):
assert driver.waitFor(lambda: not self.is_visible, timeout_msec), f'Object {self} is not hidden'
LOG.info('%s: is hidden', self)
@classmethod
def wait_for(cls, condition, timeout_msec: int = configs.timeouts.UI_LOAD_TIMEOUT_MSEC) -> bool:
return driver.waitFor(lambda: condition, timeout_msec)