|
1 | 1 | import logging
|
2 |
| -from typing import List |
| 2 | +from typing import List, Optional |
3 | 3 |
|
4 | 4 | from selenium.webdriver.support import expected_conditions as EC
|
5 | 5 |
|
@@ -39,6 +39,68 @@ def click_form_button(self, field_name):
|
39 | 39 | """Clicks submit on the form"""
|
40 | 40 | self.click_on("submit-button", labels=[field_name])
|
41 | 41 |
|
| 42 | + def verify_field_highlight( |
| 43 | + self, |
| 44 | + fields_to_test: List[str], |
| 45 | + expected_highlighted_fields: Optional[List[str]] = None, |
| 46 | + extra_fields: Optional[List[str]] = None, |
| 47 | + ): |
| 48 | + """ |
| 49 | + A common method to check which fields have the "yellow highlight". This is used in both CC and Address pages. |
| 50 | + - fields_to_test: The primary list of fields for this page (cc fields, address fields). |
| 51 | + - expected_highlighted_fields: Which ones are expected to be highlighted. Defaults to all in `fields_to_test`. |
| 52 | + - extra_fields: If some pages have extra fields to test (e.g. 'cc-csc'), pass them here. |
| 53 | + """ |
| 54 | + |
| 55 | + if expected_highlighted_fields is None: |
| 56 | + # By default, everything in fields_to_test is expected to be highlighted |
| 57 | + expected_highlighted_fields = fields_to_test[:] |
| 58 | + |
| 59 | + if extra_fields: |
| 60 | + fields_to_actually_check = fields_to_test + extra_fields |
| 61 | + else: |
| 62 | + fields_to_actually_check = fields_to_test |
| 63 | + |
| 64 | + browser_action_obj = BrowserActions(self.driver) |
| 65 | + |
| 66 | + def is_yellow_highlight(rgb_tuple): |
| 67 | + """ |
| 68 | + Returns True if the color tuple is bright yellow-ish. |
| 69 | + """ |
| 70 | + if len(rgb_tuple) == 3: |
| 71 | + r, g, b = rgb_tuple |
| 72 | + else: |
| 73 | + r, g, b, *_ = rgb_tuple |
| 74 | + |
| 75 | + return (r >= 250) and (g >= 250) and (180 < b < 220) |
| 76 | + |
| 77 | + for field_name in fields_to_actually_check: |
| 78 | + # Focus the field so the highlight is visible |
| 79 | + self.click_on("form-field", labels=[field_name]) |
| 80 | + |
| 81 | + # Get all colors in the field |
| 82 | + selector = self.get_selector("form-field", labels=[field_name]) |
| 83 | + colors = browser_action_obj.get_all_colors_in_element(selector) |
| 84 | + logging.info(f"Colors found in '{field_name}': {colors}") |
| 85 | + |
| 86 | + # Check the highlight |
| 87 | + is_field_highlighted = any(is_yellow_highlight(color) for color in colors) |
| 88 | + should_be_highlighted = field_name in expected_highlighted_fields |
| 89 | + |
| 90 | + # Assert based on expectation |
| 91 | + if should_be_highlighted: |
| 92 | + assert is_field_highlighted, ( |
| 93 | + f"Expected yellow highlight on '{field_name}', but none found." |
| 94 | + ) |
| 95 | + logging.info(f"Yellow highlight found in '{field_name}'.") |
| 96 | + else: |
| 97 | + assert not is_field_highlighted, ( |
| 98 | + f"Expected NO yellow highlight on '{field_name}', but found one." |
| 99 | + ) |
| 100 | + logging.info(f"No yellow highlight in '{field_name}', as expected.") |
| 101 | + |
| 102 | + return self |
| 103 | + |
42 | 104 |
|
43 | 105 | class CreditCardFill(Autofill):
|
44 | 106 | """
|
@@ -323,50 +385,15 @@ def verify_clear_form_all_fields(self, autofill_popup_obj: AutofillPopup):
|
323 | 385 |
|
324 | 386 | def verify_field_yellow_highlights(self, expected_highlighted_fields=None):
|
325 | 387 | """
|
326 |
| - Verifies that specified form fields have the expected highlight state |
| 388 | + Reuses the common highlight-check method from the base class. |
| 389 | + We also want to include the "cc-csc" field in the test, so we |
| 390 | + pass it via 'extra_fields'. |
327 | 391 | """
|
328 |
| - if expected_highlighted_fields is None: |
329 |
| - expected_highlighted_fields = self.fields |
330 |
| - |
331 |
| - browser_action_obj = BrowserActions(self.driver) |
332 |
| - |
333 |
| - # Check if a color is yellow-ish |
334 |
| - def is_yellow_highlight(rgb_tuple): |
335 |
| - if len(rgb_tuple) == 3: |
336 |
| - r, g, b = rgb_tuple |
337 |
| - elif len(rgb_tuple) >= 4: |
338 |
| - r, g, b, *_ = rgb_tuple |
339 |
| - else: |
340 |
| - return False |
341 |
| - # Uses a tolerance to detect a yellow highlight |
342 |
| - return r >= 250 and g >= 250 and 180 < b < 220 |
343 |
| - |
344 |
| - all_fields = self.fields + ["cc-csc"] |
345 |
| - |
346 |
| - for field_name in all_fields: |
347 |
| - # Bring the fields into focus |
348 |
| - self.click_on("form-field", labels=[field_name]) |
349 |
| - |
350 |
| - # Get the color of the fields |
351 |
| - selector = self.get_selector("form-field", labels=[field_name]) |
352 |
| - colors = browser_action_obj.get_all_colors_in_element(selector) |
353 |
| - logging.info(f"Colors found in {field_name}: {colors}") |
354 |
| - |
355 |
| - is_field_highlighted = any(is_yellow_highlight(color) for color in colors) |
356 |
| - should_be_highlighted = field_name in expected_highlighted_fields |
357 |
| - |
358 |
| - if should_be_highlighted: |
359 |
| - assert is_field_highlighted, ( |
360 |
| - f"Expected yellow highlight on {field_name}, but none found." |
361 |
| - ) |
362 |
| - logging.info(f"Yellow highlight correctly found in {field_name}.") |
363 |
| - else: |
364 |
| - assert not is_field_highlighted, ( |
365 |
| - f"Expected no yellow highlight on {field_name}, but found one." |
366 |
| - ) |
367 |
| - logging.info(f"No yellow highlight found in {field_name}, as expected.") |
368 |
| - |
369 |
| - return self |
| 392 | + return self.verify_field_highlight( |
| 393 | + fields_to_test=self.fields, |
| 394 | + expected_highlighted_fields=expected_highlighted_fields, |
| 395 | + extra_fields=["cc-csc"], |
| 396 | + ) |
370 | 397 |
|
371 | 398 |
|
372 | 399 | class LoginAutofill(Autofill):
|
@@ -414,6 +441,18 @@ class AddressFill(Autofill):
|
414 | 441 |
|
415 | 442 | URL_TEMPLATE = "https://mozilla.github.io/form-fill-examples/basic.html"
|
416 | 443 |
|
| 444 | + fields = [ |
| 445 | + "name", |
| 446 | + "organization", |
| 447 | + "street-address", |
| 448 | + "address-level2", # city |
| 449 | + "address-level1", # state/province |
| 450 | + "postal-code", |
| 451 | + "country", |
| 452 | + "email", |
| 453 | + "tel", |
| 454 | + ] |
| 455 | + |
417 | 456 | def save_information_basic(self, autofill_info: AutofillAddressBase):
|
418 | 457 | """
|
419 | 458 | Saves information passed in, in the form of an AutofillAddressBase object.
|
@@ -519,6 +558,17 @@ def verify_autofill_data(
|
519 | 558 | f"Mismatch in {field}: Expected '{expected}', but got '{actual}'"
|
520 | 559 | )
|
521 | 560 |
|
| 561 | + def verify_field_yellow_highlights( |
| 562 | + self, fields_to_test=None, expected_highlighted_fields=None |
| 563 | + ): |
| 564 | + if fields_to_test is None: |
| 565 | + fields_to_test = self.fields # By default, test all address fields |
| 566 | + |
| 567 | + return self.verify_field_highlight( |
| 568 | + fields_to_test=fields_to_test, |
| 569 | + expected_highlighted_fields=expected_highlighted_fields, |
| 570 | + ) |
| 571 | + |
522 | 572 |
|
523 | 573 | class TextAreaFormAutofill(Autofill):
|
524 | 574 | """
|
|
0 commit comments