4

尝试使用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);
}
4

2 回答 2

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);
}
于 2013-08-06T06:05:01.583 回答
1

利用

userInputBox.sendKeys(String.valueOf(User.username));
于 2013-08-06T06:57:34.277 回答