Skip to content

Commit 0e06503

Browse files
Address some comments
1 parent b0e98b4 commit 0e06503

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

percy/snapshot.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
LABEL = '[\u001b[35m' + ('percy:python' if PERCY_DEBUG else 'percy') + '\u001b[39m]'
2222
CDP_SUPPORT_SELENIUM = (str(SELENIUM_VERSION)[0].isdigit() and int(
2323
str(SELENIUM_VERSION)[0]) >= 4) if SELENIUM_VERSION else False
24-
fetched_widths = {}
24+
eligible_widths = {}
2525

2626
def log(message, lvl = 'info'):
27-
requests.post(f'{PERCY_CLI_API}/percy/log',
28-
json={'message': message, 'level': lvl}, timeout=30)
29-
print(message)
27+
try:
28+
requests.post(f'{PERCY_CLI_API}/percy/log',
29+
json={'message': message, 'level': lvl}, timeout=1)
30+
except:
31+
if PERCY_DEBUG: print('Sending log to CLI Failed')
32+
finally:
33+
print(message)
3034

3135
# Check if Percy is enabled, caching the result so it is only checked once
3236
@lru_cache(maxsize=None)
@@ -36,8 +40,8 @@ def is_percy_enabled():
3640
response.raise_for_status()
3741
data = response.json()
3842
session_type = data.get('type', None)
39-
global fetched_widths
40-
fetched_widths = data.get('widths', {})
43+
global eligible_widths
44+
eligible_widths = data.get('widths', {})
4145

4246
if not data['success']: raise Exception(data['error'])
4347
version = response.headers.get('x-percy-core-version')
@@ -66,14 +70,23 @@ def fetch_percy_dom():
6670
response.raise_for_status()
6771
return response.text
6872

73+
def get_serialized_dom(driver, cookies, **kwargs):
74+
dom_snapshot = driver.execute_script(f'return PercyDOM.serialize({json.dumps(kwargs)})')
75+
dom_snapshot['cookies'] = cookies
76+
return dom_snapshot
77+
6978
@lru_cache(maxsize=None)
70-
def get_widths_for_multi_dom(widths):
79+
def get_widths_for_multi_dom(width, widths):
80+
user_passed_widths = []
81+
if len(widths) != 0: user_passed_widths = widths
82+
if width: user_passed_widths = [width]
83+
7184
# Deep copy mobile widths otherwise it will get overridden
72-
allWidths = fetched_widths.get('mobile', [])[:]
73-
if widths and len(widths) != 0:
74-
allWidths.extend(widths)
85+
allWidths = eligible_widths.get('mobile', [])[:]
86+
if len(user_passed_widths) != 0:
87+
allWidths.extend(user_passed_widths)
7588
else:
76-
allWidths.extend(fetched_widths.get('config', []))
89+
allWidths.extend(eligible_widths.get('config', []))
7790
return list(set(allWidths))
7891

7992
def change_window_dimension(driver, width, height):
@@ -84,21 +97,16 @@ def change_window_dimension(driver, width, height):
8497
driver.set_window_size(width, height)
8598

8699
def capture_responsive_dom(driver, cookies, **kwargs):
87-
widths = kwargs.get('widths') or []
88-
if 'width' in kwargs:
89-
widths = [kwargs.get('width')]
90-
91100
# cache doesn't work when parameter is a list so passing tuple
92-
widths = get_widths_for_multi_dom(tuple(widths))
101+
widths = get_widths_for_multi_dom(kwargs.get('width'), tuple(kwargs.get('widths') or []))
93102
dom_snapshots = []
94103
window_size = driver.get_window_size()
95104
current_width, current_height = window_size['width'], window_size['height']
96105

97106
for width in widths:
98107
change_window_dimension(driver, width, current_height)
99108
sleep(1)
100-
dom_snapshot = driver.execute_script(f'return PercyDOM.serialize({json.dumps(kwargs)})')
101-
dom_snapshot['cookies'] = cookies
109+
dom_snapshot = get_serialized_dom(driver, cookies, **kwargs)
102110
dom_snapshot['width'] = width
103111
dom_snapshots.append(dom_snapshot)
104112

@@ -118,20 +126,18 @@ def percy_snapshot(driver, name, **kwargs):
118126
# Inject the DOM serialization script
119127
driver.execute_script(fetch_percy_dom())
120128
cookies = driver.get_cookies()
121-
user_agent = driver.execute_script("return navigator.userAgent;")
122129

123130
# Serialize and capture the DOM
124131
if kwargs.get('responsive_snapshot_capture', False) or kwargs.get(
125132
'responsiveSnapshotCapture', False):
126133
dom_snapshot = capture_responsive_dom(driver, cookies, **kwargs)
127134
else:
128-
dom_snapshot = driver.execute_script(f'return PercyDOM.serialize({json.dumps(kwargs)})')
129-
dom_snapshot['cookies'] = cookies
135+
dom_snapshot = get_serialized_dom(driver, cookies, **kwargs)
130136

131137
# Post the DOM to the snapshot endpoint with snapshot options and other info
132138
response = requests.post(f'{PERCY_CLI_API}/percy/snapshot', json={**kwargs, **{
133139
'client_info': CLIENT_INFO,
134-
'environment_info': ENV_INFO + [user_agent],
140+
'environment_info': ENV_INFO,
135141
'dom_snapshot': dom_snapshot,
136142
'url': driver.current_url,
137143
'name': name

tests/test_snapshot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ def test_posts_snapshots_to_the_local_percy_server(self):
192192
self.assertRegex(s1['client_info'], r'percy-selenium-python/\d+')
193193
self.assertRegex(s1['environment_info'][0], r'selenium/\d+')
194194
self.assertRegex(s1['environment_info'][1], r'python/\d+')
195-
self.assertRegex(s1['environment_info'][2], r'Mozilla/\d+')
196195

197196
s2 = httpretty.latest_requests()[3].parsed_body
198197
self.assertEqual(s2['name'], 'Snapshot 2')
@@ -268,7 +267,7 @@ def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_captu
268267
def test_posts_snapshots_to_the_local_percy_server_for_responsive_dom_chrome(self, MockChrome):
269268
driver = MockChrome.return_value
270269
driver.execute_script.side_effect = [
271-
'', '', { 'html': 'some_dom' }, { 'html': 'some_dom_1' }
270+
'', { 'html': 'some_dom' }, { 'html': 'some_dom_1' }
272271
]
273272
driver.get_cookies.return_value = ''
274273
driver.execute_cdp_cmd.return_value = ''

0 commit comments

Comments
 (0)