-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
163 lines (138 loc) · 5.14 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Configuration of the pytest with CmdLine options, Fixtures and TearDown
function
"""
import os
import pytest
from django.conf import settings
from selenium.webdriver import DesiredCapabilities, Chrome
from selenium.webdriver import Firefox, PhantomJS, Remote
from control_chart.tests.utilies import create_correct_sample_data
from control_chart.tests.utilies import create_limited_users, login_as_admin
from control_chart.tests.utilies import create_characteristic_values
def pytest_addoption(parser):
"""
Add the different Browser engines as cmd line option
"""
parser.addoption('--phantomjs', action="store_true",
help='Use PhantomJS Webdriver instead of FireFox')
parser.addoption('--htmlunit', action="store_true",
help='Use HtmlUnit Webdriver instead of FireFox')
parser.addoption('--chrome', action="store_true",
help='Use Google Chrome Webdriver instead of FireFox')
parser.addoption('--ci', action="store_true",
help='Using the chrome on sauce lab')
class HtmlUnit(Remote):
"""
Wrapper class for the HtmlUnit Webdriver to set the server address
"""
def __init__(self):
super(HtmlUnit, self).__init__('http://127.0.0.1:4444/wd/hub',
DesiredCapabilities.HTMLUNITWITHJS)
class SauceLab(Remote):
"""
Wrapper class for the Remote Webdriver for using SauceLab
"""
def __init__(self, request=None):
tunnel_id = os.environ['TRAVIS_JOB_NUMBER']
browser = os.environ['SAUCE_BROWSER']
platform = os.environ['SAUCE_PLATFORM']
desired_cap = {
'platform': platform,
'browserName': browser,
'tunnelIdentifier': tunnel_id
}
if request:
desired_cap['name'] = request.node.nodeid
user = os.environ['SAUCE_USERNAME']
key = os.environ['SAUCE_ACCESS_KEY']
url = 'http://{0}:{1}@ondemand.saucelabs.com/wd/hub'.format(user, key)
super(SauceLab, self).__init__(url, desired_cap)
def pytest_generate_tests(metafunc):
"""
Processes the new CmdLine Options
"""
if 'webdriver' in metafunc.fixturenames:
if metafunc.config.option.phantomjs:
metafunc.parametrize(['webdriver'], ((PhantomJS,),))
elif metafunc.config.option.htmlunit:
metafunc.parametrize(['webdriver'], ((HtmlUnit,),))
elif metafunc.config.option.chrome:
metafunc.parametrize(['webdriver'], ((Chrome,),))
elif metafunc.config.option.ci:
metafunc.parametrize(['webdriver'], ((SauceLab,),))
else:
metafunc.parametrize(['webdriver'], ((Firefox,),))
@pytest.fixture
def fix_webdriver(request):
"""
Browser engine fixutre for use in working_instance fixture
"""
webdriver = Firefox
if request.config.getoption('phantomjs'):
webdriver = PhantomJS
elif request.config.getoption('chrome'):
webdriver = Chrome
elif request.config.getoption('htmlunit'):
webdriver = HtmlUnit
elif request.config.getoption('ci'):
webdriver = lambda: SauceLab(request) # # noqa
return webdriver
@pytest.fixture
def test_data():
"""
Creates test data in the database
"""
create_correct_sample_data()
create_limited_users()
create_characteristic_values()
@pytest.fixture
def working_instance(request, live_server,
fix_webdriver, # pylint: disable=W0621
transactional_db, # pylint: disable=W0613
test_data): # pylint: disable=W0613,W0621
"""
Create ready to use testing enviroment, with sample datas in the db and
webdriver instance which is logged in as admin
"""
def fin(selenium):
"""
Close the Webdriver
"""
selenium.quit()
selenium = fix_webdriver()
selenium.get(live_server.url)
login_as_admin(selenium)
request.addfinalizer(lambda: fin(selenium))
return selenium
@pytest.fixture(scope='session')
def bokeh_server(request):
"""
Fixture to start the bokeh server, the fixture is session scoped so the
server starts only once per session
"""
from subprocess import Popen
os.environ['BOKEH_SERVER'] = 'http://localhost:6008/'
server = Popen(['bokeh', 'serve',
'--port=6008',
'--allow-websocket-origin=localhost:8081',
'--allow-websocket-origin=127.0.0.1:8081',
'--allow-websocket-origin=localhost:8082',
'--allow-websocket-origin=127.0.0.1:8082'])
def fin(server):
"""
Stops the bokeh server
"""
server.terminate()
request.addfinalizer(lambda: fin(server))
return None
def pytest_runtest_teardown(item):
"""
Teardown function to delete all create measurement raw data files
"""
if item.get_marker('django_db') and \
os.path.exists(settings.MEASUREMENT_FILE_DIR):
for file in os.listdir(settings.MEASUREMENT_FILE_DIR):
os.remove(os.path.join(settings.MEASUREMENT_FILE_DIR, file))