Last active
September 29, 2020 21:36
-
-
Save boatcoder/2ad9d39b273a4b955a22f8f18d471117 to your computer and use it in GitHub Desktop.
Fixtures and associated configuration for PyTest
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 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 | |
logging.basicConfig(stream=sys.stdout, | |
level=logging._nameToLevel.get(os.environ.get('TEST_LOG_LEVEL', 'INFO'), 'INFO')) | |
BASE_URL = os.environ.get('BASE_URL', '<Your Base URL here>') | |
if BASE_URL.endswith('/'): | |
BASE_URL = BASE_URL[:-1] | |
logger = logging.getLogger(__name__) | |
@pytest.fixture(scope='session') | |
def base_url(): | |
return BASE_URL | |
@pytest.fixture(scope='session') | |
def login_url(base_url): | |
return os.path.join(base_url, 'login') | |
@pytest.fixture(scope='session') | |
def landing_url(base_url): | |
return os.path.join(base_url, 'landing') | |
@pytest.fixture(scope='session') | |
def company_url(base_url): | |
return os.path.join(base_url, 'company') | |
@pytest.fixture(scope='session') | |
def browser(base_url): | |
logger.info("Launching Browser") | |
browser = webdriver.Firefox() | |
browser.get(base_url) | |
from pages.Login import LoginPage | |
login_page = LoginPage(browser) | |
login_page.login() | |
expected_url = os.path.join(base_url, 'landing') | |
def condition(driver): | |
logger.info("Waiting for %s", expected_url) | |
return driver.current_url == expected_url | |
WebDriverWait(browser, 10).until(condition) | |
# Use yield to allow for custom cleanup of the browser instance | |
yield browser | |
# This is executed when the fixture is destroyed. | |
logger.info("Quitting Browser") | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment