Mastering Multilingual Mayhem A Guide to Streamlined Language Testing in Your Automation Framework
Ahtisham Ilyas
Top Software Testing Voice | Software Test Engineer | Automation Specialist: Web, Mobile, API | Scrum Fundamental Certified
Introduction
Testing an application in multiple languages can be both essential and challenging. Ensuring the application works seamlessly across different languages is vital for a global audience. This article aims to share an effective approach for achieving this without the headache of managing excessive test classes.
Why It's a Big Deal: The Multilingual Testing Predicament
When an application supports multiple languages, testing it becomes complex. The initial instinct may be to create separate test classes for each language, but this method is not scalable. It increases the codebase size and makes maintenance a tedious task. How can we conduct language-specific testing without cluttering our test framework?
In the Trenches: My Real-World Multilingual Testing Experience
In my recent project, I found myself faced with the challenge of testing an application across multiple languages. My initial approach was to create separate test classes for each language. However, I soon realized that this method would be cumbersome to maintain in the long run.
Realizing the complexity of the situation, I had a discussion with the development team to understand how they were handling multiple languages in the application. Their insights proved invaluable, leading me to devise a more effective testing strategy.
Setting Up a Property Files and Single Test Class
1. Setting Property Files
I decided to create separate property files for each language. Each file contained key-value pairs, where the key remained the same across languages, but the value changed based on the language.
For example, in the English properties file (en.properties):
welcome_message=Welcome
And in the Arabic properties file (ar.properties):
welcome_message=??????
2. Setting the Language Parameter in TestNG
In your TestNG configuration file, specify the language you want to use for the test run.
// if you want to run it for english language
<parameter name="language" value="en"/>
or
// if you want to run it for arbic language
<parameter name="language" value="ar"/>
Configure your TestNG XML file to run tests with multiple languages. In this example, I'm using two languages: English (en) and Arabic (ar).
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="MultilingualTestSuite">
<test name="EnglishLanguageTest">
<parameter name="language" value="en"/>
<classes>
<class name="com.yourpackage.YourTestClass" />
</classes>
</test>
<test name="ArabicLanguageTest">
<parameter name="language" value="ar"/>
<classes>
<class name="com.yourpackage.YourTestClass" />
</classes>
</test>
</suite>
领英推荐
3. Setting Language in the Base Class
In your base test class, read the language parameter from the TestNG configuration and set it up for the test run.
Here's a sample code snippet for the purpose
public String getLanguage() {
return language;
}
public void setLanguage(String parm, ITestContext context) {
this.language = retrieveAllParameters(context).get(parm);
}
private Map<String, String> retrieveAllParameters(ITestContext context) {
// Retrieve test parameters from the TestNG XML file
return context.getCurrentXmlTest().getAllParameters();
}
4. Fetching Language-Specific Text from Property Files
Below is a Java method that demonstrates how to fetch language-specific text from property files. The getLanguageText function takes a key and a language code as parameters and returns the corresponding text based on the language.
public String getProperty(String key) {
String value;
if (lang.contains("en")) {
value = enProperties.getProperty(key);
} else if (lang.contains("ar")) {
value = arProperties.getProperty(key);
} else {
value = "Key not specified";
}
if (value != null) return value;
else throw new RuntimeException("key not specified in the Configuration.properties file.");
}
5. Single Test Class with Parameterization
I then refactored my test classes into a single, parameterized test class. I used TestNG to run the tests and passed the language as a parameter from the TestNG configuration file.
Here's a sample test case
@Test(priority = 0, groups = "regressionTests")
public void testWelcomeMessage(String language) throws InterruptedException {
extentReport.logTestNameAndDescription("Welcome Message Test", "Verify the welcome message based on language");
extentReport.logTestInfo("Fetching the welcome message from the property file based on the language");
PropertyManager propertyManager = new PropertyManager(language);
String actualResult = someClass.performAction();
String expected = propertyManager.getProperty("welcome_message");
extentReport.logStepResult(actualResult);
Assert.assertTrue(actualResult.contains(expectedResult));
}
By doing this, I managed to reduce the codebase significantly, making it easier to maintain. The single test class could now handle multiple languages by merely switching the property files based on the language parameter.
Challenges and a Heads-Up for an Upcoming Topic
Implementing multilingual testing is not without its challenges. Apart from the complexities of maintaining multiple test classes or property files, you may also encounter encoding issues. Encoding challenges are especially common when dealing with non-Latin characters. Stay tuned for an upcoming article where I will dive deep into how language encoding works for multiple languages and how to tackle these issues effectively.
Conclusion
Handling multiple languages in a test automation framework doesn't have to be complicated. The key is to work closely with your development team and find a scalable solution. Using property files for different languages and parameterizing your test classes can make your testing framework much more maintainable and efficient.
#SoftwareTesting #TestAutomation #MultilingualTesting #QualityAssurance #TestNG #AutomationFramework #SDET #TestingBestPractices #TestStrategy #CodingChallenges #TechTalk #ContinuousTesting #DevOps #TeamCollaboration #LocalizationTesting