Last active
November 22, 2018 17:33
-
-
Save gunesmes/6b1aa7fd5f3859904cc0 to your computer and use it in GitHub Desktop.
Page-object model is a pattern that you can apply it to develop efficient automation framework. With page-model, it is possible to minimize maintanecy cost. Basically page-object means that your every page is inhereted from a base class which includes basic functionality for every page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.action_chains import ActionChains | |
# this Base class is serving basic attributes for every single page inherited from Page class | |
class Page(object): | |
def __init__(self, driver, base_url='http://www.app.com/'): | |
self.base_url = base_url | |
self.driver = driver | |
self.timeout = 30 | |
def find_element(self, *locator): | |
return self.driver.find_element(*locator) | |
def open(self,url): | |
url = self.base_url + url | |
self.driver.get(url) | |
def get_title(self): | |
return self.driver.title | |
def get_url(self): | |
return self.driver.current_url | |
def hover(self, *locator): | |
element = self.find_element(*locator) | |
hover = ActionChains(self.driver).move_to_element(element) | |
hover.perform() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.webdriver.common.by import By | |
# for maintainability we can seperate web objects by page name | |
class MainPageLocatars(object): | |
LOGO = (By.ID, 'nav-logo') | |
ACCOUNT = (By.ID, 'nav-link-yourAccount') | |
SIGNUP = (By.CSS_SELECTOR, '#nav-flyout-ya-newCust > a') | |
LOGIN = (By.CSS_SELECTOR, '#nav-flyout-ya-signin > a') | |
SEARCH = (By.ID, 'twotabsearchtextbox') | |
SEARCH_LIST = (By.ID, 's-results-list-atf') | |
class LoginPageLocatars(object): | |
EMAIL = (By.ID, 'ap_email') | |
PASSWORD = (By.ID, 'ap_password') | |
SUBMIT = (By.ID, 'signInSubmit-input') | |
ERROR_MESSAGE = (By.ID, 'message_error') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from base import Page | |
from locators import * | |
import users | |
from selenium.webdriver.support.ui import WebDriverWait | |
# Page opjects are written in this module. | |
# Depends on the page functionality we can have more functions for new classes | |
class MainPage(Page): | |
def check_page_loaded(self): | |
return True if self.find_element(*MainPageLocatars.LOGO) else False | |
def search_item(self, item): | |
self.find_element(*MainPageLocatars.SEARCH).send_keys(item) | |
self.find_element(*MainPageLocatars.SEARCH).send_keys(Keys.ENTER) | |
return self.find_element(*MainPageLocatars.SEARCH_LIST).text | |
def click_sign_up_button(self): | |
self.hover(*MainPageLocatars.ACCOUNT) | |
self.find_element(*MainPageLocatars.SIGNUP).click() | |
return SignUpPage(self.driver) | |
def click_sign_in_button(self): | |
self.hover(*MainPageLocatars.ACCOUNT) | |
self.find_element(*MainPageLocatars.LOGIN).click() | |
return LoginPage(self.driver) | |
class LoginPage(Page): | |
def enter_email(self, user): | |
self.find_element(*LoginPageLocatars.EMAIL).send_keys(users.get_user(user)["email"]) | |
def enter_password(self, user): | |
self.find_element(*LoginPageLocatars.PASSWORD).send_keys(users.get_user(user)["password"]) | |
def click_login_button(self): | |
self.find_element(*LoginPageLocatars.SUBMIT).click() | |
def login(self, user): | |
self.enter_email(user) | |
self.enter_password(user) | |
self.click_login_button() | |
def login_with_valid_user(self, user): | |
self.login(user) | |
return HomePage(self.driver) | |
def login_with_in_valid_user(self, user): | |
self.login(user) | |
return self.find_element(*LoginPageLocatars.ERROR_MESSAGE).text | |
class HomePage(Page): | |
pass | |
class SignUpPage(Page): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from selenium import webdriver | |
from pages import * | |
from testCases import test_cases | |
from locators import * | |
from selenium.webdriver.common.by import By | |
# I am using python unittest for asserting cases. | |
# In this module, there should be test cases. | |
# If you want to run it, you should type: python <module-name.py> | |
class TestPages(unittest.TestCase): | |
def setUp(self): | |
self.driver = webdriver.Chrome() | |
#self.driver = webdriver.Firefox() | |
self.driver.get("http://www.amazon.com") | |
def test_page_load(self): | |
print "\n" + str(test_cases(0)) | |
page = MainPage(self.driver) | |
self.assertTrue(page.check_page_loaded()) | |
def test_search_item(self): | |
print "\n" + str(test_cases(1)) | |
page = MainPage(self.driver) | |
search_result = page.search_item("Nexus 5") | |
self.assertIn("Nexus 5", search_result) | |
def test_sign_up_button(self): | |
print "\n" + str(test_cases(2)) | |
page = MainPage(self.driver) | |
signUpPage = page.click_sign_up_button() | |
self.assertIn("ap/register", signUpPage.get_url()) | |
def test_sign_in_button(self): | |
print "\n" + str(test_cases(3)) | |
page = MainPage(self.driver) | |
loginPage = page.click_sign_in_button() | |
self.assertIn("ap/signin", loginPage.get_url()) | |
def test_sign_in_with_valid_user(self): | |
print "\n" + str(test_cases(4)) | |
mainPage = MainPage(self.driver) | |
loginPage = mainPage.click_sign_in_button() | |
result = loginPage.login_with_valid_user("valid_user") | |
self.assertIn("yourstore/home", result.get_url()) | |
def test_sign_in_with_in_valid_user(self): | |
print "\n" + str(test_cases(5)) | |
mainPage = MainPage(self.driver) | |
loginPage = mainPage.click_sign_in_button() | |
result = loginPage.login_with_in_valid_user("invalid_user") | |
self.assertIn("There was a problem with your request", result) | |
def tearDown(self): | |
self.driver.close() | |
if __name__ == "__main__": | |
suite = unittest.TestLoader().loadTestsFromTestCase(TestPages) | |
unittest.TextTestRunner(verbosity=2).run(suite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment