Python Forum
Selenium Page Object Model with Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium Page Object Model with Python
#3
test_sign_in_page.py
from tests.base_test import BaseTest
from pages.main_page import MainPage


class TestSignInPage(BaseTest):


    def test_sign_in_with_in_valid_user(self):
        main_page = MainPage(self.driver)
        login_page = main_page.login_click()
        result = login_page.login()
main_page.py
from pages.base_page import BasePage
from pages.login_page import LoginPage
from utils import users
from utils.locators import *


class MainPage(BasePage):
    def __init__(self, driver):
        print("user_page __init__")
        self.locator = UserPageLocators
        self.user = users.get_user("google")
        super().__init__(driver)  # Python3 version

    def check_page_loaded(self):
        return True if self.find_element(*self.locator.LOGO) else False

    def click_sign_in_button(self):
        self.find_element(*self.locator.LOGIN).click()

    def click_facebook_login_button(self):
        self.find_element(*self.locator.FACEBOOK_LOGIN).click()

    def click_google_login_button(self):
        self.find_element(*self.locator.GOOGLE_LOGIN).click()

    def click_apple_login_button(self):
        self.find_element(*self.locator.APPPLE_LOGIN).click()

    def switch_to_login_iframe(self):
        self.iframe = self.find_element(*self.locator.IFRAME)
        self.switch_iframe(self.iframe)

    def login_click(self):
        self.click_sign_in_button()
        self.switch_to_login_iframe()

        if self.user["provider"] == "google":
            self.click_google_login_button()
        elif self.user["provider"] == "facebook":
            self.click_facebook_login_button()
        elif self.user["provider"] == "apple":
            self.click_apple_login_button()

        return LoginPage(self.driver, self.user)
login_page.py
import time

from utils.locators import *
from pages.base_page import BasePage


class LoginPage(BasePage):
    def __init__(self, driver, user):
        self.user = user
        if self.user["provider"] == "google":
            self.locator = GoogleLoginPageLocators
        elif self.user["provider"] == "facebook":
            self.locator = FacebookLoginPageLocators
        elif self.user["provider"] == "apple":
            self.locator = AppleLoginPageLocators

        super().__init__(driver)

    def enter_email(self, email):
        self.find_element(*self.locator.EMAIL).send_keys(email)

    def enter_password(self, password):
        self.find_element(*self.locator.PASSWORD).send_keys(password)

    def click_login_button(self):
        self.find_element(*self.locator.SUBMIT).click()

    def click_continue_button(self):
        self.find_element(*self.locator.CONTINUE).click()

    def switch_window(self, x):
        self.driver.switch_to.window(self.driver.window_handles[x])


    def login(self):
        if self.user["provider"] == "google":
            self.login_google()
        elif self.user["provider"] == "facebook":
            self.login_facebook()
        elif self.user["provider"] == "apple":
            self.login_apple()

        if self.check_element(*self.locator.IFRAME):
            self.age_gender_verification()
        else:
            print("wtf")

    def age_gender_verification(self):
        #change frame for verification
        self.iframe = self.find_element(*self.locator.IFRAME)
        self.switch_iframe(self.iframe)

        #enter age
        self.find_element(*self.locator.AGE).send_keys(self.user["age"])

        #enter gender
        self.find_element(*self.locator.GENDER_SELECT).click()
        if self.user["gender"] == "male":
            self.find_element(*self.locator.GENDER_MALE).click()
        else:
            self.find_element(*self.locator.GENDER_FEMALE).click()

        #Click Validation Continue
        self.find_element(*self.locator.VALIDATION_CONTINUE).click()

        #If error then restart the login proccess
        if self.check_element(*self.locator.VALIDATION_ERROR):
            from pages.main_page import MainPage
            #switch back from iframe
            self.driver.switch_to.default_content()
            #click close
            self.find_element(*self.locator.VALIDATION_CLOSE).click()
            #relogin
            MainPage(self).login_click()
        else:
            print("fail")


    def login_facebook(self):
        self.switch_window(1)
        print(self.user)
        self.enter_email(self.user["email"])
        self.enter_password(self.user["password"])
        self.click_login_button()
        self.switch_window(0)

    def login_google(self):
        self.switch_window(1)
        print(self.user)
        self.enter_email(self.user["email"])
        self.click_continue_button()
        time.sleep(1)
        self.enter_password(self.user["password"])
        self.click_login_button()
        self.switch_window(0)

    def login_apple(self):
        self.switch_window(1)
        print(self.user)
        self.enter_email(self.user["email"])
        self.enter_password(self.user["password"])
        self.click_login_button()
        self.switch_window(0)
Error Message:
Error:
ImportError: cannot import name 'LoginPage' from partially initialized module 'pages.login_page' (most likely due to a circular import)
for the missing classes please look at the github link https://github.com/gunesmes/page-object-python-selenium
Reply


Messages In This Thread
Selenium Page Object Model with Python - by Cryptus - Aug-17-2020, 09:54 PM
RE: Selenium Page Object Model with Python - by Cryptus - Aug-18-2020, 07:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on a button on web page using Selenium Pavel_47 7 7,448 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  Selenium/Helium loads up a blank web page firaki12345 0 2,816 Mar-23-2021, 11:51 AM
Last Post: firaki12345
  Saving html page and reloading into selenium while developing all xpaths Larz60+ 4 6,019 Feb-04-2021, 07:01 AM
Last Post: jonathanwhite1
  Selenium Parsing (unable to Parse page after loading) oneclick 7 7,730 Oct-30-2020, 08:13 PM
Last Post: tomalex
  Code example for entering input in a textbox with requests/selenium object peterjv26 1 3,003 Sep-26-2020, 04:34 PM
Last Post: Larz60+
  Selenium on Angular page Martinelli 3 7,859 Jul-28-2020, 12:40 PM
Last Post: Martinelli
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 4,808 Mar-19-2020, 06:13 PM
Last Post: apollo
  Selenium get data from newly accessed page hoff1022 2 3,790 Oct-09-2019, 06:52 PM
Last Post: hoff1022
  Difficult web page -- Selenium Larz60+ 2 3,491 Dec-31-2018, 06:51 PM
Last Post: Larz60+
  Web Page not opening while web scraping through python selenium sumandas89 4 12,473 Nov-19-2018, 02:47 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020