Skip to content

Commit a41c679

Browse files
invert field mapping
1 parent f324197 commit a41c679

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

modules/page_object_autofill.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def fill_and_submit(self, data_object: CreditCardBase | AutofillAddressBase | di
5858
raise NotImplementedError(
5959
"Method should only be called in inherited classes."
6060
)
61-
for field_name, attr_name in self.field_mapping.items():
61+
for attr_name, field_name in self.field_mapping.items():
6262
value = (
6363
getattr(data_object, attr_name, None)
6464
if not isinstance(data_object, dict)
@@ -98,7 +98,7 @@ def verify_form_data(self, sample_data: CreditCardBase | AutofillAddressBase):
9898
raise NotImplementedError(
9999
"Method should only be called in inherited classes."
100100
)
101-
for field_name, attr_name in self.field_mapping.items():
101+
for attr_name, field_name in self.field_mapping.items():
102102
if field_name != "cc-csc":
103103
expected_value = getattr(sample_data, attr_name, None)
104104
self.element_attribute_contains(
@@ -228,7 +228,7 @@ def verify_all_fields_cleared(self):
228228
"""
229229
Verifies that all autofill fields are empty.
230230
"""
231-
for field_name in self.field_mapping.keys():
231+
for field_name in self.fields:
232232
value = self.get_element("form-field", labels=[field_name]).get_attribute(
233233
"value"
234234
)
@@ -392,15 +392,16 @@ def generate_field_data(
392392
if self.__class__ == CreditCardFill
393393
else self.util.fake_autofill_data
394394
)
395+
inverted_mapping = {v: k for k, v in self.field_mapping.items()}
395396
new_sample_data_value = getattr(
396-
faker_method(country_code=region), self.field_mapping[field]
397+
faker_method(country_code=region), inverted_mapping[field]
397398
)
398-
while new_sample_data_value == getattr(sample_data, self.field_mapping[field]):
399+
while new_sample_data_value == getattr(sample_data, inverted_mapping[field]):
399400
new_sample_data_value = getattr(
400-
faker_method(country_code=region), self.field_mapping[field]
401+
faker_method(country_code=region), inverted_mapping[field]
401402
)
402403

403-
setattr(sample_data, self.field_mapping[field], new_sample_data_value)
404+
setattr(sample_data, inverted_mapping[field], new_sample_data_value)
404405
return new_sample_data_value
405406

406407

@@ -410,7 +411,7 @@ class AddressFill(Autofill):
410411
"""
411412

412413
URL_TEMPLATE = "https://mozilla.github.io/form-fill-examples/basic.html"
413-
414+
# Element Selectors
414415
fields = [
415416
"name",
416417
"organization",
@@ -422,17 +423,17 @@ class AddressFill(Autofill):
422423
"email",
423424
"tel",
424425
]
425-
426+
# Class Attributes: Element Selectors
426427
field_mapping = {
427428
"name": "name",
428429
"organization": "organization",
429-
"street-address": "street_address",
430-
"address-level2": "address_level_2",
431-
"address-level1": "address_level_1",
432-
"postal-code": "postal_code",
433-
"country": "country_code",
430+
"street_address": "street-address",
431+
"address_level_2": "address-level2",
432+
"address_level_1": "address-level1",
433+
"postal_code": "postal-code",
434+
"country_code": "country",
434435
"email": "email",
435-
"tel": "telephone",
436+
"telephone": "tel",
436437
}
437438

438439
def __init__(self, driver: Firefox, **kwargs):
@@ -476,13 +477,15 @@ class CreditCardFill(Autofill):
476477
"""
477478

478479
URL_TEMPLATE = "https://mozilla.github.io/form-fill-examples/basic_cc.html"
480+
# Element Selectors
479481
fields = ["cc-name", "cc-number", "cc-exp-month", "cc-exp-year"]
482+
# Class Attributes: Element Selectors
480483
field_mapping = {
481-
"cc-name": "name",
482-
"cc-number": "card_number",
483-
"cc-exp-month": "expiration_month",
484-
"cc-exp-year": "expiration_year",
485-
"cc-csc": "cvv",
484+
"name": "cc-name",
485+
"card_number": "cc-number",
486+
"expiration_month": "cc-exp-month",
487+
"expiration_year": "cc-exp-year",
488+
"cvv": "cc-csc",
486489
}
487490

488491
def __init__(self, driver: Firefox, **kwargs):

0 commit comments

Comments
 (0)