import re from allure import step from playwright.sync_api import Page from playwright.sync_api import expect from src.helpers.logger import Logger timeout_sec = 35000 class BaseUIElement: log = Logger() def __init__(self, page: Page, locator, name): self.page = page self.locator = locator self.name = name @property def _element(self): self.log.info(f"Getting '{self.name}' element by {self.locator}") element = self.page.locator(self.locator) return element def exists(self): self.log.info(f"Waiting for 1 element '{self.name}'") expect(self._element).to_have_count(1, timeout=timeout_sec) def not_exists(self): self.log.info(f"Waiting for 1 element '{self.name}'") expect(self._element).to_have_count(0, timeout=timeout_sec) def exists_multiple(self): self.log.info(f"Waiting for multiple element '{self.name}'") expect(self._element).not_to_have_count(0, timeout=timeout_sec) expect(self._element).not_to_have_count(1, timeout=timeout_sec) def not_exist(self): self.log.info(f"Waiting for 0 element '{self.name}'") expect(self._element).to_have_count(0, timeout=timeout_sec) def get_by(self, *args): self.log.info(f"Getting '{self.name}' element by parameters") return BaseUIElement(self.page, self.locator.format(*args), self.name) @step("Clicking {0}") def click(self, mouse_button=None, ): self.log.info(f"Clicking an element {self.locator}") self._element.click(button=mouse_button) @step("Input text {text} into {0}") def input_text(self, text): self.log.info(f"Filling with text='{text}' the {self.locator}") self._element.fill(text) @step("Is {0} enabled - {enabled}") def is_enabled(self, enabled): if enabled is True: expect(self._element).not_to_have_class(re.compile(r"disabled")) elif enabled is False: expect(self._element).to_have_class(re.compile(r"disabled")) @step("Is button {0} enabled - {enabled}") def is_button_enabled(self, enabled): if enabled is True: expect(self._element).not_to_have_attribute("disabled", '') elif enabled is False: expect(self._element).to_have_attribute("disabled", '') @step("Is {0} selected - {selected}") def is_selected(self, selected=True): if selected: expect(self._element).to_have_attribute(name="data-selected", value="selected", timeout=timeout_sec) else: expect(self._element).to_have_attribute("data-selected", "not-selected") @step("Is {0} checked - {checked}") def is_checked(self, checked=True): if checked: expect(self._element).to_have_attribute("data-checked", "checked") else: expect(self._element).to_have_attribute("data-checked", "not-checked") @step("{0} is active") def is_active(self): expect(self._element).to_have_class(re.compile(r"active")) @step("Is {0} hidden - {state}") def is_hidden(self, state): expect(self._element).to_have_attribute("aria-hidden", str(state).lower()) @step("{0} has NO text {text}") def not_contain_text(self, text): expect(self._element).not_to_contain_text(text) @step("{0} has text {text}") def contain_text(self, text): print(self._element.inner_text()) try: expect(self._element).to_contain_text(text) except Exception: raise Exception(f'Actual text: {self._element.inner_text()}') @step("Retrieve text from the element") def get_text(self): self.log.info(f"Return text content of the element {self.locator}") self._element.wait_for() self._element.all_inner_texts() return self._element.text_content().replace(u'\xa0', ' ').strip() @step("Retrieve texts from the element lists") def get_texts_from_list(self): self.log.info(f"Return text content of the element {self.locator}") return self._element.all_inner_texts() @step("Retrieve attribute value from the element") def get_attribute(self, attribute): self.log.info(f"Return attribute '{attribute}' of the element {self.locator}") return self._element.get_attribute(attribute) @step("Count number of elements") def get_count(self): self.log.info(f"Return count of appeared element {self.locator}") return self._element.count() def __repr__(self): return self.name