2

Thanks for your time first & foremost.

Problem:

I am automating a product which in the not so distant future will have multilingual support switched on, I have setup a simple resource bundle reading from a .properties file (English.properties, French.properties and so fourth).

In my Page Objects I have some @FindBy notations referencing some linkText for a number of hyperlinks on the pages, e.g: "Forgot Password". This linkText will obviously change when we switch locale.

Do I abandon these @FindBy(linkText = "Forgot Password") locators and use something else available to locate them which won't have an impact on the language.

OR

Is there a means in which I can provide dynamic content in the @FindBy to cater to this? For example:

@FindBy(linkText = (Language.getLanguage().getString("foo"))

I'm curious as to what people do in this problem?

Trying my solution returns the following:

The value for annotation attribute FindBy.linkText must be a constant expression

1
  • 2
    Do the element's other attribute change language, too? If not, stop using find by text and find by something else, like href.
    – kirbycope
    Aug 29, 2017 at 20:59

2 Answers 2

3

I would not use the linktext here to find the element, as I would assume that the element should be there, no matter the language. Better look for an attribute that doesn't change with the language, like an Id or classname and create specific testcases to check if the correct language is used,

for example

@FindBy(id = "foo")
WebElement elm;

public boolean isLinkTextInCorrectLanguage(WebElement elm, String lang){
    return elm.getText().contains(lang);     
}
0
0

In addition to @Camarde awnser, if you cannot easily use id's (in an Ember app, e.g.), you could use a factory that spawns WebElement according to its text.

Some Page Object:

final String FORGOT_PASS_TEXT = "Forgot Password";    

public text getTextFromForgotPassword() {
  WebElement forgotPasswordElem = localizatedElementsFactory.findElementWithText(FORGOT_PASS_TEXT, rootElem);
  return forgotPasswordElem.getText();
}

The localizatedElementsFactory factory would be something like:

LocalizedTextRetriver loc_text_retriver = ...

public WebElement findElementWithText(text, rootElem) {
   return rootElem.findElementByText(loc_text_retriver.getLocalizedText(text));
}

LocalizedTextRetriver would be an interface that retrives text from a language. That way, you can inject in runtime which language you want to test and all your tests would search, through the localizatedElementsFactory, for elements with a localized text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.