[英]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 ? 我在从Selenium UI中的属性文件访问数据时遇到错误:如何解决?
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 1. 配置属性文件
2. utilities class 2. 实用程序类
3. Page elements definition class file 3. 页面元素定义类文件
4. reusable functions class file 4. 可重用函数类文件
5. test classes 5. 考试班
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();
}
}
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 . 此类中存在一些严重的问题: LoginPageElements ,这是由于绝对的xpath所致 。
For example : You are using this xpath : //*[@id=\\"login\\"]/input/tr3/td2/button
to click on submit button. 例如:您正在使用此xpath:
//*[@id=\\"login\\"]/input/tr3/td2/button
单击“ 提交”按钮。
A good alternative would suggest you to use relative xpath something like : 一个好的替代方法建议您使用相对xpath之类的东西:
//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 : 不同的替代方法是: sendKeys(Keys.RETURN) ,当且仅当应用程序支持在提供
username
和password
后单击Enter。在代码中是这样的:
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
. 尽管正如@Andrei所建议的那样 ,如果您非常依赖登录作为测试方法,则应该为提交按钮而不是
Keys.RETURN
编写一个相对的xpath或任何其他定位符。
Your all xpath are absolute , try to write locators as in this order : 您所有的xpath都是绝对的,请尝试按以下顺序编写定位符:
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 . 不久前,我遇到了属性文件的问题,问题出在行分隔符上,当我用Notepad ++打开属性文件时,一切看起来都很好,但是当用Notepad打开文件时,行分隔符却丢失了。 Possibly it will help
可能会有所帮助
Don't save the Excel table with Excel icon in Excel sheet. 不要将带有Excel图标的Excel表保存在Excel工作表中。 Save the Excel sheet with the button in Eclipse.
使用按钮在Eclipse中保存Excel工作表。 Please check the Image Link given below:- enter image description here
请检查以下图像链接:- 在此处输入图像描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.