1. Introduction
2. What is Localise/Loco?
3. How to use it along with Appium?
4. Conclusions
Introduction:
When it comes to multilingual applications, the need for thorough testing in different languages becomes even more critical. In this blog, we will explore how to combine Appium/Pytest with LOCO to achieve effective test automation in multilingual applications.
What is Localise/Loco?
Loco helps you manage and integrate translations into your software, regardless of what you’re building. Developers can import and export translations in many different file formats, including Android, iOS, Gettext, PHP, JavaScript, JSON, YAML, XML, XLIFF, TMX, and more. A single Loco project can manage the same set of translations across multiple platforms at once.
How to use it along with Appium?
- Management of different languages: With this integration between Appium and Loco, we can verify that our app displays the expected texts in each language.
1. In our conftest.py, we will download the yml file based on the language selected in our test
@pytest.fixture(autouse=True, scope="session")
def download_yaml_file(request):
# Get the language option from the pytest configuration
language = request.config.getoption("language").lower()
# Map the language option to its corresponding language code
languages = {"spanish": "es", "catalan": "ca", "french": "fr", "english": "en"}
language = languages.get(language.lower())
# Get the localization key from the project configuration
loco_key = project_config["loco_key"]
# Construct the URL to download the YAML file for the selected language
url = f"https://localise.biz:443/api/export/locale/{language}.yml?key={loco_key}"
# Define the directory path to save the YAML file
directory_path = f"{CommonUtils.get_project_path()}/utilities/translations"
yaml_file_path = f"{directory_path}/{language}_locale.yml"
# Download the YAML file from the constructed URL
response = requests.get(url)
response.raise_for_status() # Raise an exception for error HTTP statuses
# Check if the directory exists, if not, create it
if not os.path.exists(directory_path):
os.makedirs(directory_path)
logger.info(f"Directory '{directory_path}' created.")
else:
logger.info(f"Directory '{directory_path}' already exists.")
# Write the downloaded YAML content to the file
with open(yaml_file_path, "w") as f:
f.write(response.text)
2. We will retrieve the Loco ID that we want.
def get_id_text_for_language(key_in_loco: str):
"""
Retrieves the text associated with a given key for the currently selected language.
Args:
key_in_loco (str): The key for which the text needs to be retrieved.
Returns:
str: The text associated with the given key for the selected language.
The function determines the selected language from the device information and constructs the file path
to the corresponding locale YAML file. It then loads the YAML file and retrieves the text associated
with the provided key.
"""
language_selected = {
"spanish": "es",
"catalan": "ca",
"english": "en",
"french": "fr",
}.get(os.getenv("LANGUAGE"), "en")
file_path = f"utilities/translations/{language_selected}_locale.yml"
try:
with open(file_path, "r") as file:
data = yaml.safe_load(file)
if key_in_loco not in data:
logger.error(f"Key '{key_in_loco}' not found in {file_path}")
raise KeyError(f"Key '{key_in_loco}' not found in {file_path}")
return data.get(key_in_loco)
except FileNotFoundError:
logger.error(f"File not found: {file_path}")
raise FileNotFoundError
except yaml.YAMLError as exc:
logger.error(f"Error parsing YAML file: {exc}")
return None
3. We create a function to use the Loco ID.
def get_id_text_for_language(key_in_loco: str):
"""
Retrieves the text associated with a given key for the currently selected language.
Args:
key_in_loco (str): The key for which the text needs to be retrieved.
Returns:
str: The text associated with the given key for the selected language.
The function determines the selected language from the device information and constructs the file path
to the corresponding locale YAML file. It then loads the YAML file and retrieves the text associated
with the provided key.
"""
language_selected = {
"spanish": "es",
"catalan": "ca",
"english": "en",
"french": "fr",
}.get(os.getenv("LANGUAGE"), "en")
file_path = f"utilities/translations/{language_selected}_locale.yml"
try:
with open(file_path, "r") as file:
data = yaml.safe_load(file)
if key_in_loco not in data:
logger.error(f"Key '{key_in_loco}' not found in {file_path}")
raise KeyError(f"Key '{key_in_loco}' not found in {file_path}")
return data.get(key_in_loco)
except FileNotFoundError:
logger.error(f"File not found: {file_path}")
raise FileNotFoundError
except yaml.YAMLError as exc:
logger.error(f"Error parsing YAML file: {exc}")
return None
4. Use of the element created through the Loco ID for localization and as an element identifier.
Now we can use the element created from the Loco ID to verify that the localized text is in the app and, in turn, as a unique ID to locate elements.
element_to_verify = create_element_with_loco_id(“welcome.text”)
self.driver.find_element_(* element_to_verify)
Conclusions
In summary, the combination of Appium and Loco enables:
- Automate the verification of localized texts: Ensuring that the correct texts are displayed in each language.
- Simplify translation management: Using Loco to centralize and manage localization files.
- Improve test code maintainability: By using Loco IDs as unique identifiers for elements.
- Facilitate multilingual test setup: Through the dynamic download of YAML files and pytest configuration.