Skip to content

Commit 372912b

Browse files
author
Xing Han Lu
committed
Break down percy.io tests into multiple snapshots
1 parent c94b1ac commit 372912b

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

tests/IntegrationTests.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def percy_snapshot(self, name=''):
2222
snapshot_name = '{} - {}'.format(name, sys.version_info)
2323

2424
self.percy_runner.snapshot(
25-
name=snapshot_name,
26-
enable_javascript=True
25+
name=snapshot_name
2726
)
2827

2928
@classmethod
@@ -39,7 +38,8 @@ def setUpClass(cls):
3938

4039
if os.environ.get('PERCY_ENABLED', False):
4140
loader = percy.ResourceLoader(webdriver=cls.driver)
42-
cls.percy_runner = percy.Runner(loader=loader)
41+
percy_config = percy.Config(default_widths=[1280])
42+
cls.percy_runner = percy.Runner(loader=loader, config=percy_config)
4343
cls.percy_runner.initialize_build()
4444

4545
@classmethod
@@ -53,15 +53,16 @@ def tearDownClass(cls):
5353
def setUp(self):
5454
pass
5555

56-
def tearDown(self):
56+
def tearDown(self, port=8050):
5757
time.sleep(3)
5858
if platform.system() == 'Windows':
59-
requests.get('http://localhost:8050/stop')
59+
requests.get('http://localhost:{}/stop'.format(port))
60+
sys.exit()
6061
else:
6162
self.server_process.terminate()
6263
time.sleep(3)
6364

64-
def startServer(self, app):
65+
def startServer(self, app, port=8050):
6566
if 'DASH_TEST_PROCESSES' in os.environ:
6667
processes = int(os.environ['DASH_TEST_PROCESSES'])
6768
else:
@@ -71,7 +72,7 @@ def run():
7172
app.scripts.config.serve_locally = True
7273
app.css.config.serve_locally = True
7374
app.run_server(
74-
port=8050,
75+
port=port,
7576
debug=False,
7677
processes=processes
7778
)
@@ -87,9 +88,9 @@ def _stop_server_windows():
8788
return 'stop'
8889

8990
app.run_server(
90-
port=8050,
91+
port=port,
9192
debug=False,
92-
threaded=True
93+
threaded=False
9394
)
9495

9596
# Run on a separate process so that it doesn't block
@@ -105,5 +106,5 @@ def _stop_server_windows():
105106
time.sleep(5)
106107

107108
# Visit the dash page
108-
self.driver.get('http://localhost:8050')
109+
self.driver.get('http://localhost:{}'.format(port))
109110
time.sleep(0.5)

tests/test_percy_snapshot.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import base64
2-
import dash
3-
import dash_html_components as html
42
import os
3+
import time
4+
55
from .IntegrationTests import IntegrationTests
6+
import dash
7+
import dash_html_components as html
8+
import dash_core_components as dcc
69
from selenium.webdriver.common.by import By
710
from selenium.webdriver.support.ui import WebDriverWait
811
from selenium.webdriver.support import expected_conditions as EC
@@ -31,28 +34,46 @@ def encode(name):
3134
encoded_string = base64.b64encode(image_file.read())
3235
return "data:image/png;base64," + encoded_string.decode('ascii')
3336

34-
def image_section(name):
35-
return html.Div([
36-
html.P(name),
37-
html.Img(src=encode(name))
38-
])
39-
40-
asset_list = os.listdir(os.path.join(
41-
os.path.dirname(__file__),
42-
'screenshots'
43-
))
44-
37+
# Define the app
4538
app = dash.Dash(__name__)
4639

4740
app.layout = html.Div([
48-
image_section(name)
49-
for name in asset_list if name.endswith('png')
41+
# represents the URL bar, doesn't render anything
42+
dcc.Location(id='url', refresh=False),
43+
# content will be rendered in this element
44+
html.Div(id='page-content')
5045
])
5146

47+
@app.callback(dash.dependencies.Output('page-content', 'children'),
48+
[dash.dependencies.Input('url', 'pathname')])
49+
def display_image(pathname):
50+
"""
51+
Assign the url path to return the image it represent. For example,
52+
to return "usage.png", you can visit localhost/usage.png.
53+
:param pathname: name of the screenshot, prefixed with "/"
54+
:return: An html.Img object containing the base64 encoded image
55+
"""
56+
if pathname:
57+
name = pathname.replace('/', '')
58+
return html.Img(id=name, src=encode(name))
59+
60+
# Start the app
5261
self.startServer(app)
5362

54-
WebDriverWait(self.driver, 30).until(
55-
EC.presence_of_element_located((By.CSS_SELECTOR, "img"))
56-
)
63+
# Find the names of all the screenshots
64+
asset_list = os.listdir(os.path.join(
65+
os.path.dirname(__file__),
66+
'screenshots'
67+
))
68+
69+
# Run Percy
70+
for image in asset_list:
71+
if image.endswith('png'):
72+
self.driver.get('http://localhost:8050/{}'.format(image))
73+
74+
WebDriverWait(self.driver, 20).until(
75+
EC.presence_of_element_located((By.ID, image))
76+
)
5777

58-
self.percy_snapshot(name='Snapshot of all usage apps')
78+
self.percy_snapshot(name=image)
79+
time.sleep(2)

0 commit comments

Comments
 (0)