import logging from contextlib import suppress import allure from ui_automation_framework.utils import CoreConfig from ui_automation_framework.utils import logger as cl from waiting import ALL, TimeoutExpired from app.src.helpers.waiting_wrapper import wait from app.src.pages.navigation_page import Navigation from app.src.pages.on_boarding_page import OnBoardingPage from app.src.pages.search_page import SearchPage from app.src.ui_elements.button_ui_element import ButtonUIElement from app.src.ui_elements.input_ui_element import InputUIElement from app.src.ui_elements.label_ui_element import LabelUIElement class LoginPage(Navigation): log = cl.Logger(logging.DEBUG) def __init__(self, driver, api_client): super().__init__(driver) self.api = api_client self.driver = driver self.login_locators = self.get_page_locators("LoginPage", "login_elements.json") self.search_locators = self.get_page_locators("SearchPage", "search_elements.json") self.onBoardingPage = OnBoardingPage(self.driver, self.api) @property def lb_logo(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "logo"), "logo") @property def bn_sign_in(self): return ButtonUIElement(self.driver, self.locator(self.login_locators, "sign_in"), "Sign in") @property def in_email(self): return InputUIElement(self.driver, self.locator(self.login_locators, "email_field"), "Email") @property def in_password(self): return InputUIElement(self.driver, self.locator(self.login_locators, "password_field"), "password") @property def bn_login(self): return ButtonUIElement(self.driver, self.locator(self.login_locators, "login_btn"), "Login") @property def bn_next(self): return ButtonUIElement(self.driver, self.locator(self.login_locators, "next_btn"), "Next") @property def bn_finish(self): return ButtonUIElement(self.driver, self.locator(self.login_locators, "finish_btn"), "Finish") @property def lb_on_boarding_flow(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "on_boarding_flow"), "on boarding flow") @property def bn_forgot_password(self): return ButtonUIElement(self.driver, self.locator(self.login_locators, "forgot_password"), "Forgot password") @property def lb_search_field_clear(self): return LabelUIElement(self.driver, self.locator(self.search_locators, "search_field_clear"), "Search") @property def lb_wrong_creds(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "wrong_creds"), "Label wrong credentials") @property def lb_email_error(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "email_error"), "Email error placeholder") @property def lb_email_blank_error(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "email_blank_error"), "Blank error popup") @property def lb_password_blank_error(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "password_blank_error"), "Blank password error") @property def lb_forgot_password(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "forgot_password_msg"), "Forgot password") @property def lb_send_password(self): return LabelUIElement(self.driver, self.locator(self.login_locators, "send_password"), "Send password") @allure.step("verifying successful login with valid credentials") def login(self, email, password): self.lb_logo.wait(to_assert=False) self.bn_sign_in.click() self.in_email.wait(to_assert=False) self.in_email.send_text(email) self.in_password.click() self.in_password.send_text(password) self.bn_login.click() # Turn on this block in carousel is exists in upcoming release # if self.bn_next.is_visible() or self.bn_finish.is_visible(): # self.skip_carousel() # elif self.lb_on_boarding_flow.is_visible(): # self.onBoardingPage.skip_on_boarding() # else: # self.log.info("No app additional login flows triggered") return self def login_without_on_boarding_handler(self, email, password): self.lb_logo.wait(to_assert=False) self.bn_sign_in.click() self.in_email.click() self.in_email.send_text(email) self.in_password.click() self.in_password.send_text(password) self.bn_login.click() return self def skip_carousel(self): self.bn_next.wait(to_assert=False, timeout=3) if self.bn_next.is_visible(): while not self.bn_finish.is_present(): self.bn_next.click() self.bn_finish.click() return self @allure.step("Navigating to the login form") def go_to_login_form(self): self.lb_logo.wait(to_assert=False) self.bn_sign_in.click() self.in_email.wait() return self @allure.step("Typing correct user email and incorrect password") def enter_login_credentials(self, email, password): self.in_email.click() self.in_email.clear_text() # hack for not fully cleared email field due to @ char self.in_email.clear_text() self.in_email.send_text(email) self.in_password.click() self.in_password.clear_text() self.in_password.send_text(password) return self @allure.step("Clicking login button") def click_login(self): self.bn_login.click() return self @allure.step("Clicking forgot password button") def click_forgot_password(self): self.bn_forgot_password.click() return self def is_logged_in(self): with suppress(TimeoutExpired): return wait(self.lb_search_field_clear.is_visible) return False def is_wrong_credentials_error_message_shown(self): with suppress(TimeoutExpired): return wait(self.lb_wrong_creds.is_visible) return False def is_invalid_email_error_message_shown(self): with suppress(TimeoutExpired): return wait(self.lb_email_error.is_visible) return False def is_invalid_email_error_message_hidden(self): return not self.lb_email_error.is_visible() def are_empty_credentials_error_messages_shown(self): with suppress(TimeoutExpired): return wait(ALL([self.lb_email_blank_error.is_visible, self.lb_password_blank_error.is_visible])) return False def are_forgot_password_messages_shown(self): with suppress(TimeoutExpired): return wait(ALL([self.lb_forgot_password.is_visible, self.lb_send_password.is_visible])) return False def login_as_regular_user(self): login = CoreConfig.USER_EMAIL password = CoreConfig.USER_PASSWORD self.login(login, password) assert self.is_logged_in(), "user is logged in" return SearchPage(self.driver, self.api) def login_as_notifications_user(self): login = CoreConfig.NOTIFICATIONS_EMAIL password = CoreConfig.NOTIFICATIONS_PASSWORD self.login(login, password) assert self.is_logged_in(), "user is logged in" return SearchPage(self.driver, self.api) def login_as_mock_user(self, user_number: int): email = \ {1: CoreConfig.MOCK_USER_EMAIL_1, 2: CoreConfig.MOCK_USER_EMAIL_2, 3: CoreConfig.MOCK_USER_EMAIL_3, 4: CoreConfig.MOCK_USER_EMAIL_4, 5: CoreConfig.MOCK_USER_EMAIL_5} login = email[user_number] password = CoreConfig.USER_PASSWORD self.login(login, password) assert self.is_logged_in(), "user is logged in" return SearchPage(self.driver, self.api)