Skip to content

Instantly share code, notes, and snippets.

@rouge8
Created August 1, 2013 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rouge8/6134091 to your computer and use it in GitHub Desktop.
Save rouge8/6134091 to your computer and use it in GitHub Desktop.
py.test selenium fixtures
import os
import pytest
from selenium import webdriver
browsers = {
'firefox': webdriver.Firefox,
'chrome': webdriver.Chrome,
}
@pytest.fixture(scope='session', params=browsers.keys())
def driver(request):
if 'DISPLAY' not in os.environ and not os.uname()[0] in ('Darwin', ):
pytest.skip('Test requires display server (export DISPLAY)')
if request.param == 'firefox':
profile = webdriver.FirefoxProfile()
profile.set_preference('network.http.use-cache', False)
b = browsers[request.param](firefox_profile=profile)
else:
b = browsers[request.param]()
request.addfinalizer(lambda *args: b.quit())
return b
@pytest.fixture(scope='function')
def browser(driver):
'''Open a Selenium browser with window size and wait time set.'''
b = driver
b.set_window_size(1400, 800)
# Wait for elements/conditions to appear. This slows down the tests
# somewhat in Firefox, but it makes them more reliable.
b.implicitly_wait(5)
return b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment