4

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);
}

2 Answers 2

8

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
2

use

userInputBox.sendKeys(String.valueOf(user.username));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.