1
+ import errno
2
+ import logging
1
3
import os
4
+ import socket
5
+ from json import load
2
6
from typing import List
3
7
4
8
import pytest
8
12
from modules .page_object_prefs import AboutPrefs
9
13
from modules .util import Utilities
10
14
15
+ current_dir = os .path .dirname (__file__ )
16
+ parent_dir = os .path .dirname (current_dir )
17
+
18
+ LOCALHOST = "127.0.0.1"
19
+ PORT = 8080
20
+
21
+
22
+ def is_port_in_use () -> bool :
23
+ """Check if the port is already open (server running)."""
24
+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
25
+ return s .connect_ex ((LOCALHOST , PORT )) == 0
26
+
27
+
28
+ def get_html_files (live_site , region ):
29
+ address_path_to_site = (
30
+ parent_dir + f"/sites/{ live_site } /{ region } /{ live_site } _ad.html"
31
+ )
32
+ cc_path_to_site = parent_dir + f"/sites/{ live_site } /{ region } /{ live_site } _cc.html"
33
+ address_html_file , cc_html_file = "" , ""
34
+ if os .path .exists (address_path_to_site ) and os .path .exists (cc_path_to_site ):
35
+ with open (address_path_to_site , "r" , encoding = "utf-8" ) as fp :
36
+ address_html_file = fp .read ()
37
+ with open (cc_path_to_site , "r" , encoding = "utf-8" ) as fp :
38
+ cc_html_file = fp .read ()
39
+ return address_html_file , cc_html_file
40
+ return address_html_file , cc_html_file
41
+
11
42
12
43
@pytest .fixture ()
13
44
def region ():
14
45
return os .environ .get ("FX_REGION" , "US" )
15
46
16
47
48
+ @pytest .fixture ()
49
+ def live_site ():
50
+ return os .environ .get ("CM_SITE" , "demo" )
51
+
52
+
17
53
@pytest .fixture ()
18
54
def add_to_prefs_list (region : str ):
19
55
return []
@@ -33,30 +69,196 @@ def prefs_list(add_to_prefs_list: List[tuple[str, str | bool]], region: str):
33
69
34
70
35
71
@pytest .fixture ()
36
- def address_autofill (driver ):
37
- yield AddressFill (driver )
72
+ def ad_site_data (live_site , region ):
73
+ ad_live_site = (
74
+ f"{ live_site } /{ region } /{ live_site } _ad"
75
+ if live_site != "demo"
76
+ else f"{ live_site } /{ live_site } _ad"
77
+ )
78
+ path_to_site = parent_dir + "/constants/"
79
+ with open (path_to_site + ad_live_site + ".json" , "r" ) as fp :
80
+ live_site_data = load (fp )
81
+ # Remove address level 1 for regions other than US and CA
82
+ if region not in {"US" , "CA" } and live_site_data ["field_mapping" ].get (
83
+ "address_level_1"
84
+ ):
85
+ live_site_data ["fields" ].remove (
86
+ live_site_data ["field_mapping" ]["address_level_1" ]
87
+ )
88
+ del live_site_data ["field_mapping" ]["address_level_1" ]
89
+ return live_site_data
90
+
91
+
92
+ @pytest .fixture ()
93
+ def cc_site_data (live_site , region ):
94
+ cc_live_site = (
95
+ f"{ live_site } /{ region } /{ live_site } _cc"
96
+ if live_site != "demo"
97
+ else f"{ live_site } /{ live_site } _cc"
98
+ )
99
+ path_to_site = parent_dir + "/constants/"
100
+ with open (path_to_site + cc_live_site + ".json" , "r" ) as fp :
101
+ live_site_data = load (fp )
102
+ return live_site_data
103
+
104
+
105
+ @pytest .fixture
106
+ def is_live_site (live_site ):
107
+ """Determine if the site is live."""
108
+ return live_site != "demo"
109
+
110
+
111
+ @pytest .fixture (scope = "session" )
112
+ def httpserver_listen_address ():
113
+ """Set port for local http server"""
114
+ return LOCALHOST , PORT
115
+
116
+
117
+ @pytest .fixture ()
118
+ def serve_live_site (is_live_site , live_site , region , request ):
119
+ """Serve the live site only if needed."""
120
+ if not is_live_site or is_port_in_use ():
121
+ return
122
+ # only serve content if url is already not served.
123
+ try :
124
+ ad_html_file , cc_html_file = get_html_files (live_site , region )
125
+ http_server = request .getfixturevalue ("httpserver" )
126
+ http_server .expect_request (f"/{ live_site } _ad.html" ).respond_with_data (
127
+ ad_html_file , content_type = "text/html"
128
+ )
129
+ http_server .expect_request (f"/{ live_site } _cc.html" ).respond_with_data (
130
+ cc_html_file , content_type = "text/html"
131
+ )
132
+ except OSError as e :
133
+ if e == errno .EADDRINUSE :
134
+ logging .info (f"{ live_site } already served." )
135
+ finally :
136
+ return
137
+
138
+
139
+ @pytest .fixture ()
140
+ def ad_form_field (ad_site_data ):
141
+ selector = ad_site_data .get ("form_field" )
142
+ return (
143
+ {"form-field" : {"selectorData" : selector , "strategy" : "css" , "groups" : []}}
144
+ if selector
145
+ else {}
146
+ )
147
+
148
+
149
+ @pytest .fixture ()
150
+ def cc_form_field (cc_site_data ):
151
+ selector = cc_site_data .get ("form_field" )
152
+ return (
153
+ {"form-field" : {"selectorData" : selector , "strategy" : "css" , "groups" : []}}
154
+ if selector
155
+ else {}
156
+ )
157
+
158
+
159
+ @pytest .fixture ()
160
+ def address_autofill (driver , ad_site_data , ad_form_field , serve_live_site ):
161
+ af = AddressFill (
162
+ driver ,
163
+ url_template = ad_site_data .get ("url" ),
164
+ field_mapping = ad_site_data .get ("field_mapping" ),
165
+ fields = ad_site_data .get ("fields" ),
166
+ )
167
+ af .elements |= ad_form_field
168
+ return af
169
+
170
+
171
+ @pytest .fixture ()
172
+ def credit_card_autofill (driver , cc_site_data , cc_form_field , serve_live_site ):
173
+ cf = CreditCardFill (
174
+ driver ,
175
+ url_template = cc_site_data .get ("url" ),
176
+ field_mapping = cc_site_data .get ("field_mapping" ),
177
+ fields = cc_site_data .get ("fields" ),
178
+ )
179
+ cf .elements |= cc_form_field
180
+ return cf
38
181
39
182
40
183
@pytest .fixture ()
41
184
def autofill_popup (driver ):
42
- yield AutofillPopup (driver )
185
+ return AutofillPopup (driver )
43
186
44
187
45
188
@pytest .fixture ()
46
189
def util ():
47
- yield Utilities ()
190
+ return Utilities ()
48
191
49
192
50
193
@pytest .fixture ()
51
194
def about_prefs_privacy (driver ):
52
- yield AboutPrefs (driver , category = "privacy" )
195
+ return AboutPrefs (driver , category = "privacy" )
53
196
54
197
55
198
@pytest .fixture ()
56
199
def about_prefs (driver ):
57
- yield AboutPrefs (driver )
200
+ return AboutPrefs (driver )
201
+
202
+
203
+ @pytest .fixture ()
204
+ def populate_saved_payments (
205
+ about_prefs_privacy : AboutPrefs , util : Utilities , region : str
206
+ ):
207
+ """Fixture to add cc data through saved payments method."""
208
+ # Go to about:preferences#privacy and open Saved Payment Methods
209
+ about_prefs_privacy .open ()
210
+ about_prefs_privacy .open_and_switch_to_saved_payments_popup ()
211
+
212
+ # Save CC information using fake data
213
+ credit_card_sample_data = util .fake_credit_card_data (region )
214
+
215
+ # Add a new CC profile
216
+ about_prefs_privacy .click_add_on_dialog_element ()
217
+ about_prefs_privacy .add_entry_to_saved_payments (credit_card_sample_data )
218
+ return credit_card_sample_data
219
+
220
+
221
+ @pytest .fixture ()
222
+ def populate_saved_addresses (
223
+ about_prefs_privacy : AboutPrefs , util : Utilities , region : str
224
+ ):
225
+ """Fixture to add cc data through saved payments method."""
226
+ # Go to about:preferences#privacy and open Saved Addresses Methods
227
+ about_prefs_privacy .open ()
228
+ about_prefs_privacy .open_and_switch_to_saved_addresses_popup ()
229
+
230
+ # Save address information using fake data
231
+ address_data_sample_data = util .fake_autofill_data (region )
232
+
233
+ # Add a new address profile
234
+ about_prefs_privacy .click_add_on_dialog_element ()
235
+ about_prefs_privacy .add_entry_to_saved_addresses (address_data_sample_data )
236
+ return address_data_sample_data
237
+
238
+
239
+ @pytest .fixture ()
240
+ def fill_and_save_address (
241
+ address_autofill : AddressFill , ad_site_data , region : str , request
242
+ ):
243
+ """
244
+ Fixture to populate address entry depending on whether the url is a live site.
245
+ If live site, populate data through about:prefs, if not fill directly through page.
246
+ """
247
+ if ad_site_data .get ("url" ):
248
+ return request .getfixturevalue ("populate_saved_addresses" )
249
+ address_autofill .open ()
250
+ return address_autofill .fill_and_save (region )
58
251
59
252
60
253
@pytest .fixture ()
61
- def credit_card_autofill (driver ):
62
- yield CreditCardFill (driver )
254
+ def fill_and_save_payments (
255
+ credit_card_autofill : CreditCardFill , cc_site_data , region : str , request
256
+ ):
257
+ """
258
+ Fixture to populate cc entry depending on whether the url is a live site.
259
+ If live site, populate data through about:prefs, if not fill directly through page.
260
+ """
261
+ if cc_site_data .get ("url" ):
262
+ return request .getfixturevalue ("populate_saved_payments" )
263
+ credit_card_autofill .open ()
264
+ return credit_card_autofill .fill_and_save (region )
0 commit comments