6
6
7
7
import httpretty
8
8
import requests
9
- from selenium .webdriver import Firefox , FirefoxOptions , Chrome
9
+ from selenium .webdriver import Firefox , FirefoxOptions
10
10
from selenium .webdriver .remote .webdriver import WebDriver
11
11
from selenium .webdriver .remote .webelement import WebElement
12
12
from selenium .webdriver .remote .remote_connection import RemoteConnection
@@ -48,7 +48,7 @@ def dummy_method1(self):
48
48
49
49
50
50
# 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 ):
52
52
health_body = { "success" : True }
53
53
health_headers = { 'X-Percy-Core-Version' : '1.0.0' }
54
54
health_status = 200
@@ -64,7 +64,7 @@ def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = {
64
64
if session_type :
65
65
health_body ["type" ] = session_type
66
66
67
- health_body ['widths' ] = widths
67
+ if widths : health_body ['widths' ] = widths
68
68
health_body = json .dumps (health_body )
69
69
httpretty .register_uri (
70
70
httpretty .GET , 'http://localhost:5338/percy/healthcheck' ,
@@ -73,7 +73,8 @@ def mock_healthcheck(fail=False, fail_how='error', session_type=None, widths = {
73
73
status = health_status )
74
74
httpretty .register_uri (
75
75
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 } } };' ,
77
78
status = 200 )
78
79
79
80
def mock_snapshot (fail = False , data = False ):
@@ -175,7 +176,8 @@ def test_posts_snapshots_to_the_local_percy_server(self):
175
176
mock_healthcheck ()
176
177
mock_snapshot ()
177
178
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' }]
179
181
180
182
percy_snapshot (self .driver , 'Snapshot 1' )
181
183
response = percy_snapshot (self .driver , 'Snapshot 2' , enable_javascript = True )
@@ -185,7 +187,8 @@ def test_posts_snapshots_to_the_local_percy_server(self):
185
187
s1 = httpretty .latest_requests ()[2 ].parsed_body
186
188
self .assertEqual (s1 ['name' ], 'Snapshot 1' )
187
189
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>' })
189
192
self .assertRegex (s1 ['client_info' ], r'percy-selenium-python/\d+' )
190
193
self .assertRegex (s1 ['environment_info' ][0 ], r'selenium/\d+' )
191
194
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):
208
211
s1 = httpretty .latest_requests ()[2 ].parsed_body
209
212
self .assertEqual (s1 ['name' ], 'Snapshot 1' )
210
213
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' : [] })
212
216
self .assertRegex (s1 ['client_info' ], r'percy-selenium-python/\d+' )
213
217
self .assertRegex (s1 ['environment_info' ][0 ], r'selenium/\d+' )
214
218
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):
218
222
self .assertEqual (s2 ['enable_javascript' ], True )
219
223
self .assertEqual (s2 ['sync' ], True )
220
224
self .assertEqual (response , data_object )
221
-
225
+
222
226
def test_posts_snapshots_to_the_local_percy_server_for_responsive_snapshot_capture (self ):
223
227
mock_healthcheck (widths = { "config" : [375 , 1280 ], "mobile" : [390 ]})
224
228
mock_snapshot ()
225
229
dom_string = '<html><head></head><body>Snapshot Me</body></html>'
226
230
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
+ ]
229
238
230
239
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 ])
232
241
percy_snapshot (self .driver , 'Snapshot 3' , responsive_snapshot_capture = True , width = 820 )
233
242
234
243
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
243
252
244
253
s2 = httpretty .latest_requests ()[3 ].parsed_body
245
254
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
+ ])
247
259
248
260
s3 = httpretty .latest_requests ()[4 ].parsed_body
249
261
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
+ ])
251
266
252
267
@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 ):
254
269
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
+ ]
256
273
driver .get_cookies .return_value = ''
257
274
driver .execute_cdp_cmd .return_value = ''
258
275
driver .get_window_size .return_value = { 'height' : 400 , 'width' : 800 }
259
276
mock_healthcheck (widths = { "config" : [375 ], "mobile" : [390 ]})
260
277
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
+ ]
262
282
263
283
with patch .object (driver , 'current_url' , 'http://localhost:8000/' ):
264
284
with patch .object (driver , 'capabilities' , new = { 'browserName' : 'chrome' }):
@@ -283,7 +303,8 @@ def test_has_a_backwards_compatible_function(self):
283
303
s1 = httpretty .latest_requests ()[2 ].parsed_body
284
304
self .assertEqual (s1 ['name' ], 'Snapshot' )
285
305
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' : [] })
287
308
288
309
def test_handles_snapshot_errors (self ):
289
310
mock_healthcheck (session_type = "web" )
@@ -294,7 +315,8 @@ def test_handles_snapshot_errors(self):
294
315
percy_snapshot (self .driver , 'Snapshot 1' )
295
316
296
317
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' })
298
320
self .assertEqual (len (httpretty .latest_requests ()), 5 )
299
321
300
322
def test_raise_error_poa_token_with_snapshot (self ):
@@ -461,8 +483,9 @@ def test_handles_screenshot_errors(self):
461
483
percy_screenshot (self .driver , 'Snapshot 1' )
462
484
463
485
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' })
466
489
467
490
def test_raise_error_web_token_with_screenshot (self ):
468
491
mock_healthcheck (session_type = "web" )
0 commit comments