Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm getting an error when trying to pass in a username and password to a form field using sendKeys. Below is my User Class followed by my test class. Does anyone know why the application is not passing a string?

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

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);
}
share|improve this question

2 Answers 2

You're accessing static properties that are never initialized (null) because the constructor is never called.

You can either set the static properties directly or take out the static context and initialize a User in your test.

Ex.

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);
}
share|improve this answer
    
programming advice: I would even consider changing user.username to user.getUsername() because as OOP approach acessing variable through getter should be safer. Dont ask me why, I am also programming newbie :) –  Pavel Janicek Aug 6 '13 at 8:55
    
perfect. This worked. Thanks all. –  adom Aug 6 '13 at 17:34

use

userInputBox.sendKeys(String.valueOf(User.username));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.