107 lines
4.6 KiB
Python
107 lines
4.6 KiB
Python
from playwright.sync_api import Page
|
|
from data.environment import host
|
|
from playwright.sync_api import expect
|
|
from pages.base_page import BasePage
|
|
from Locators.base_page import BasePageLocators
|
|
|
|
import jsondiff
|
|
|
|
class Assertions(BasePage):
|
|
def __init__(self, page: Page) -> None:
|
|
super().__init__(page)
|
|
|
|
def check_URL(self, uri, msg):
|
|
expect(self.page).to_have_url(f"{host.get_base_url()}{uri}", timeout=60000), msg
|
|
|
|
def have_text(self, locator, text: str, msg): #элемент имеет текст
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_have_text(text), msg
|
|
|
|
def have_title(self, locator, title: str, msg):
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_have_text(title), msg
|
|
|
|
def check_presence(self, locator, msg):
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_be_visible(visible=True, timeout=12000), msg
|
|
|
|
def check_button_presence_with_text(self, text, msg):
|
|
loc = self.page.get_by_role("button", name=text)
|
|
expect(loc).to_be_visible(visible=True, timeout=12000), msg
|
|
|
|
def check_absence(self, locator, msg):
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_be_hidden(timeout=700), msg
|
|
|
|
def check_absence_after_period(self, locator, period, msg):
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_be_hidden(timeout=period), msg
|
|
|
|
def check_empty_input_area(self, input_area, msg):
|
|
expect(input_area).to_be_empty(), msg
|
|
|
|
def check_menu_item_with_text(self, text, msg):
|
|
count = self.page.get_by_role("listitem", name=text).count()
|
|
assert count != 1, msg
|
|
|
|
def check_equals(self, actual, expected, msg):
|
|
assert actual == expected, msg
|
|
|
|
def check_not_equals(self, actual, expected, msg):
|
|
assert actual != expected, msg
|
|
|
|
def check_json_equals(self, actual, expected, msg):
|
|
diff = jsondiff.diff(expected, actual, syntax='symmetric')
|
|
assert len(diff) == 0, f"{msg}. DIFF is {diff}"
|
|
|
|
def check_lists_equals(self, actual, expected, msg):
|
|
def compare_lists(list1, list2):
|
|
if len(list1) != len(list2):
|
|
return False
|
|
for item1, item2 in zip(list1, list2):
|
|
if isinstance(item1, list) and isinstance(item2, list):
|
|
if not compare_lists(item1, item2):
|
|
return False
|
|
elif item1 != item2:
|
|
return False
|
|
return True
|
|
|
|
assert compare_lists(actual, expected), msg
|
|
|
|
def check_url_content(self, uri,msg):
|
|
assert f"{uri}" in self.page.url, msg
|
|
|
|
def check_element_active(self, locator, msg):
|
|
element_handle = self.page.wait_for_selector(locator)
|
|
class_names = element_handle.get_attribute('class')
|
|
assert 'active' in class_names, msg
|
|
|
|
def check_alert_window_with_text(self, alert_type, text):
|
|
self.check_presence(BasePageLocators.ALERT_WINDOW, "No alert window for action notification")
|
|
|
|
if alert_type == "error":
|
|
self.have_text(BasePageLocators.ALERT_WINDOW_TEXT_ERROR, text, \
|
|
"Unexpected error message in alert window")
|
|
elif alert_type == "success":
|
|
self.have_text(BasePageLocators.ALERT_WINDOW_TEXT_SUCCESS, text, \
|
|
"Unexpected message about success action in alert window")
|
|
elif alert_type == "info":
|
|
self.have_text(BasePageLocators.ALERT_WINDOW_TEXT_WARNING, text, \
|
|
"Unexpected info message in alert window")
|
|
elif alert_type == "warning":
|
|
self.have_text(BasePageLocators.ALERT_WINDOW_TEXT_WARNING, text, \
|
|
"Unexpected warning message about success action in alert window")
|
|
else:
|
|
assert False, "Unsupported type of alert window"
|
|
self.check_absence_after_period(BasePageLocators.ALERT_WINDOW, 30000, \
|
|
"Alert window for action notification should disappear")
|
|
|
|
def check_tooltip_with_text(self, locator, text):
|
|
self.page.locator(locator).hover()
|
|
tooltip = self.page.locator(BasePageLocators.TOOLTIP)
|
|
assert tooltip.text_content().strip() == text, "Unexpected tooltip text"
|
|
|
|
def check_confirmation_dialog_with_title(self, locator, title):
|
|
loc = self.page.locator(locator)
|
|
expect(loc).to_contain_text(title), f"Confirmation dialog window with title {title} is not present"
|
|
|