Reputation: 29
I have created a xpath.properties file and stored it at the location only. The format is as:
objuserName=//input[@placeholder='Username']
objpassword=//input[@placeholder='Password']
objloginButton=//a[@onclick='return ValidateLogin()']
Written a code to load this property file and enter the username and password and click login button. The code opens the browser successfully but on entering the username it gives "Exception in thread "main" java.lang.IllegalArgumentException: Cannot find elements when the XPath is null."
public class Login {
static Properties objprop = new Properties();
static void PropertyManager() throws IOException{
File file = new File("C:\\proj-Automation\\financialsys\\abcd\\src\\test\\resources\\xpath.properties");
FileInputStream fileInput = null;
try{
fileInput = new FileInputStream(file);
}catch (FileNotFoundException e){
}
Properties objprop = new Properties();
try{
objprop.load(fileInput);
}catch(IOException e){
}
//objprop.load(objfile);
}
//When user opens the "firefox" browser
void OpenBrowser(String browsername) throws IOException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",config.getParameterValue("chrome_driver_exe_path_32bit"));
config.driver=new ChromeDriver();
}
public void EnterUserName(String username){
config.driver.findElement(By.xpath(objprop.getProperty("objuserName"))).sendKeys(username);
}
public void PageMaximise(){
config.driver.manage().window().maximize();
}
//code for entering the password and clicking on login button
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
Login LF = new Login();
Login.PropertyManager();
LF.OpenBrowser("CH32");
LF.EnterURL("http://localhost:90/financialsys");
LF.PageMaximise();
LF.EnterUserName("dummycfo");
LF.EnterPassword("passw0rd");
LF.ClickLoginButton();
}
}
What could be the reason of the IllegalArgumentException error at line config.driver.findElement(By.xpath(objprop.getProperty("objuserName"))).sendKeys(username); and LF.EnterUserName("dummycfo");
Upvotes: 0
Views: 2757
Reputation: 198
Kindly try the below code to fetch the key from property file by below code snippet:
public static String fetchLocatorValue(String key) throws IOException {
FileInputStream file = new FileInputStream(Path of perperty file);
Properties property = new Properties();
property.load(file);
return property.getProperty(key).toString();
}
Upvotes: 0
Reputation: 150
static void PropertyManager() throws IOException{
File file = new File("C:\\proj-Automation\\financialsys\\abcd\\src\\test\\resources\\xpath.properties");
FileInputStream fileInput = null;
try{
fileInput = new FileInputStream(file);
}catch (FileNotFoundException e){
}
try{
objprop.load(fileInput);
}catch(IOException e){
}
//objprop.load(objfile);
}
remove Properties objprop = new Properties(); this line from the above method, you are initializing objprop variable with a new object, instead of using the global one which you already have on the top.
Upvotes: 1