Skip to content

Commit bf00c0f

Browse files
form autofill initial refactor
1 parent fab250f commit bf00c0f

8 files changed

+228
-109
lines changed

modules/browser_object_autofill_popup.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def click_doorhanger_button(self, button_type: str) -> BasePage:
4141
return self
4242

4343
# Interaction with form options
44+
@BasePage.context_chrome
4445
def click_autofill_form_option(self) -> BasePage:
4546
"""Clicks the credit card or address selection in the autofill panel"""
46-
with self.driver.context(self.driver.CONTEXT_CHROME):
47-
self.get_element("select-form-option").click()
47+
self.get_element("select-form-option").click()
4848
return self
4949

5050
def click_clear_form_option(self) -> BasePage:
@@ -56,49 +56,49 @@ def get_doorhanger_cc_number(self) -> str:
5656
"""Retrieves the last 4 digits of the credit card from the doorhanger popup"""
5757
return self.get_element("doorhanger-cc-number").text
5858

59+
@BasePage.context_chrome
5960
def get_cc_doorhanger_data(self, selector: str) -> str:
6061
"""
6162
get text for the credit card doorhanger data.
6263
"""
63-
with self.driver.context(self.driver.CONTEXT_CHROME):
64-
return self.get_element(selector).text
64+
return self.get_element(selector).text
6565

6666
# Interaction with autocomplete list elements
67+
@BasePage.context_chrome
6768
def get_nth_element(self, index: str | int) -> WebElement:
6869
"""
6970
Get the nth element from the autocomplete list
7071
Parameters: index (str): The index of the element to retrieve (1-based)
7172
Returns: WebElement: The nth element in the autocomplete list
7273
"""
73-
with self.driver.context(self.driver.CONTEXT_CHROME):
74-
return self.wait.until(
75-
EC.visibility_of_element_located(
76-
(
77-
"css selector",
78-
f".autocomplete-richlistbox .autocomplete-richlistitem:nth-child({index})",
79-
)
74+
return self.wait.until(
75+
EC.visibility_of_element_located(
76+
(
77+
"css selector",
78+
f".autocomplete-richlistbox .autocomplete-richlistitem:nth-child({index})",
8079
)
8180
)
81+
)
8282

83+
@BasePage.context_chrome
8384
def select_nth_element(self, index: int):
8485
"""
8586
Select the nth element from the autocomplete list
8687
Arguments:
8788
index (int): The index of the element to retrieve (1-based)
8889
"""
89-
with self.driver.context(self.driver.CONTEXT_CHROME):
90-
self.expect(
91-
EC.visibility_of(
92-
self.get_element("select-form-option-by-index", labels=[str(index)])
93-
)
90+
self.expect(
91+
EC.visibility_of(
92+
self.get_element("select-form-option-by-index", labels=[str(index)])
9493
)
95-
self.get_element("select-form-option-by-index", labels=[str(index)]).click()
94+
)
95+
self.get_element("select-form-option-by-index", labels=[str(index)]).click()
9696

97+
@BasePage.context_chrome
9798
def get_primary_value(self, element: WebElement) -> str:
9899
"""
99100
Get the primary value from the autocomplete element
100101
Parameters: element (WebElement): The autocomplete element from which to retrieve the primary value
101102
Returns: str: The primary value extracted from the element's attribute
102103
"""
103-
with self.driver.context(self.driver.CONTEXT_CHROME):
104-
return element.get_attribute("ac-value")
104+
return element.get_attribute("ac-value")

modules/page_object_autofill.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,13 @@ def save_information_basic(self, autofill_info: AutofillAddressBase):
525525

526526
self.click_form_button("submit")
527527

528+
@BasePage.context_chrome
528529
def verify_autofill_displayed(self):
529530
"""
530531
Verifies that the autofill suggestions are displayed.
531532
"""
532-
with self.driver.context(self.driver.CONTEXT_CHROME):
533-
element = self.get_element("select-address")
534-
self.expect(EC.visibility_of(element))
533+
element = self.get_element("select-address")
534+
self.expect(EC.visibility_of(element))
535535

536536
def check_autofill_preview_for_field(
537537
self,

modules/page_object_prefs.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,48 @@ def verify_cc_json(
7373
cc_info_json: dict
7474
The dictionary that is the json representation of the extracted information from a web page
7575
credit_card_fill_obj: CreditCardBase
76-
The object that contains all of the generated information
76+
The object that contains all the generated information
7777
"""
7878
assert cc_info_json["name"] == credit_card_fill_obj.name
7979
assert cc_info_json["number"][-4:] == credit_card_fill_obj.card_number[-4:]
8080
assert int(cc_info_json["month"]) == int(credit_card_fill_obj.expiration_month)
8181
return self
8282

83+
def verify_cc_edit_saved_payments_profile(
84+
self, credit_card_fill_obj: CreditCardBase
85+
):
86+
"""
87+
Verify saved payment profile data is the same as the generated fake credit_card_fill_obj data.
88+
Make sure cvv is not displayed.
89+
90+
Arguments:
91+
credit_card_fill_obj: CreditCardBase
92+
The object that contains all the generated information
93+
"""
94+
self.switch_to_edit_saved_payments_popup_iframe()
95+
form_container = self.get_element("form-container")
96+
input_elements = form_container.find_elements(By.TAG_NAME, "input")
97+
for element in input_elements:
98+
field_name = element.get_attribute("id")
99+
if field_name.startswith("cc"):
100+
field_value = element.get_attribute("value")
101+
assert field_value in credit_card_fill_obj.__dict__.values(), (
102+
f"{field_name} not found in generated data."
103+
)
104+
assert field_value != credit_card_fill_obj.cvv, "CVV is displayed."
105+
select_elements = form_container.find_elements(By.TAG_NAME, "select")
106+
for element in select_elements:
107+
field_name = element.get_attribute("id")
108+
if field_name.startswith("cc"):
109+
val = Select(element)
110+
# Only get the last two digits
111+
field_value = val.first_selected_option.get_attribute("value")[-2:]
112+
assert field_value in credit_card_fill_obj.__dict__.values(), (
113+
f"{field_name} not found in generated data."
114+
)
115+
assert field_value != credit_card_fill_obj.cvv, "CVV is displayed."
116+
return self
117+
83118
def press_button_get_popup_dialog_iframe(self, button_label: str) -> WebElement:
84119
"""
85120
Returns the iframe object for the dialog panel in the popup after pressing some button that triggers a popup
@@ -233,10 +268,28 @@ def get_saved_payments_popup_iframe(self) -> WebElement:
233268
"""
234269
Returns the iframe object for the dialog panel in the popup
235270
"""
236-
self.get_element("prefs-button", labels=["Saved payment methods"]).click()
271+
self.open_saved_payments_list()
237272
iframe = self.get_element("browser-popup")
238273
return iframe
239274

275+
def open_saved_payments_list(self):
276+
"""
277+
Open saved payments dialog panel
278+
"""
279+
self.get_element("prefs-button", labels=["Saved payment methods"]).click()
280+
return self
281+
282+
def click_edit_saved_payment(self):
283+
"""
284+
Click on edit button on saved payments dialog panel
285+
"""
286+
edit_button = self.get_element(
287+
"panel-popup-button", labels=["autofill-manage-edit-button"]
288+
)
289+
self.expect(EC.element_to_be_clickable(edit_button))
290+
edit_button.click()
291+
return self
292+
240293
def switch_to_saved_payments_popup_iframe(self) -> BasePage:
241294
"""
242295
Switch to saved payments popup frame.
@@ -245,6 +298,14 @@ def switch_to_saved_payments_popup_iframe(self) -> BasePage:
245298
self.driver.switch_to.frame(saved_payments)
246299
return self
247300

301+
def switch_to_edit_saved_payments_popup_iframe(self) -> BasePage:
302+
"""
303+
Switch to form iframe to edit saved payments.
304+
"""
305+
self.switch_to_default_frame()
306+
self.switch_to_iframe(2)
307+
return self
308+
248309
def update_cc_field_panel(self, num_tabs: int, new_info: str) -> BasePage:
249310
"""
250311
Updates a field in the credit card popup panel in about:prefs by pressing the number of tabs and sending the new information

tests/form_autofill/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import pytest
22

3+
from modules.browser_object_autofill_popup import AutofillPopup
4+
from modules.page_object_autofill import AddressFill, CreditCardFill
5+
from modules.page_object_prefs import AboutPrefs
6+
from modules.util import Utilities
7+
38

49
@pytest.fixture()
510
def suite_id():
@@ -20,3 +25,33 @@ def prefs_list(add_to_prefs_list: dict):
2025
@pytest.fixture()
2126
def add_to_prefs_list():
2227
return []
28+
29+
30+
@pytest.fixture()
31+
def address_autofill(driver):
32+
yield AddressFill(driver)
33+
34+
35+
@pytest.fixture()
36+
def autofill_popup(driver):
37+
yield AutofillPopup(driver)
38+
39+
40+
@pytest.fixture()
41+
def util():
42+
yield Utilities()
43+
44+
45+
@pytest.fixture()
46+
def about_prefs_privacy(driver):
47+
yield AboutPrefs(driver, category="privacy")
48+
49+
50+
@pytest.fixture()
51+
def about_prefs(driver):
52+
yield AboutPrefs(driver)
53+
54+
55+
@pytest.fixture()
56+
def credit_card_autofill(driver):
57+
yield CreditCardFill(driver)

tests/form_autofill/test_address_autofill_attribute.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,35 @@ def test_case():
1313
return "122359"
1414

1515

16-
def test_address_attribute_selection(driver: Firefox):
16+
def test_address_attribute_selection(
17+
driver: Firefox,
18+
address_autofill: AddressFill,
19+
autofill_popup: AutofillPopup,
20+
util: Utilities,
21+
):
1722
"""
1823
C122359 - This test verifies that after filling the autofill fields and saving the data, hovering over the first
1924
item in the dropdown ensures that the actual value matches the expected value.
25+
26+
Arguments:
27+
address_autofill: AddressFill instance
28+
autofill_popup: AutofillPopup instance
29+
util: Utilities instance
2030
"""
21-
# Instantiate objects and open the navigation page
22-
address_form_fields = AddressFill(driver).open()
23-
autofill_popup_panel = AutofillPopup(driver)
24-
util = Utilities()
31+
# open the navigation page
32+
address_autofill.open()
2533

2634
# Create fake data, fill in the form, and press submit and save on the doorhanger
2735
autofill_sample_data = util.fake_autofill_data(COUNTRY_CODE)
28-
address_form_fields.save_information_basic(autofill_sample_data)
29-
autofill_popup_panel.click_doorhanger_button("save")
36+
address_autofill.save_information_basic(autofill_sample_data)
37+
autofill_popup.click_doorhanger_button("save")
3038

3139
# Double-click on the name field to trigger the autocomplete dropdown
32-
address_form_fields.click_on("form-field", labels=["street-address"])
33-
address_form_fields.click_on("form-field", labels=["street-address"])
40+
address_autofill.double_click("form-field", labels=["street-address"])
3441

3542
# Get the first element from the autocomplete dropdown
36-
first_item = autofill_popup_panel.get_nth_element(1)
37-
actual_value = autofill_popup_panel.hover(first_item).get_primary_value(first_item)
43+
first_item = autofill_popup.get_nth_element(1)
44+
actual_value = autofill_popup.hover(first_item).get_primary_value(first_item)
3845

3946
# Get the primary value (street address) from the first item in the dropdown and assert that the actual value
4047
# matches the expected value

tests/form_autofill/test_autofill_cc_cvv.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,46 @@ def test_case():
1515
return "122399"
1616

1717

18-
def test_autofill_cc_cvv(driver: Firefox):
18+
def test_autofill_cc_cvv(
19+
driver: Firefox,
20+
credit_card_autofill: CreditCardFill,
21+
autofill_popup: AutofillPopup,
22+
util: Utilities,
23+
about_prefs_privacy: AboutPrefs,
24+
):
1925
"""
2026
C122399, Test form autofill CC CVV number
27+
28+
Arguments:
29+
about_prefs_privacy: AboutPrefs instance
30+
credit_card_autofill: CreditCardFill instance
31+
autofill_popup: AutofillPopup instance
32+
util: Utilities instance
2133
"""
22-
# instantiate objects
23-
credit_card_autofill = CreditCardFill(driver).open()
24-
autofill_popup = AutofillPopup(driver)
25-
util = Utilities()
26-
about_prefs_obj = AboutPrefs(driver, category="privacy")
27-
browser_action_obj = BrowserActions(driver)
28-
29-
# create fake data, fill it in and press submit and save on the doorhanger
34+
35+
# open credit card autofill page
36+
credit_card_autofill.open()
37+
38+
# create fake data, fill it in and press submit and save on the door hanger
3039
credit_card_sample_data = util.fake_credit_card_data()
3140
credit_card_autofill.fill_credit_card_info(credit_card_sample_data)
3241
cvv = credit_card_sample_data.cvv
3342
autofill_popup.click_doorhanger_button("save")
3443

3544
# navigate to prefs
45+
about_prefs_privacy.open()
46+
about_prefs_privacy.switch_to_saved_payments_popup_iframe()
47+
48+
# Get the saved CC data (first entry)
49+
cc_profile = about_prefs_privacy.get_all_saved_cc_profiles()[0]
50+
cc_info_json = json.loads(cc_profile.get_dom_attribute("data-l10n-args"))
51+
52+
# Compare input CC data with saved CC data
53+
about_prefs_privacy.verify_cc_json(cc_info_json, credit_card_sample_data)
54+
55+
# Click on saved address and edit
56+
cc_profile.click()
57+
about_prefs_privacy.click_edit_saved_payment()
3658

37-
about_prefs_obj.open()
38-
iframe = about_prefs_obj.press_button_get_popup_dialog_iframe(
39-
"Saved payment methods"
40-
)
41-
browser_action_obj.switch_to_iframe_context(iframe)
42-
43-
# Select the saved cc
44-
saved_profile = about_prefs_obj.get_element("cc-saved-options")
45-
info_string = saved_profile.get_attribute("data-l10n-args")
46-
cc_info_json_original = json.loads(info_string)
47-
logging.info(f"Extracted Original data: {cc_info_json_original}")
48-
saved_profile.click()
49-
50-
# Click on edit
51-
about_prefs_obj.get_element(
52-
"panel-popup-button", labels=["autofill-manage-edit-button"]
53-
).click()
54-
55-
# Verify that CVV number is not saved under CC profile
56-
element = about_prefs_obj.get_element("cc-saved-options", multiple=True)
57-
cvv_not_displayed = not any(cvv in element.text for element in element)
58-
assert cvv_not_displayed, "CVV is displayed."
59+
# Verify that CVV number is not saved under CC profile but the rest of the data is saved
60+
about_prefs_privacy.verify_cc_edit_saved_payments_profile(credit_card_sample_data)

0 commit comments

Comments
 (0)