class PageObjectManager: log = ConsoleLogger.get_logger("BaseClass") welcome_page_obj = None reg_page_obj = None signon_page_obj = None @classmethod def get_welcome_page_obj(cls): cls.log.info('executing method :: get_welcome_page_obj') if cls.welcome_page_obj is None: cls.log.info("obj is null -- so creating the welcome page obj") cls.welcome_page_obj = WelcomePageActions() return cls.welcome_page_obj @classmethod def get_reg_page_obj(cls): cls.log.info('executing method :: get_reg_page_obj') if cls.reg_page_obj is None: cls.log.info("obj is null -so creating reg page obj") cls.reg_page_obj = RegistrationPageActions() return cls.reg_page_obj @classmethod def get_signon_page_obj(cls): cls.log.info('executing method :: get_signon_page_obj') if cls.signon_page_obj is None: cls.log.info("obj is null -so creating reg page obj") cls.signon_page_obj = SignOnPageActions() return cls.signon_page_obj
class BaseClass: """ This base page class contains common webdriver methods """ log = ConsoleLogger.get_logger("BaseClass") welcome_page_obj = None @pytest.fixture(scope="class") def pom_init(self): self.log.info('initializing the welcome page obj') if BaseClass.welcome_page_obj is None: BaseClass.welcome_page_obj = PageObjectManager.get_welcome_page_obj() yield BaseClass.welcome_page_obj @pytest.fixture(scope="function") def get_driver(self): self.log.info('Initializing the browser') if BROWSER_TYPE == "chrome": self.log.info('initializing the chrome browser--') driver = webdriver.Chrome(ChromeDriverManager().install()) elif BROWSER_TYPE == "firefox": self.log.info('initializing the ff browser') driver = webdriver.Chrome(executable_path=GeckoDriverManager().install()) else: self.log.info('The selected browser type is not supported, choose the correct one') raise ValueError("The selected browser type is not supported, choose the correct one") driver.maximize_window() driver.delete_all_cookies() driver.implicitly_wait(15) self.log.info('launching the application url') driver.get(APP_URL) yield driver driver.quit()
class WelcomePageActions(WebDriverActions): log = ConsoleLogger.get_logger("WelcomePageActions") @classmethod def welcome_page_title(cls, driver): return cls.get_title(driver) @classmethod def sign_in_action(cls, driver, uid, pwd): cls.log.info("sign in action") try: cls.enter_text(driver, WelcomePageObjects.USER_NAME_FIELD, uid) cls.enter_text(driver, WelcomePageObjects.PASSWORD_FIELD, pwd) cls.click_element(driver, WelcomePageObjects.SIGN_IN_BTN) except Exception as e: cls.log.info("exception in sign in action") print("Exception in sign_in_action--" + str(e)) exit(-1) @classmethod def navigate_to_reg_page(cls, driver): cls.log.info("navigating to the reg page :: navigate_to_reg_page") try: cls.click_element(driver, WelcomePageObjects.REGISTER_LINK) except Exception as e: cls.log.error("Exception in navigate_to_reg_page --" + str(e)) exit(-1)
class TestsHomePage(BaseClass): """ Home Page Tests""" log = ConsoleLogger.get_logger("TestsHomePage") @pytest.fixture(autouse=True) def set_up(self, get_driver): self.driver = get_driver @allure.description("Validating valid login") def test_valid_login(self): self.log.info("executing test script :: test_valid_login ") self.welcome_page_obj.sign_in_action(self.driver, 'admin', 'admin') signon_page_obj = PageObjectManager.get_signon_page_obj() signon_page_obj.verify_signon_page(self.driver) @allure.description("Validating invalid login") def test_invalid_login(self): self.log.info("executing test script :: test_invalid_login") self.welcome_page_obj.sign_in_action(self.driver, 'invalid_admin', 'invalid_admin') @allure.description("Validating some random test") def test_not_valid_login(self, get_driver): self.log.info("test_not_valid_login") print(self.welcome_page_obj.welcome_page_title(self.driver))
class RegistrationPageActions: log = ConsoleLogger.get_logger("RegistrationPageActions") @classmethod def reg_page(cls): cls.log.info("on reg page")
class SignOnPageActions(WebDriverActions): """ This page contains action methods of SignOnPage""" log = ConsoleLogger.get_logger("SignOnPageActions") @classmethod def verify_signon_page(cls, driver): cls.log.info('verifying SIGN-ON page') return cls.is_element_displayed(driver, SignOnPageLocators.SIGN_ON_LINK)
class WebDriverActions: """ This method contains webdriver common actions """ log = ConsoleLogger.get_logger("WebDriverActions") @classmethod def get_title(cls, driver): try: return driver.title except Exception as e: cls.log.error("returning title failed due to - " + str(e)) exit(-1) @classmethod def get_current_url(cls, driver): try: return driver.current_url except Exception as e: cls.log.error("returning current url is failed due to - " + str(e)) exit(-1) @classmethod def click_element(cls, driver, locator): try: driver.find_element(*locator).click() except Exception as e: cls.log.error("native click failed due to - " + str(e)) exit(-1) @classmethod def click_using_js(cls, driver, locator): try: element = driver.find_element(*locator) driver.execute_script('arguments[0].click();', element) except Exception as e: cls.log.error("JavaScript click failed due to - " + str(e)) exit(-1) @classmethod def enter_text(cls, driver, locator, text): try: element = driver.find_element(*locator) element.clear() element.send_keys(text + Keys.TAB) except Exception as e: cls.log.error("entering text failed due to - " + str(e)) exit(-1) @classmethod def is_element_exist(cls, driver, locator): try: element = driver.find_element(*locator) if element.size != 0: return True else: return False except Exception as e: cls.log.error("checking is_element exist is failed - " + str(e)) exit(-1) @classmethod def is_element_displayed(cls, driver, locator): try: element = driver.find_element(*locator) if element.is_displayed(): return True else: return False except Exception as e: cls.log.error("checking is_element_displayed is failed - " + str(e)) exit(-1)