Search code examples
javaseleniumselenium-webdriverframeworkspageobjects

How to parameterize the locators?


I am trying store all values required for locators in Property File:

This is Property File:

url=https://www.google.com
value=search   

This is my object repository class:

public class pageobjects extends helperclass{
public WebDriver driver;

By search  = By.xpath("//input[@aria-label='search12()']") //I am trying to insert helpclass method here.

public class pageobjects(Webdriver driver){ //creating constructor
  
 this.driver=driver; }

 public WebElement search(){
return driver.findElement(search);
}
}

Here I don't want to hardcode "Search" in my object repository file and in my testcase file so I trying to store in a Property File. I know that by using "load ()" and "getProperty()", I can read/load Property File.

This is my helper class to consists of methods to store particular String value from Property file:

public helperclass { 
public properties prop;


static String search12() { 
prop=new Properties();
FileInputStream fis = new FileInputStream(path of property file);
return prop.getProperty("value");
}

}

Error: The String //input[@aria-label='search12()'] is not valid Xpath expression.


Solution

  • Why don't you try this -

    By.xpath("//input[@aria-label="+search12()+"]")