1

I have integrated my Selenium testNG suite with Jenkins build. Depending on the environment the build happens onto, automation script gets triggered. I am sending environment info from Jenkins to Automation code using "mvn clean test -Denv=" command. I am successfully using this value for environment in my script for distinguishing between data on different environments.

However some of my Xpaths have varying data depending on environment like text()='something environment specific'. I am unable to provide dynamic string at run time in @FindBy as it is not allowed for this annotation.

Please suggest any leads for this issue as I am willing to stick with the page factory approach.

Web Element Repository : 

    @FindBy(how = How.XPATH, using = "//tr/td[text()='Dipali33']")
public WebElement student_UserName_Value;


pom.xml contains:

    <configuration>
<systemPropertyVariables>
<environment>${env}</environment>
</systemPropertyVariables>
</configuration>

Automation code contains logic based on environment:

    this.env = System.getProperty("environment");
if(env='dev'){
    ..........
    .......
} 

Actual- The XPATHS are defined using static String.

Expected- XPATHS should be defined using dynamic String variable at run time something like as as below:

    if(env='dev'){
        @FindBy(how = How.XPATH, using = "//tr/td[text()='"+dev_studentName+"']")
        public WebElement student_UserName_Value;
    }
    else if(env='test'){
        @FindBy(how = How.XPATH, using = "//tr/td[text()='"+test_studentName+"']")
        public WebElement student_UserName_Value;
    }
Dipsy
  • 11
  • 1
  • Use strings instead of `@FindBy`. – Fenio Jun 24 '19 at 10:29
  • This is not a duplicate of the linked question. This is clearly asking how to use a dynamic String with a @FindBy annotation. The linked ticket is asking how to use ExplicitWaits with a PageFactory. – Ardesco Jun 24 '19 at 15:17
  • The answer by the way is to use a `public static final String DEV_STUDENT_NAME`. The compiler will initialise the static final String before it processes the annotations. – Ardesco Jun 24 '19 at 15:19
  • Thanks for your suggestion. However, this solution might not work in my case because I am getting the dynamic text value of xpath selector at runtime from an excel file. What you suggested will work only if I declare public static final String DEV_STUDENT_NAME and provide value to it then and there which will defeat my purpose of segregation for hard-coded values from the code into environment specific excel files. – Dipsy Jun 26 '19 at 21:27

1 Answers1

2

No way to use @FindBy with dynamic xpath. You need build By.xpath in method

Amerousful
  • 2,292
  • 1
  • 12
  • 26