Skip to content

Commit 917c95f

Browse files
Addressed all comments
1 parent 904547c commit 917c95f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

percy/snapshot.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import requests
77

88
from selenium.webdriver import __version__ as SELENIUM_VERSION
9+
from selenium.common.exceptions import TimeoutException
10+
from selenium.webdriver.support.ui import WebDriverWait
911
from percy.version import __version__ as SDK_VERSION
1012
from percy.driver_metadata import DriverMetaData
1113

@@ -16,6 +18,7 @@
1618
# Maybe get the CLI API address from the environment
1719
PERCY_CLI_API = os.environ.get('PERCY_CLI_API') or 'http://localhost:5338'
1820
PERCY_DEBUG = os.environ.get('PERCY_LOGLEVEL') == 'debug'
21+
RESONSIVE_CAPTURE_SLEEP_TIME = os.environ.get('RESONSIVE_CAPTURE_SLEEP_TIME')
1922

2023
# for logging
2124
LABEL = '[\u001b[35m' + ('percy:python' if PERCY_DEBUG else 'percy') + '\u001b[39m]'
@@ -89,28 +92,48 @@ def get_widths_for_multi_dom(width, widths):
8992
allWidths.extend(eligible_widths.get('config', []))
9093
return list(set(allWidths))
9194

92-
def change_window_dimension(driver, width, height):
95+
def change_window_dimension_and_wait(driver, width, height, resizeCount):
9396
if CDP_SUPPORT_SELENIUM and driver.capabilities['browserName'] == 'chrome':
9497
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', { 'height': height,
9598
'width': width, 'deviceScaleFactor': 1, 'mobile': False })
9699
else:
97100
driver.set_window_size(width, height)
98101

102+
try:
103+
WebDriverWait(driver, 2).until(
104+
lambda driver: driver.execute_script("return window.resizeCount") == resizeCount
105+
)
106+
except TimeoutException:
107+
if PERCY_DEBUG: log(f"Timed out waiting for window resize event for width {width}")
108+
109+
99110
def capture_responsive_dom(driver, cookies, **kwargs):
100111
# cache doesn't work when parameter is a list so passing tuple
101112
widths = get_widths_for_multi_dom(kwargs.get('width'), tuple(kwargs.get('widths') or []))
102113
dom_snapshots = []
103114
window_size = driver.get_window_size()
104115
current_width, current_height = window_size['width'], window_size['height']
116+
resize_count = 0
117+
driver.execute_script("""
118+
// if window resizeCount present means event listener was already present
119+
if (!window.resizeCount) {
120+
window.addEventListener('resize', () => {
121+
window.resizeCount++;
122+
})
123+
}
124+
// always reset count 0
125+
window.resizeCount = 0
126+
""")
105127

106128
for width in widths:
107-
change_window_dimension(driver, width, current_height)
108-
sleep(1)
129+
resize_count += 1
130+
change_window_dimension_and_wait(driver, width, current_height, resize_count)
131+
if RESONSIVE_CAPTURE_SLEEP_TIME: sleep(int(RESONSIVE_CAPTURE_SLEEP_TIME))
109132
dom_snapshot = get_serialized_dom(driver, cookies, **kwargs)
110133
dom_snapshot['width'] = width
111134
dom_snapshots.append(dom_snapshot)
112135

113-
change_window_dimension(driver, current_width, current_height)
136+
change_window_dimension_and_wait(driver, current_width, current_height, resize_count + 1)
114137
return dom_snapshots
115138

116139
# Take a DOM snapshot and post it to the snapshot endpoint

tests/test_snapshot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import unittest
23
from unittest.mock import patch, Mock
34
from http.server import BaseHTTPRequestHandler, HTTPServer
@@ -265,9 +266,10 @@ def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_captu
265266

266267
@patch('selenium.webdriver.Chrome')
267268
def test_posts_snapshots_to_the_local_percy_server_for_responsive_dom_chrome(self, MockChrome):
269+
os.environ['RESONSIVE_CAPTURE_SLEEP_TIME'] = '1'
268270
driver = MockChrome.return_value
269271
driver.execute_script.side_effect = [
270-
'', { 'html': 'some_dom' }, { 'html': 'some_dom_1' }
272+
'', '', 1, { 'html': 'some_dom' }, 2, { 'html': 'some_dom_1' }, 3
271273
]
272274
driver.get_cookies.return_value = ''
273275
driver.execute_cdp_cmd.return_value = ''
@@ -290,7 +292,6 @@ def test_posts_snapshots_to_the_local_percy_server_for_responsive_dom_chrome(sel
290292
self.assertEqual(s1['url'], 'http://localhost:8000/')
291293
self.assertEqual(s1['dom_snapshot'], expected_dom_snapshot)
292294

293-
294295
def test_has_a_backwards_compatible_function(self):
295296
mock_healthcheck()
296297
mock_snapshot()

0 commit comments

Comments
 (0)