I am using selenium WebDiver and PageFacotry to initialize webelement,but I want to initialize element at runtime but value should come form xml file or property file.
Below statement initialize LogIn_PG_POF all the element which are declare in LogIn_PG_POF class but I want to initialize how(it is a attribute of FindBy annotation) portion from the properties file or xml file so later on I can change easily no need to change in code portion.
LogIn_PG_POF LoginPage = PageFactory.initElements(driver, LogIn_PG_POF.class);
This is a LogIn_PG_POF
public class LogIn_PG_POF {
final WebDriver driver;
@FindBy(how = How.ID, using = "log")
public WebElement txtbx_UserName;
@FindBy(how = How.ID, using = "pwd")
public WebElement txtbx_Password;
@FindBy(how = How.NAME, using = "submit")
public WebElement btn_Login ;
// This is a constructor, as every page need a base driver to find web elements
public LogIn_PG_POF(WebDriver driver){
this.driver = driver;
}
}
I haven't done it myself, but I believe you can do this with a few custom classes. PageFactory has an initElements method that takes an ElementLocatorFactory.
If you create a custom ElementLocatorFactory, ElementLocator, and a custom attribute class, you can use your ElementLocator to identify those elements with your custom tag and pull the find-by information out of your properties file.
Then you call the page factory with your own element locator factory:
LogIn_PG_POF page = new LogIn_PG_POF(driver);
ElementLocatorFactory factory = new CustomElementLocatorFactory(driver);
PageFactory.initElements(factory, page);
I had a similar requirement and I have created my own annotation and PageFactory Class. I am passing the key value in the annotation whose value I am getting later from the XML and in PageFactory class say in initElements(WebDriver d, Page.class) method, using reflections retrieve all the fields annotated with your custom annotation, take the value and then retrieve the corresponding value from the XML and instantiate each element of the class. It can also give you flexibility to pass locator type like id, css from the XML itself.
Lately came to know about ElementLocator in selenium, but I am not sure how to use it. Neither found any good topics on its usage
Your question is not very clear.
Based on my understanding I think you want to pass the element id from property file or XML file.
Here is sample code that may help you. This code does not use pagefactory for simplicity.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchUsingSelenium {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String searchBoxId = prop.getProperty("id");
String searchText = prop.getProperty("searchtext");
WebElement searchBox = driver.findElement(By.id(searchBoxId));
searchBox.sendKeys(searchText);
driver.close();
}
}