Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #160 from plotly/test-config-flags
Browse files Browse the repository at this point in the history
Test config flags
  • Loading branch information
byronz authored Apr 18, 2019
2 parents bfa44d5 + 6800ab4 commit 3360995
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 111 deletions.
100 changes: 57 additions & 43 deletions tests/IntegrationTests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import dash
import dash_core_components
import dash_core_components as dcc
Expand All @@ -12,25 +10,40 @@
import os
import sys

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class IntegrationTests(unittest.TestCase):

def percy_snapshot(cls, name=''):
last_timestamp = 0


def percy_snapshot(self, name=''):
snapshot_name = '{} - py{}.{}'.format(name, sys.version_info.major, sys.version_info.minor)
print(snapshot_name)
cls.percy_runner.snapshot(
self.percy_runner.snapshot(
name=snapshot_name
)
cls.driver.save_screenshot('/tmp/artifacts/{}.png'.format(name))
self.driver.save_screenshot('/tmp/artifacts/{}.png'.format(name))

@classmethod
def setUpClass(cls):
super(IntegrationTests, cls).setUpClass()
cls.driver = webdriver.Chrome()

loader = percy.ResourceLoader(
webdriver=cls.driver
)
options = Options()
capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = {'browser': 'SEVERE'}

if 'DASH_TEST_CHROMEPATH' in os.environ:
options.binary_location = os.environ['DASH_TEST_CHROMEPATH']

cls.driver = webdriver.Chrome(
options=options, desired_capabilities=capabilities)

loader = percy.ResourceLoader(webdriver=cls.driver)
cls.percy_runner = percy.Runner(loader=loader)

cls.percy_runner.initialize_build()
Expand All @@ -42,15 +55,18 @@ def tearDownClass(cls):
cls.driver.quit()
cls.percy_runner.finalize_build()

def setUp(s):
def setUp(self):
pass

def tearDown(s):
def tearDown(self):
time.sleep(2)
s.server_process.terminate()
self.server_process.terminate()
time.sleep(2)

def startServer(s, dash, **kwargs):
self.clear_log()
time.sleep(1)

def startServer(self, dash, **kwargs):
def run():
dash.scripts.config.serve_locally = True
dash.css.config.serve_locally = True
Expand All @@ -64,36 +80,34 @@ def run():
dash.run_server(**kws)

# Run on a separate process so that it doesn't block
s.server_process = multiprocessing.Process(target=run)
s.server_process.start()
self.server_process = multiprocessing.Process(target=run)
self.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)
self.driver.implicitly_wait(2)
self.driver.get('http://localhost:8050')

def clear_log(self):
entries = self.driver.get_log("browser")
if entries:
self.last_timestamp = entries[-1]["timestamp"]

def get_log(self):
entries = self.driver.get_log("browser")
return [entry for entry in entries if entry["timestamp"] > self.last_timestamp]

def wait_until_get_log(self, timeout=10):
logs = None
cnt, poll = 0, 0.1
while not logs:
logs = self.get_log()
time.sleep(poll)
cnt += 1
if cnt * poll >= timeout * 1000:
raise TimeoutError('cannot get log in {}'.format(timeout))

return logs

def is_console_clean(self):
return not self.get_log()
4 changes: 2 additions & 2 deletions tests/test_race_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dash_core_components as dcc

from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console, wait_for
from .utils import wait_for


class Tests(IntegrationTests):
Expand Down Expand Up @@ -60,7 +60,7 @@ def element_text(id):
)
)

assert_clean_console(self)
self.assertTrue(self.is_console_clean())

return test

Expand Down
Loading

0 comments on commit 3360995

Please sign in to comment.