Getting error : org.openqa.selenium.WebDriverException: unknown error: keys should be a string


Getting error : org.openqa.selenium.WebDriverException: unknown error: keys should be a string



I am getting the error to access the data from the properties file in my selenium UI : How can this be solved ?


org.openqa.selenium.WebDriverException: unknown error: keys should be a string.



I have the following framework. I made it for my self for ease, looks like complicating myself. If any good ideas to better it, please suggest.
Appreciate all suggestions.



Framework contains the following :
1. config properties file
2. utilities class
3. Page elements definition class file
4. reusable functions class file
5. test classes



config.properties file has the following content :


config.properties


url=http://some.com
Email=someuser
Password=somepassword



Utilities class (BrowserCalls) the following code :


BrowserCalls


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BrowserCalls {

public WebDriver driver;
public Properties configs = new Properties();
public String pathToProperties = "path to config.properties file";

public void invokeChromeBrowser() throws IOException {

FileInputStream input = new FileInputStream(pathToProperties);
pmsConfigs.load(input);
System.setProperty("webdriver.chrome.driver", configs.getProperty("chromepath"));
driver = new ChromeDriver();
getAndMazimize();

}

private void getAndMazimize(){

driver.get(configs.getProperty("url"));
driver.manage().window().maximize();

}


public void closeChromeBrowser(){

if(driver != null){

driver.close();
}

}


}



Page elements definition class file has the following code :


import org.openqa.selenium.By;

public class LoginPageElements {


//Login page elements
public static By element1 = By.xpath("/html/head/link[1]");
public static By username = By.xpath("//*[@id="login"]/input/tr1/td1");
public static By password = By.xpath("//*[@id="login"]/input/tr2/td1");
public static By submitButton = By.xpath("//*[@id="login"]/input/tr3/td2/button");
public static By title = By.xpath("/html/head/title");

}



Functionality definition classes to be called by test case classes :


import com.automation.PageElements.LoginPageElements;
import com.automation.ReusableFunctions.BrowserCalls;

public class LoginFeature extends BrowserCalls {

public void userLogin(){

driver.findElement(LoginPageElements.element1);
driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty(Email));
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty(Password));
driver.findElement(LoginPageElements.submitButton).click();

}

}



Test Case class is as below :


import com.automation.ReusableFunctions.BrowserCalls;
import com.automation.Components.LoginFeature;
import org.testng.annotations.Test;

import java.io.IOException;

public class LoginTestCase1 extends BrowserCalls {

@Test (description = "Verify application login")
public void LoginTest() throws IOException {

LoginFeature login = new LoginFeature();
login.invokeChromeBrowser();
login.userLogin();
login.closeChromeBrowser();

}

}




3 Answers
3



You can check what this two statements return like this:


System.out.println(System.getProperty("Email"));
System.out.println(System.getProperty("Password"));



probably they are returning null, and that's why you are getting error.


null



Try this:


public void userLogin(){
System.out.println(System.getProperty("Email")); // check statement return
System.out.println(System.getProperty("Password")); // check statement return

driver.findElement(LoginPageElements.element1);
driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("Email"));
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("Password"));
driver.findElement(LoginPageElements.submitButton).click();

}



PS


System.getProperty("key") // should be like this
System.getProperty(key) // not like this



Change these two lines to :


driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty(Email))
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty(Password));



To:


driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("username"))
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"));



Now talking about the suggestion part :



There is some serious issue in this class : LoginPageElements , and that is because of absolute xpath.



For example : You are using this xpath : //*[@id="login"]/input/tr3/td2/button to click on submit button.


//*[@id="login"]/input/tr3/td2/button



A good alternative would suggest you to use relative xpath something like :


//button[text()='Submit'] // This may not work cause you have not shared the HTML for the submit button. Here I am just guessing.



different alternative would be to go with : sendKeys(Keys.RETURN) , if and only if the application support clicking on enter after providing the username and password.Something like this in code :


username


password


driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"+Keys.RETURN));



Though as suggested by @Andrei , if you are heavily dependent on login as test method , then you should write a relative xpath or any other locator for the submit button instead of Keys.RETURN.


Keys.RETURN



Your all xpath are absolute , try to write locators as in this order :





I would not say, that sendKeys(Keys.RETURN) is better, because in this case the submit button will not be tested, if it is clickable and works as expected. I think the most of users will click on the button and not use Enter.
– Andrei Suvorkov
Jun 30 at 18:55


sendKeys(Keys.RETURN)





@AndreiSuvorkov : That again comes as a use case. If I had to test different module of an application,(By different module I mean internal part) I would not consider login as my test.
– cruisepandey
Jun 30 at 18:57





The login test is the first test should be done, and the most important one. If you are not able to login in app, you can't do anything. Automation is about simulating user behavior. That's why I would prefer to locate the button and click on it.
– Andrei Suvorkov
Jun 30 at 19:00





@AndreiSuvorkov : Yep agreed, updated the question with the same. Now as when I do regression of some modules , would I go for login as a test method ? and if you are in such cases where you already have login as a data driven test method where you are testing more than 10-15 username and password , then I would not consider login as a test while doing the regression. Though I would have login test method running before I can make that conclusion.
– cruisepandey
Jun 30 at 19:06






Did you able to resolve your query ? If yes , then upvote and accept any answer which have helped you to resolve your problem. Thank you !
– cruisepandey
Jun 30 at 20:25



Some time ago I was have troubles with property file, the problem was with line separators, when I opened property file with Notepad++ all looked fine, but line separators was missing when file was opened with Notepad. Possibly it will help






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API