尝试使用sendKeys
将用户名和密码传递到表单字段时出错。下面是我的用户类,后面是我的测试类。有谁知道为什么应用程序没有传递字符串?
org.openqa.selenium.WebDriverException:未知错误:键应为字符串
public class User {
public static String username;
public static String password;
public User() {
this.username = "username";
this.password = "password";
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
}
@Test
public void starWebDriver() {
driver.get(domainURL.getURL());
WebElement userInputBox, passInputBox;
userInputBox = driver.findElement(By.xpath("//input[@name='email']"));
passInputBox = driver.findElement(By.xpath("//input[@name='password']"));
System.out.println("before sending keys");
userInputBox.sendKeys(User.username);
}
答案 0 :(得分:8)
您正在访问从未初始化的静态属性(null),因为永远不会调用构造函数。
您可以直接设置静态属性,也可以取出静态上下文并在测试中初始化用户。
实施例
public class User {
public String username;
public String password;
public User() {
this.username = "username";
this.password = "password";
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
}
@Test
public void starWebDriver() {
User user = new User();
driver.get(domainURL.getURL());
...
userInputBox.sendKeys(user.username);
}
答案 1 :(得分:1)
使用
userInputBox.sendKeys(String.valueOf(User.username));