21
21
LABEL = '[\u001b [35m' + ('percy:python' if PERCY_DEBUG else 'percy' ) + '\u001b [39m]'
22
22
CDP_SUPPORT_SELENIUM = (str (SELENIUM_VERSION )[0 ].isdigit () and int (
23
23
str (SELENIUM_VERSION )[0 ]) >= 4 ) if SELENIUM_VERSION else False
24
- fetched_widths = {}
24
+ eligible_widths = {}
25
25
26
26
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 )
30
34
31
35
# Check if Percy is enabled, caching the result so it is only checked once
32
36
@lru_cache (maxsize = None )
@@ -36,8 +40,8 @@ def is_percy_enabled():
36
40
response .raise_for_status ()
37
41
data = response .json ()
38
42
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' , {})
41
45
42
46
if not data ['success' ]: raise Exception (data ['error' ])
43
47
version = response .headers .get ('x-percy-core-version' )
@@ -66,14 +70,23 @@ def fetch_percy_dom():
66
70
response .raise_for_status ()
67
71
return response .text
68
72
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
+
69
78
@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
+
71
84
# 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 )
75
88
else :
76
- allWidths .extend (fetched_widths .get ('config' , []))
89
+ allWidths .extend (eligible_widths .get ('config' , []))
77
90
return list (set (allWidths ))
78
91
79
92
def change_window_dimension (driver , width , height ):
@@ -84,21 +97,16 @@ def change_window_dimension(driver, width, height):
84
97
driver .set_window_size (width , height )
85
98
86
99
def capture_responsive_dom (driver , cookies , ** kwargs ):
87
- widths = kwargs .get ('widths' ) or []
88
- if 'width' in kwargs :
89
- widths = [kwargs .get ('width' )]
90
-
91
100
# 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 [] ))
93
102
dom_snapshots = []
94
103
window_size = driver .get_window_size ()
95
104
current_width , current_height = window_size ['width' ], window_size ['height' ]
96
105
97
106
for width in widths :
98
107
change_window_dimension (driver , width , current_height )
99
108
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 )
102
110
dom_snapshot ['width' ] = width
103
111
dom_snapshots .append (dom_snapshot )
104
112
@@ -118,20 +126,18 @@ def percy_snapshot(driver, name, **kwargs):
118
126
# Inject the DOM serialization script
119
127
driver .execute_script (fetch_percy_dom ())
120
128
cookies = driver .get_cookies ()
121
- user_agent = driver .execute_script ("return navigator.userAgent;" )
122
129
123
130
# Serialize and capture the DOM
124
131
if kwargs .get ('responsive_snapshot_capture' , False ) or kwargs .get (
125
132
'responsiveSnapshotCapture' , False ):
126
133
dom_snapshot = capture_responsive_dom (driver , cookies , ** kwargs )
127
134
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 )
130
136
131
137
# Post the DOM to the snapshot endpoint with snapshot options and other info
132
138
response = requests .post (f'{ PERCY_CLI_API } /percy/snapshot' , json = {** kwargs , ** {
133
139
'client_info' : CLIENT_INFO ,
134
- 'environment_info' : ENV_INFO + [ user_agent ] ,
140
+ 'environment_info' : ENV_INFO ,
135
141
'dom_snapshot' : dom_snapshot ,
136
142
'url' : driver .current_url ,
137
143
'name' : name
0 commit comments