Skip to content

Instantly share code, notes, and snippets.

Avatar

Mark Jones boatcoder

View GitHub Profile
@boatcoder
boatcoder / conftest.py
Last active Sep 29, 2020
Fixtures and associated configuration for PyTest
View conftest.py
import os
import logging
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import pytest
import sys
# If you want more logging in the test code, export TEST_LOG_LEVEL=DEBUG
@boatcoder
boatcoder / test_login.py
Created Sep 29, 2020
A simple test using the PageElements
View test_login.py
from selenium.webdriver.support.ui import WebDriverWait
import pages
def test_login_with_invalid_credentials(login_url):
try:
driver = get_browser()
driver.get(login_url)
@boatcoder
boatcoder / login.py
Created Sep 29, 2020
Page description of the login page. Connects the Locators to the actual Page Elements
View login.py
from .Base import BasePage
from locators import LoginPageLocators as Locators
from elements import InputPageElement, PageElement
class LoginPage(BasePage):
""" Contains methods to do things on the login page """
username = InputPageElement(Locators.USERNAME)
password = InputPageElement(Locators.PASSWORD)
error_message = PageElement(Locators.LOGIN_ERROR_MESSAGE)
@boatcoder
boatcoder / pages.py
Created Sep 29, 2020
Locators for the login page, these uniquely describe some element on the login page. You would need a set of these for each "page" in your site
View pages.py
class LoginPageLocators(object):
""" A class for login page locators. """
USERNAME = (By.XPATH, '//input[@data-test-id="login-username"]')
PASSWORD = (By.XPATH, '//input[@data-test-id="login-password"]')
LOGIN_BUTTON = (By.XPATH, '//button[@data-test-id="login-button"]')
LOGIN_ERROR_MESSAGE = (By.XPATH, '//div[@data-test-id="login-error"]')
@boatcoder
boatcoder / element.py
Last active Sep 29, 2020
A BasePageElement with a useful constructor and 2 derived classes
View element.py
class BasePageElement(object):
"""Base page class that is initialized on every page object class."""
def __init__(self, locator:Tuple[str, str]):
self.locator = locator
def __set__(self, instance, value:str):
""" returns the element so that derived classes can set things based upon the type of element they are dealing with """
driver = instance.driver
WebDriverWait(driver, 10).until(
lambda driver: driver.find_element(*self.locator))
@boatcoder
boatcoder / element.py
Last active Sep 29, 2020
A better version of a BasePageElement that returns the element
View element.py
from selenium.webdriver.support.ui import WebDriverWait
class BasePageElement(object):
"""Base page class that is initialized on every page object class."""
def __set__(self, obj, value):
"""Sets the text to the value supplied"""
driver = obj.driver
WebDriverWait(driver, 100).until(
@boatcoder
boatcoder / element.py
Created Sep 29, 2020
Original code from the Selenium documentation of PageObjects in Python
View element.py
from selenium.webdriver.support.ui import WebDriverWait
class BasePageElement(object):
"""Base page class that is initialized on every page object class."""
def __set__(self, obj, value):
"""Sets the text to the value supplied"""
driver = obj.driver
WebDriverWait(driver, 100).until(