Created
November 3, 2017 10:03
-
-
Save rafalf/3ff566b0e020e65aee1b3867215585a7 to your computer and use it in GitHub Desktop.
pytest conftest for show&tell
This file contains hidden or 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 pytest | |
from selenium import webdriver | |
import os | |
from sys import platform | |
from config import * | |
import logging | |
import logging.config | |
def pytest_addoption(parser): | |
parser.addoption("--browser", action="store", default="chrome", help="Browser type: chrome or firefox.") | |
parser.addoption("--logger", action="store", default="DEBUG", help="Logger level: INFO, DEBUG, WARNING ERROR") | |
parser.addoption("--headless", action="store_true", default=False, help="Headless mode if supplied.") | |
@pytest.fixture() | |
def driver(): | |
global BROWSER | |
BROWSER = pytest.config.getoption("--browser") | |
print("--browser: {}".format(BROWSER)) | |
headless = pytest.config.getoption("--headless") | |
# setup | |
if BROWSER == 'chrome': | |
# browser preferences and options | |
chromeOptions = webdriver.ChromeOptions() | |
prefs = dict() | |
prefs["credentials_enable_service"] = False | |
prefs["password_manager_enabled"] = False | |
chromeOptions.add_experimental_option("prefs", prefs) | |
chromeOptions.add_argument("--disable-extensions") | |
chromeOptions.add_argument("--disable-infobars") | |
if headless: | |
print("Run test in headless mode: --headless") | |
chromeOptions.add_argument("--headless") | |
if platform == 'darwin': | |
d = webdriver.Chrome(chrome_options=chromeOptions) | |
elif platform == 'linux' or platform == 'linux2': | |
d = webdriver.Chrome(chrome_options=chromeOptions) | |
else: # win64 | |
chrome_path = os.path.join(BASE_DIR, 'drivers', 'chrome-win', 'chromedriver.exe') | |
d = webdriver.Chrome(executable_path=chrome_path, chrome_options=chromeOptions) | |
elif BROWSER == 'firefox': | |
if platform == 'darwin': | |
d = webdriver.Firefox() | |
elif platform == 'linux' or platform == 'linux2': | |
d = webdriver.Firefox() | |
else: # win64 | |
gecko_path = os.path.join(BASE_DIR, 'drivers', 'gecko-win', 'geckodriver.exe') | |
d = webdriver.Firefox(executable_path=gecko_path) | |
else: | |
print("unrecognized --browser: {}".format(BROWSER)) | |
yield None | |
d.get(BASE_URL) | |
d.maximize_window() | |
yield d | |
# teardown | |
d.quit() | |
@pytest.fixture() | |
def logger(): | |
LEVEL = pytest.config.getoption("--logger") | |
print("--logger: {}".format(LEVEL)) | |
logging.config.dictConfig(LOGGING_CONFIG) | |
log = logging.getLogger('main') | |
log.setLevel(level=logging.getLevelName(LEVEL)) | |
return log | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment