|
1 | 1 | import base64 |
2 | | -import dash |
3 | | -import dash_html_components as html |
4 | 2 | import os |
| 3 | +import time |
| 4 | + |
5 | 5 | from .IntegrationTests import IntegrationTests |
| 6 | +import dash |
| 7 | +import dash_html_components as html |
| 8 | +import dash_core_components as dcc |
6 | 9 | from selenium.webdriver.common.by import By |
7 | 10 | from selenium.webdriver.support.ui import WebDriverWait |
8 | 11 | from selenium.webdriver.support import expected_conditions as EC |
@@ -31,28 +34,46 @@ def encode(name): |
31 | 34 | encoded_string = base64.b64encode(image_file.read()) |
32 | 35 | return "data:image/png;base64," + encoded_string.decode('ascii') |
33 | 36 |
|
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 |
45 | 38 | app = dash.Dash(__name__) |
46 | 39 |
|
47 | 40 | 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') |
50 | 45 | ]) |
51 | 46 |
|
| 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 |
52 | 61 | self.startServer(app) |
53 | 62 |
|
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 | + ) |
57 | 77 |
|
58 | | - self.percy_snapshot(name='Snapshot of all usage apps') |
| 78 | + self.percy_snapshot(name=image) |
| 79 | + time.sleep(2) |
0 commit comments