Skip to content

Commit b0e98b4

Browse files
Lint fixes
1 parent c5cdb85 commit b0e98b4

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

percy/snapshot.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import platform
33
import json
44
from functools import lru_cache
5+
from time import sleep
56
import requests
67

78
from selenium.webdriver import __version__ as SELENIUM_VERSION
89
from percy.version import __version__ as SDK_VERSION
910
from percy.driver_metadata import DriverMetaData
10-
from time import sleep
1111

1212
# Collect client and environment information
1313
CLIENT_INFO = 'percy-selenium-python/' + SDK_VERSION
@@ -19,11 +19,13 @@
1919

2020
# for logging
2121
LABEL = '[\u001b[35m' + ('percy:python' if PERCY_DEBUG else 'percy') + '\u001b[39m]'
22-
CDP_SUPPORT_SELENIUM = (str(SELENIUM_VERSION)[0].isdigit() and int(str(SELENIUM_VERSION)[0]) >= 4) if SELENIUM_VERSION else False
22+
CDP_SUPPORT_SELENIUM = (str(SELENIUM_VERSION)[0].isdigit() and int(
23+
str(SELENIUM_VERSION)[0]) >= 4) if SELENIUM_VERSION else False
2324
fetched_widths = {}
2425

2526
def log(message, lvl = 'info'):
26-
requests.post(f'{PERCY_CLI_API}/percy/log', json={ 'message': message, 'level': lvl }, timeout=30)
27+
requests.post(f'{PERCY_CLI_API}/percy/log',
28+
json={'message': message, 'level': lvl}, timeout=30)
2729
print(message)
2830

2931
# Check if Percy is enabled, caching the result so it is only checked once
@@ -66,7 +68,6 @@ def fetch_percy_dom():
6668

6769
@lru_cache(maxsize=None)
6870
def get_widths_for_multi_dom(widths):
69-
global fetched_widths
7071
# Deep copy mobile widths otherwise it will get overridden
7172
allWidths = fetched_widths.get('mobile', [])[:]
7273
if widths and len(widths) != 0:
@@ -77,7 +78,8 @@ def get_widths_for_multi_dom(widths):
7778

7879
def change_window_dimension(driver, width, height):
7980
if CDP_SUPPORT_SELENIUM and driver.capabilities['browserName'] == 'chrome':
80-
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', { 'height': height, 'width': width, 'deviceScaleFactor': 1, 'mobile': False })
81+
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', { 'height': height,
82+
'width': width, 'deviceScaleFactor': 1, 'mobile': False })
8183
else:
8284
driver.set_window_size(width, height)
8385

@@ -112,15 +114,15 @@ def percy_snapshot(driver, name, **kwargs):
112114
"For more information on usage of PercyScreenshot, "\
113115
"refer https://www.browserstack.com/docs/percy/integrate/functional-and-visual")
114116

115-
116117
try:
117118
# Inject the DOM serialization script
118119
driver.execute_script(fetch_percy_dom())
119120
cookies = driver.get_cookies()
120121
user_agent = driver.execute_script("return navigator.userAgent;")
121122

122123
# Serialize and capture the DOM
123-
if kwargs.get('responsive_snapshot_capture', False) or kwargs.get('responsiveSnapshotCapture', False):
124+
if kwargs.get('responsive_snapshot_capture', False) or kwargs.get(
125+
'responsiveSnapshotCapture', False):
124126
dom_snapshot = capture_responsive_dom(driver, cookies, **kwargs)
125127
else:
126128
dom_snapshot = driver.execute_script(f'return PercyDOM.serialize({json.dumps(kwargs)})')

tests/test_snapshot.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import httpretty
88
import requests
9-
from selenium.webdriver import Firefox, FirefoxOptions, Chrome
9+
from selenium.webdriver import Firefox, FirefoxOptions
1010
from selenium.webdriver.remote.webdriver import WebDriver
1111
from selenium.webdriver.remote.webelement import WebElement
1212
from selenium.webdriver.remote.remote_connection import RemoteConnection
@@ -48,7 +48,7 @@ def dummy_method1(self):
4848

4949

5050
# mock helpers
51-
def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = {}):
51+
def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = None):
5252
health_body = { "success": True }
5353
health_headers = { 'X-Percy-Core-Version': '1.0.0' }
5454
health_status = 200
@@ -64,7 +64,7 @@ def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = {
6464
if session_type:
6565
health_body["type"] = session_type
6666

67-
health_body['widths'] = widths
67+
if widths: health_body['widths'] = widths
6868
health_body = json.dumps(health_body)
6969
httpretty.register_uri(
7070
httpretty.GET, 'http://localhost:5338/percy/healthcheck',
@@ -73,7 +73,8 @@ def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = {
7373
status=health_status)
7474
httpretty.register_uri(
7575
httpretty.GET, 'http://localhost:5338/percy/dom.js',
76-
body='window.PercyDOM = { serialize: () => { return { html: document.documentElement.outerHTML } } };',
76+
body='window.PercyDOM = \
77+
{ serialize: () => { return { html: document.documentElement.outerHTML } } };',
7778
status=200)
7879

7980
def mock_snapshot(fail=False, data=False):
@@ -175,7 +176,8 @@ def test_posts_snapshots_to_the_local_percy_server(self):
175176
mock_healthcheck()
176177
mock_snapshot()
177178
self.driver.add_cookie({'name': 'foo', 'value': 'bar'})
178-
expected_cookies = [{'name': 'foo', 'value': 'bar', 'path': '/', 'domain': 'localhost', 'secure': False, 'httpOnly': False, 'sameSite': 'None'}]
179+
expected_cookies = [{'name': 'foo', 'value': 'bar', 'path': '/',
180+
'domain': 'localhost', 'secure': False, 'httpOnly': False, 'sameSite': 'None'}]
179181

180182
percy_snapshot(self.driver, 'Snapshot 1')
181183
response = percy_snapshot(self.driver, 'Snapshot 2', enable_javascript=True)
@@ -185,7 +187,8 @@ def test_posts_snapshots_to_the_local_percy_server(self):
185187
s1 = httpretty.latest_requests()[2].parsed_body
186188
self.assertEqual(s1['name'], 'Snapshot 1')
187189
self.assertEqual(s1['url'], 'http://localhost:8000/')
188-
self.assertEqual(s1['dom_snapshot'], { 'cookies': expected_cookies, 'html': '<html><head></head><body>Snapshot Me</body></html>'})
190+
self.assertEqual(s1['dom_snapshot'], { 'cookies': expected_cookies,
191+
'html': '<html><head></head><body>Snapshot Me</body></html>'})
189192
self.assertRegex(s1['client_info'], r'percy-selenium-python/\d+')
190193
self.assertRegex(s1['environment_info'][0], r'selenium/\d+')
191194
self.assertRegex(s1['environment_info'][1], r'python/\d+')
@@ -208,7 +211,8 @@ def test_posts_snapshots_to_the_local_percy_server_for_sync(self):
208211
s1 = httpretty.latest_requests()[2].parsed_body
209212
self.assertEqual(s1['name'], 'Snapshot 1')
210213
self.assertEqual(s1['url'], 'http://localhost:8000/')
211-
self.assertEqual(s1['dom_snapshot'], { 'html': '<html><head></head><body>Snapshot Me</body></html>', 'cookies': [] })
214+
self.assertEqual(s1['dom_snapshot'], {
215+
'html': '<html><head></head><body>Snapshot Me</body></html>', 'cookies': [] })
212216
self.assertRegex(s1['client_info'], r'percy-selenium-python/\d+')
213217
self.assertRegex(s1['environment_info'][0], r'selenium/\d+')
214218
self.assertRegex(s1['environment_info'][1], r'python/\d+')
@@ -218,17 +222,22 @@ def test_posts_snapshots_to_the_local_percy_server_for_sync(self):
218222
self.assertEqual(s2['enable_javascript'], True)
219223
self.assertEqual(s2['sync'], True)
220224
self.assertEqual(response, data_object)
221-
225+
222226
def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_capture(self):
223227
mock_healthcheck(widths = { "config": [375, 1280], "mobile": [390]})
224228
mock_snapshot()
225229
dom_string = '<html><head></head><body>Snapshot Me</body></html>'
226230
self.driver.add_cookie({'name': 'foo', 'value': 'bar'})
227-
expected_cookies = [{'name': 'foo', 'value': 'bar', 'path': '/', 'domain': 'localhost', 'secure': False, 'httpOnly': False, 'sameSite': 'None'}]
228-
expected_dom_snapshot = [{ 'cookies': expected_cookies, 'html': dom_string, 'width': 1280 }, { 'cookies': expected_cookies, 'html': dom_string, 'width': 390 }, { 'cookies': expected_cookies, 'html': dom_string, 'width': 375 }]
231+
expected_cookies = [{'name': 'foo', 'value': 'bar', 'path': '/', 'domain': 'localhost',
232+
'secure': False, 'httpOnly': False, 'sameSite': 'None'}]
233+
expected_dom_snapshot = [
234+
{ 'cookies': expected_cookies, 'html': dom_string, 'width': 1280 },
235+
{ 'cookies': expected_cookies, 'html': dom_string, 'width': 390 },
236+
{ 'cookies': expected_cookies, 'html': dom_string, 'width': 375 }
237+
]
229238

230239
percy_snapshot(self.driver, 'Snapshot 1', responsiveSnapshotCapture = True)
231-
percy_snapshot(self.driver, 'Snapshot 2', responsive_snapshot_capture = True, widths = [765])
240+
percy_snapshot(self.driver, 'Snapshot 2', responsive_snapshot_capture=True, widths=[765])
232241
percy_snapshot(self.driver, 'Snapshot 3', responsive_snapshot_capture = True, width = 820)
233242

234243
self.assertEqual(httpretty.last_request().path, '/percy/snapshot')
@@ -243,22 +252,33 @@ def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_captu
243252

244253
s2 = httpretty.latest_requests()[3].parsed_body
245254
self.assertEqual(s2['name'], 'Snapshot 2')
246-
self.assertEqual(s2['dom_snapshot'], [{ 'cookies': expected_cookies, 'html': dom_string, 'width': 765 }, { 'html': dom_string, 'cookies': expected_cookies, 'width': 390 }])
255+
self.assertEqual(s2['dom_snapshot'], [
256+
{ 'cookies': expected_cookies, 'html': dom_string, 'width': 765 },
257+
{ 'html': dom_string, 'cookies': expected_cookies, 'width': 390 }
258+
])
247259

248260
s3 = httpretty.latest_requests()[4].parsed_body
249261
self.assertEqual(s3['name'], 'Snapshot 3')
250-
self.assertEqual(s3['dom_snapshot'], [{ 'cookies': expected_cookies, 'html': dom_string, 'width': 820 }, { 'html': dom_string, 'cookies': expected_cookies, 'width': 390 }])
262+
self.assertEqual(s3['dom_snapshot'], [
263+
{ 'cookies': expected_cookies, 'html': dom_string, 'width': 820 },
264+
{ 'html': dom_string, 'cookies': expected_cookies, 'width': 390 }
265+
])
251266

252267
@patch('selenium.webdriver.Chrome')
253-
def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_capture_with_cdp(self, MockChrome):
268+
def test_posts_snapshots_to_the_local_percy_server_for_responsive_dom_chrome(self, MockChrome):
254269
driver = MockChrome.return_value
255-
driver.execute_script.side_effect = ['', '', { 'html': 'some_dom' }, { 'html': 'some_dom_1' }]
270+
driver.execute_script.side_effect = [
271+
'', '', { 'html': 'some_dom' }, { 'html': 'some_dom_1' }
272+
]
256273
driver.get_cookies.return_value = ''
257274
driver.execute_cdp_cmd.return_value = ''
258275
driver.get_window_size.return_value = { 'height': 400, 'width': 800 }
259276
mock_healthcheck(widths = { "config": [375], "mobile": [390]})
260277
mock_snapshot()
261-
expected_dom_snapshot = [{ 'cookies': '', 'html': 'some_dom', 'width': 600 }, { 'cookies': '', 'html': 'some_dom_1', 'width': 390 }]
278+
expected_dom_snapshot = [
279+
{ 'cookies': '', 'html': 'some_dom', 'width': 600 },
280+
{ 'cookies': '', 'html': 'some_dom_1', 'width': 390 }
281+
]
262282

263283
with patch.object(driver, 'current_url', 'http://localhost:8000/'):
264284
with patch.object(driver, 'capabilities', new={ 'browserName': 'chrome' }):
@@ -283,7 +303,8 @@ def test_has_a_backwards_compatible_function(self):
283303
s1 = httpretty.latest_requests()[2].parsed_body
284304
self.assertEqual(s1['name'], 'Snapshot')
285305
self.assertEqual(s1['url'], 'http://localhost:8000/')
286-
self.assertEqual(s1['dom_snapshot'], { 'html': '<html><head></head><body>Snapshot Me</body></html>', 'cookies': [] })
306+
self.assertEqual(s1['dom_snapshot'], {
307+
'html': '<html><head></head><body>Snapshot Me</body></html>', 'cookies': [] })
287308

288309
def test_handles_snapshot_errors(self):
289310
mock_healthcheck(session_type="web")
@@ -294,7 +315,8 @@ def test_handles_snapshot_errors(self):
294315
percy_snapshot(self.driver, 'Snapshot 1')
295316

296317
mock_print.assert_any_call(f'{LABEL} Could not take DOM snapshot "Snapshot 1"')
297-
self.assertEqual(httpretty.latest_requests()[3].parsed_body, { 'message': f'{LABEL} Could not take DOM snapshot "Snapshot 1"', 'level': 'info' })
318+
self.assertEqual(httpretty.latest_requests()[3].parsed_body, {
319+
'message': f'{LABEL} Could not take DOM snapshot "Snapshot 1"', 'level': 'info' })
298320
self.assertEqual(len(httpretty.latest_requests()), 5)
299321

300322
def test_raise_error_poa_token_with_snapshot(self):
@@ -461,8 +483,9 @@ def test_handles_screenshot_errors(self):
461483
percy_screenshot(self.driver, 'Snapshot 1')
462484

463485
mock_print.assert_any_call(f'{LABEL} Could not take Screenshot "Snapshot 1"')
464-
465-
self.assertEqual(httpretty.latest_requests()[2].parsed_body, { 'message': f'{LABEL} Could not take Screenshot "Snapshot 1"', 'level': 'info' })
486+
487+
self.assertEqual(httpretty.latest_requests()[2].parsed_body, {
488+
'message': f'{LABEL} Could not take Screenshot "Snapshot 1"', 'level': 'info' })
466489

467490
def test_raise_error_web_token_with_screenshot(self):
468491
mock_healthcheck(session_type="web")

0 commit comments

Comments
 (0)