Skip to content

Commit

Permalink
Merge pull request #432 from mozilla/as/add-new-address
Browse files Browse the repository at this point in the history
Anca / Extend Faker support for DE and FR (demo page only)
  • Loading branch information
soncuteanca authored Jan 31, 2025
2 parents 17d93fa + 9a83dc5 commit deb91d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
from modules.page_object_autofill import AddressFill
from modules.util import Utilities

params = [("US", "US"), ("CA", "CA")]
regions = ["US", "CA", "DE", "FR"]


@pytest.fixture()
def test_case():
return "2886581"


@pytest.mark.parametrize("region, locale", params)
@pytest.mark.parametrize("region", regions)
def test_address_doorhanger_displayed_after_entering_valid_address(
driver: Firefox, region: str, locale: str
driver: Firefox, region: str
):
"""
C2886581 - Verify the Capture Doorhanger is displayed after entering valid Address data
Expand All @@ -33,7 +33,7 @@ def test_address_doorhanger_displayed_after_entering_valid_address(

# Create fake data and fill it in
address_autofill.open()
address_autofill_data = util.fake_autofill_data(locale)
address_autofill_data = util.fake_autofill_data(region)
address_autofill.save_information_basic(address_autofill_data)

# Check "Save Address?" doorhanger appears in the Address bar
Expand Down
1 change: 1 addition & 0 deletions modules/page_object_prefs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
from time import sleep
from typing import List
Expand Down
60 changes: 32 additions & 28 deletions modules/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,39 @@ def __write_contents(self, driver: Firefox, file_name: str):

def create_localized_faker(self, country_code: str):
"""
Given a country code, try to find the associated English locale. Returns the faker object
and whether or not the country code was valid.
Given a country code, creates a Faker instance with the appropriate locale.
Ensures valid Faker locale names are used.
...
Attributes
----------
country_code : str
The two letter country code.
Returns
Returns:
-------
Tuple[Faker, bool]
A tuple where the first element is the faker object, second is a boolean indicated whether or not
the locale is valid.
Optional[Tuple[Faker, bool]] -> (faker_instance, is_valid_locale) or None if invalid.
"""
locale = f"en_{country_code.upper()}"
locale_map = {
"US": "en_US",
"CA": "en_CA",
"DE": "de_DE",
"FR": "fr_FR",
}

# Check if locale exists, otherwise return None
locale = locale_map.get(country_code.upper())

if not locale:
logging.error(
f"Invalid country code `{country_code}`. No faker instance created."
)
return None # No fallback

try:
faker = Faker(locale)
faker.add_provider(internet)
faker.add_provider(misc)
return (faker, True)
return faker, True
except AttributeError:
faker = Faker(locale)
faker.add_provider(internet)
faker.add_provider(misc)
return (faker, False)
logging.error(
f"Invalid locale `{locale}`. Faker instance could not be created."
)
return None

def generate_localized_phone_US_CA(self, fake: Faker) -> str:
"""
Expand Down Expand Up @@ -178,14 +184,7 @@ def generate_localized_phone_US_CA(self, fake: Faker) -> str:

def fake_autofill_data(self, country_code: str) -> AutofillAddressBase:
"""
Given a country code, tries to initialize the locale of the faker and generates fake data
then returns the new AutofillAddressBase object with the fake data.
...
Attributes
----------
country_code : str
The two letter country code, defaults to CA if it is not valid.
Generates fake autofill data for a given country code.
"""
fake, valid_code = self.create_localized_faker(country_code)
name = fake.name()
Expand All @@ -199,7 +198,12 @@ def fake_autofill_data(self, country_code: str) -> AutofillAddressBase:
postal_code = fake.postcode()
country = "CA" if not valid_code else country_code
email = fake.email()
telephone = self.generate_localized_phone_US_CA(fake)

# Use `generate_localized_phone_US_CA` only for US & CA, otherwise use default Faker phone number.
if country in ["US", "CA"]:
telephone = self.generate_localized_phone_US_CA(fake)
else:
telephone = fake.phone_number()

fake_data = AutofillAddressBase(
name=name,
Expand Down

0 comments on commit deb91d7

Please sign in to comment.