-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathIntegrationTests.py
115 lines (94 loc) · 3.45 KB
/
IntegrationTests.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
import multiprocessing
import sys
import time
import unittest
import percy
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TIMEOUT = 20
class IntegrationTests(unittest.TestCase):
def percy_snapshot(cls, name=''):
snapshot_name = '{} - py{}.{}'.format(name, sys.version_info.major, sys.version_info.minor)
print(snapshot_name)
cls.percy_runner.snapshot(
name=snapshot_name
)
def wait_for_element_by_css_selector(self, selector, timeout=TIMEOUT):
return WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, selector)),
'Could not find element with selector "{}"'.format(selector)
)
def wait_for_text_to_equal(self, selector, assertion_text, timeout=TIMEOUT):
el = self.wait_for_element_by_css_selector(selector)
WebDriverWait(self.driver, timeout).until(
lambda *args: (
(str(el.text) == assertion_text) or
(str(el.get_attribute('value')) == assertion_text)
),
"Element '{}' text was supposed to equal '{}' but it didn't".format(
selector,
assertion_text
)
)
@classmethod
def setUpClass(cls):
super(IntegrationTests, cls).setUpClass()
cls.driver = webdriver.Chrome()
loader = percy.ResourceLoader(
webdriver=cls.driver,
base_url='/assets',
root_dir='tests/assets'
)
cls.percy_runner = percy.Runner(loader=loader)
cls.percy_runner.initialize_build()
@classmethod
def tearDownClass(cls):
super(IntegrationTests, cls).tearDownClass()
cls.driver.quit()
cls.percy_runner.finalize_build()
def setUp(s):
pass
def tearDown(s):
if hasattr(s, 'server_process'):
time.sleep(2)
s.server_process.terminate()
time.sleep(2)
def startServer(s, dash):
def run():
dash.scripts.config.serve_locally = True
dash.run_server(
port=8050,
debug=False,
processes=4,
threaded=False
)
# Run on a separate process so that it doesn't block
s.server_process = multiprocessing.Process(target=run)
s.server_process.start()
time.sleep(0.5)
# Visit the dash page
s.driver.get('http://localhost:8050')
time.sleep(0.5)
# Inject an error and warning logger
logger = '''
window.tests = {};
window.tests.console = {error: [], warn: [], log: []};
var _log = console.log;
var _warn = console.warn;
var _error = console.error;
console.log = function() {
window.tests.console.log.push({method: 'log', arguments: arguments});
return _log.apply(console, arguments);
};
console.warn = function() {
window.tests.console.warn.push({method: 'warn', arguments: arguments});
return _warn.apply(console, arguments);
};
console.error = function() {
window.tests.console.error.push({method: 'error', arguments: arguments});
return _error.apply(console, arguments);
};
'''
s.driver.execute_script(logger)