java sendkeys Error keys should be a string

17,474

Solution 1

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

Solution 2

use

userInputBox.sendKeys(String.valueOf(User.username));
Share:
17,474
adom
Author by

adom

Updated on June 21, 2022

Comments

  • adom
    adom almost 2 years

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