Skip to content

Commit 282798d

Browse files
Hani YacoubHani Yacoub
authored andcommitted
Fix phone not matching FR and DE
1 parent 5d676e0 commit 282798d

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

l10n_CM/Unified/test_demo_ad_email_phone_captured_in_doorhanger_and_stored.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,19 @@ def test_demo_ad_email_phone_captured_in_doorhanger_and_stored(
3838

3939
# containing phone field
4040
expected_phone = address_autofill_data.telephone
41-
with driver.context(driver.CONTEXT_CHROME):
42-
actual_phone = autofill_popup.get_element("address-doorhanger-phone").text
43-
normalize_expected = util.normalize_phone_number(expected_phone)
44-
normalized_actual = util.normalize_phone_number(actual_phone)
45-
assert normalized_actual == normalize_expected
41+
# Skip verification if no phone number isn't provided
42+
if expected_phone:
43+
with driver.context(driver.CONTEXT_CHROME):
44+
actual_phone = autofill_popup.get_element("address-doorhanger-phone").text
45+
46+
normalize_expected = util.normalize_regional_phone_numbers(expected_phone, region)
47+
normalized_actual = util.normalize_regional_phone_numbers(actual_phone, region)
48+
49+
assert normalized_actual == normalize_expected, (
50+
f"Phone number mismatch for {region} | Expected: {normalize_expected}, Got: {normalized_actual}"
51+
)
52+
53+
print(f"DEBUG: {region} | Input: {normalized_actual} | Output: {normalize_expected}") # Debugging print
4654

4755
# Click the "Save" button
4856
autofill_popup.click_doorhanger_button("save")

modules/util.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Utilities:
3232
Methods that may be useful, that have nothing to do with Selenium.
3333
"""
3434

35+
# COUNTRY_CODES = {
36+
# "US": "1", "CA": "1", "FR": "33", "DE": "49", "UK": "44", "JP": "81"
37+
# }
38+
3539
def __init__(self):
3640
self.state_province_abbr = {
3741
# US States
@@ -469,6 +473,59 @@ def get_state_province_abbreviation(self, full_name: str) -> str:
469473
"""
470474
return self.state_province_abbr.get(full_name, "Not Found")
471475

476+
def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
477+
"""
478+
Normalizes a phone number by separating the country prefix and verifying the rest of the number as an integer.
479+
This is used for localization (l10n) regional tests.
480+
Parameters:
481+
-----------
482+
phone : str
483+
The phone number to be normalized.
484+
region : str
485+
The region (country) code to determine the correct country prefix.
486+
Returns:
487+
--------
488+
str
489+
The normalized phone number in the format <country-code><number>.
490+
"""
491+
492+
# Country code mapping for different regions
493+
country_codes = {
494+
"US": "1",
495+
"CA": "1",
496+
"FR": "33",
497+
"DE": "49",
498+
}
499+
500+
# Remove phone number extensions (e.g., "x555" or "ext 555")
501+
phone = re.sub(r"\s*(?:x|ext)\s*\d*$", "", phone, flags=re.IGNORECASE)
502+
503+
# Remove all non-numeric characters
504+
digits = re.sub(r"\D", "", phone)
505+
506+
# Determine country code
507+
country_code = country_codes.get(region, "1") # Default to "1" (US/CA) if region is unknown
508+
local_number = digits
509+
510+
# Check if phone already contains a valid country code
511+
for code in country_codes.values():
512+
if digits.startswith(code):
513+
country_code = code
514+
local_number = digits[len(code):] # Remove country code from local number
515+
break
516+
517+
# Handle leading zero in local numbers (France & Germany)
518+
if region in ["FR", "DE"] and local_number.startswith("0"):
519+
local_number = local_number[1:] # Remove the leading zero
520+
521+
# Validate local number length
522+
if len(local_number) < 6: # Too short to be valid
523+
logging.warning(f"Invalid phone number format: {phone}")
524+
return ""
525+
526+
# Return formatted phone number with correct country code
527+
return f"{country_code}{local_number}"
528+
472529

473530
class BrowserActions:
474531
"""

0 commit comments

Comments
 (0)