Motivation
We need to externalize all the strings we use in our locators, such as "User order number" or validation methods, such as "Order Created." Why?- It'll make your code more maintainable if that string changes
- It keeps things organized
- Internationalization (i18n)
- Multi-platform automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_strings_generic(language): | |
if (language == 'en_US'): | |
set_strings_en_US() | |
current_language = "English (United States)" | |
elif (language == 'es_MX'): | |
set_strings_ex_MX() | |
current_language = "Español (Mexico)" | |
... | |
def set_strings_en_US(): | |
strings.Login_Here = 'Login Here' | |
strings.No = 'No' | |
strings.No_ = 'No.' | |
strings.NO_WAY_ = 'NO WAY.' | |
strings.Purchase = 'Purchase' | |
strings.Welcome_ = 'Welcome!' | |
... | |
def set_strings_es_MX(): | |
strings.Login_Here = 'Entre aquí' | |
strings.No = 'No' | |
strings.No_ = 'No.' | |
strings.NO_WAY_ = 'DE NINGUNA MANERA.' | |
strings.Purchase = 'Compra' | |
strings.Welcome_ = '¡Bienvenidos!' | |
... |
And for multi-platform automation... (there is a lot more work involved, but this is part of the deal...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_strings_generic(language, platform): | |
if (language == 'en_US'): | |
set_strings_en_US(platform) | |
current_language = "English (United States)" | |
elif (language == 'es_MX'): | |
set_strings_ex_MX(platform) | |
current_language = "Español (Mexico)" | |
... | |
def set_strings_en_US(platform): | |
if (platform == "ios"): | |
strings.System_Settings = 'System Settings' | |
... | |
if (platform == "android"): | |
strings.System_Settings = 'Settings' | |
... | |
if (platform == "chrome"): | |
strings.System_Settings = 'chrome://settings' # whatever... | |
... | |
def set_strings_es_MX(platform): | |
... |
Anyway, the last thing I'll say about externalizing strings is about dynamic strings.
Here's option 1, strings live in the data file and you compose them in the page object class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dataStrings file | |
if (locale == "en_US") | |
final String Order_number_ = "Order number " | |
final String _is_on_its_way_ = " is on its way!" | |
//page class file | |
class MyPage | |
void validateOrderOnItsWay(orderID) | |
String validationString = dataStrings.Order_number_ + orderID + dataStrings._is_on_its_way_ | |
element e = driver.findElement(By.text(validationString)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dataStrings file | |
if (locale == "en_US") | |
String orderNumberIsOnItsWay(orderID) | |
return "Order number " + orderID + " is on its way!" | |
if (locale == "es_CA") | |
String orderNumberIsOnItsWay(orderID) | |
return orderID + " is the number of the order comin' at ya, eh!" | |
// page class file | |
class MyPage | |
void validateOrderOnItsWay(orderID) | |
String validationString = dataStrings.orderNumberIsOnItsWay(orderId) | |
element e = driver.findElement(By.text(validationString)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dataStrings file | |
if (locale == "en_US") | |
final String Order_number_ = "Order number " | |
final String _is_on_its_way_ = " is on its way!" | |
String orderNumberIsOnItsWay(orderID) | |
return Order_number_ + orderID + _is_on_its_way_ | |
if (locale == "es_CA") | |
final String Order_number_ = "" | |
final String _is_on_its_way_ = " is the number of the order comin' at ya, eh!" | |
String orderNumberIsOnItsWay(orderID) | |
return orderID + _is_on_its_way_ | |
//page class file | |
class MyPage | |
void validateOrderOnItsWay(orderID) | |
String validationString = dataStrings.orderNumberIsOnItsWay(orderId) | |
element e = driver.findElement(By.text(validationString)) | |