|
| 1 | +import json |
1 | 2 | import logging
|
2 | 3 | from html import unescape
|
3 | 4 | from typing import List, Optional
|
@@ -295,20 +296,40 @@ def verify_autofill_cc_data_on_hover(
|
295 | 296 | autofill_popup: AutofillPopup object to get element from dropdown.
|
296 | 297 | """
|
297 | 298 | # Get preview data from hovering through the chrome context
|
298 |
| - element = autofill_popup.get_element("cc-preview-form-container") |
| 299 | + try: |
| 300 | + # Attempt to parse the string as JSON |
| 301 | + container = json.loads( |
| 302 | + autofill_popup.get_element("cc-preview-form-container").get_attribute( |
| 303 | + "ac-comment" |
| 304 | + ) |
| 305 | + ) |
| 306 | + except json.JSONDecodeError: |
| 307 | + # If parsing fails, raise ValueError. |
| 308 | + raise ValueError("Given preview data is incomplete.") |
| 309 | + container_data = container.get("fillMessageData", {}).get("profile", {}) |
| 310 | + assert container_data, "No preview data available." |
| 311 | + assert all(field in container_data.keys() for field in self.fields), ( |
| 312 | + "Not all fields present in preview data." |
| 313 | + ) |
299 | 314 |
|
300 |
| - # Get every span element that is a child of the form and is not empty |
301 |
| - children = [ |
302 |
| - x.get_attribute("innerHTML") |
303 |
| - for x in element.find_elements(By.TAG_NAME, "span") |
304 |
| - if len(x.get_attribute("innerHTML").strip()) >= 2 |
| 315 | + # sanitize data |
| 316 | + autofill_data.card_number = autofill_data.card_number[-4:] |
| 317 | + expected_values = [ |
| 318 | + int(val) if val.isnumeric() else val |
| 319 | + for val in autofill_data.__dict__.values() |
305 | 320 | ]
|
306 |
| - |
307 |
| - for expected in children: |
308 |
| - # Check if this value exists in our CreditCardBase object |
309 |
| - assert unescape(expected) in autofill_data.__dict__.values(), ( |
310 |
| - f"Mismatched data: {expected} not in {autofill_data}." |
311 |
| - ) |
| 321 | + for field, value in container_data.items(): |
| 322 | + if field in self.fields: |
| 323 | + value = str(value) |
| 324 | + if field == "cc-number": |
| 325 | + value = value[-4:] |
| 326 | + elif field == "cc-exp-year": |
| 327 | + value = value[-2:] |
| 328 | + value = int(value) if value.isnumeric() else value |
| 329 | + # Check if this value exists in our CreditCardBase object |
| 330 | + assert value in expected_values, ( |
| 331 | + f"Mismatched data: {(field, value)} not in {expected_values}." |
| 332 | + ) |
312 | 333 |
|
313 | 334 | @staticmethod
|
314 | 335 | def extract_credit_card_obj_into_list(
|
@@ -367,6 +388,25 @@ def verify_updated_information(
|
367 | 388 | self.verify_credit_card_form_data(credit_card_sample_data)
|
368 | 389 | return self
|
369 | 390 |
|
| 391 | + def check_cc_autofill_preview_for_field( |
| 392 | + self, |
| 393 | + field_label: str, |
| 394 | + credit_card_sample_data: CreditCardBase, |
| 395 | + autofill_popup: AutofillPopup, |
| 396 | + ): |
| 397 | + """ |
| 398 | + Check that hovering over a field autofill option will prefill the other fields. |
| 399 | +
|
| 400 | + Arguments: |
| 401 | + field_label: str, |
| 402 | + credit_card_sample_data: credit card autofill sample data, |
| 403 | + autofill_popup: AutofillPopup instance |
| 404 | + """ |
| 405 | + self.double_click("form-field", labels=[field_label]) |
| 406 | + autofill_popup.ensure_autofill_dropdown_visible() |
| 407 | + autofill_popup.hover("select-form-option") |
| 408 | + self.verify_autofill_cc_data_on_hover(credit_card_sample_data, autofill_popup) |
| 409 | + |
370 | 410 | def update_cc(
|
371 | 411 | self,
|
372 | 412 | util: Utilities,
|
@@ -539,18 +579,28 @@ def verify_autofill_displayed(self):
|
539 | 579 | def check_autofill_preview_for_field(
|
540 | 580 | self,
|
541 | 581 | field_label: str,
|
542 |
| - autofill_data, |
| 582 | + address_sample_data, |
543 | 583 | autofill_popup,
|
544 | 584 | util: Utilities,
|
545 | 585 | region: str = None,
|
546 | 586 | ):
|
| 587 | + """ |
| 588 | + Check that hovering over a field autofill option will prefill the other fields. |
| 589 | +
|
| 590 | + Arguments: |
| 591 | + field_label: str, |
| 592 | + address_sample_data: address autofill sample data, |
| 593 | + autofill_popup: AutofillPopup instance |
| 594 | + util: Utilities instance |
| 595 | + region: country code in use |
| 596 | + """ |
547 | 597 | # Skip fields that don't appear in the dropdown for certain regions.
|
548 | 598 | if field_label == "address-level1" and region in ["DE", "FR"]:
|
549 | 599 | return
|
550 | 600 | self.double_click("form-field", labels=[field_label])
|
551 | 601 | autofill_popup.ensure_autofill_dropdown_visible()
|
552 | 602 | autofill_popup.hover("select-form-option")
|
553 |
| - self.verify_autofill_data_on_hover(autofill_data, autofill_popup, util) |
| 603 | + self.verify_autofill_data_on_hover(address_sample_data, autofill_popup, util) |
554 | 604 |
|
555 | 605 | def send_keys_to_element(self, name: str, label: str, keys: str):
|
556 | 606 | """
|
|
0 commit comments