refactor selenium driver handling
This commit is contained in:
parent
63f94deb40
commit
0d10ef1b6b
18
tests/acceptance/browsers.py
Normal file
18
tests/acceptance/browsers.py
Normal file
@ -0,0 +1,18 @@
|
||||
BROWSERSTACK_CONFIG = {
|
||||
"win7_ie10": {
|
||||
"browser": "IE",
|
||||
"browser_version": "10.0",
|
||||
"os": "Windows",
|
||||
"os_version": "7",
|
||||
"resolution": "1024x768",
|
||||
"browserstack.local": True,
|
||||
},
|
||||
"win10_chrome62": {
|
||||
"browser": "Chrome",
|
||||
"browser_version": "62.0",
|
||||
"os": "Windows",
|
||||
"os_version": "10",
|
||||
"resolution": "1024x768",
|
||||
"browserstack.local": True,
|
||||
},
|
||||
}
|
@ -2,16 +2,18 @@ import os
|
||||
import pytest
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from collections import Mapping
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
|
||||
from .live_server import LiveServer
|
||||
from .browsers import BROWSERSTACK_CONFIG
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def live_app(app):
|
||||
handler = RotatingFileHandler('log/acceptance.log', maxBytes=10000, backupCount=1)
|
||||
handler = RotatingFileHandler("log/acceptance.log", maxBytes=10000, backupCount=1)
|
||||
handler.setLevel(logging.INFO)
|
||||
app.logger.addHandler(handler)
|
||||
|
||||
@ -24,44 +26,49 @@ def live_app(app):
|
||||
runnable.terminate()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def browserstack_config():
|
||||
return {
|
||||
"win7_ie10": {
|
||||
"browser": "IE",
|
||||
"browser_version": "10.0",
|
||||
"os": "Windows",
|
||||
"os_version": "7",
|
||||
"resolution": "1024x768",
|
||||
"browserstack.local": True,
|
||||
},
|
||||
"iphone7": {
|
||||
'browserName': 'iPhone',
|
||||
'device': 'iPhone 7',
|
||||
'realMobile': 'true',
|
||||
'os_version': '10.3',
|
||||
"browserstack.local": True,
|
||||
}
|
||||
}
|
||||
class DriverCollection(Mapping):
|
||||
"""
|
||||
Allows access to drivers with dictionary syntax. Keeps track of which ones
|
||||
have already been initialized. Allows teardown of all existing drivers.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._drivers = {}
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def driver_builder(browserstack_config):
|
||||
def build_driver(config_key):
|
||||
def __iter__(self):
|
||||
return iter(self._drivers)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._drivers)
|
||||
|
||||
def __getitem__(self, name):
|
||||
if name in self._drivers:
|
||||
return self._drivers[name]
|
||||
|
||||
elif name in BROWSERSTACK_CONFIG:
|
||||
self._drivers[name] = self._build_driver(name)
|
||||
return self._drivers[name]
|
||||
|
||||
else:
|
||||
raise AttributeError("Driver {} not found".format(name))
|
||||
|
||||
def _build_driver(self, config_key):
|
||||
return webdriver.Remote(
|
||||
command_executor="http://{}:{}@hub.browserstack.com:80/wd/hub".format(
|
||||
os.getenv("BROWSERSTACK_EMAIL"), os.getenv("BROWSERSTACK_TOKEN")
|
||||
),
|
||||
desired_capabilities=browserstack_config.get(config_key),
|
||||
desired_capabilities=BROWSERSTACK_CONFIG.get(config_key),
|
||||
)
|
||||
|
||||
return build_driver
|
||||
def teardown(self):
|
||||
for driver in self._drivers.values():
|
||||
driver.quit()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def ie10_driver(driver_builder):
|
||||
driver = driver_builder("win7_ie10")
|
||||
def drivers():
|
||||
driver_collection = DriverCollection()
|
||||
|
||||
yield driver
|
||||
yield driver_collection
|
||||
|
||||
driver.quit()
|
||||
driver_collection.teardown()
|
||||
|
@ -1,3 +1,9 @@
|
||||
def test_can_get_title(live_app, ie10_driver):
|
||||
ie10_driver.get(live_app.server_url)
|
||||
assert "JEDI" in ie10_driver.title
|
||||
import pytest
|
||||
from .browsers import BROWSERSTACK_CONFIG
|
||||
|
||||
|
||||
@pytest.mark.parametrize("browser_type", BROWSERSTACK_CONFIG.keys())
|
||||
def test_can_get_title(browser_type, live_app, drivers):
|
||||
driver = drivers[browser_type]
|
||||
driver.get(live_app.server_url)
|
||||
assert "JEDI" in driver.title
|
||||
|
Loading…
x
Reference in New Issue
Block a user