javaseleniumautomated-testsdata-driven-tests

Selenium-sendKeys() throwing 'Keys to send should be a not null CharSequence'


Hey guys i am new to Selenium and facing some issue . There are 3 class , one is Data_Provider which reads data from excel another is a class to Add users (after reading from excel) last class is main class which calls Add class.

Below is Add class code

    package pomPagefactory;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.Test;

    public class Add extends Data_Provider {
    public Add(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
    }

    public static String fname, lname, title, email, tel, uname, pass, cpass;

    @Test(dataProvider = "testset")
    public void create(String fname, String lname, String title, String email, String tel, String uname, String pass,
            String cpass) {

        driver.findElement(By.xpath("//input[@name='FirstName']")).clear();
        driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys(fname);

        driver.findElement(By.xpath("//input[@id='LastName']")).clear();
        driver.findElement(By.xpath("//input[@id='LastName']")).sendKeys(lname);
        driver.findElement(By.xpath("//input[@id='Title']")).clear();
        driver.findElement(By.xpath("//input[@id='Title']")).sendKeys(title);

        driver.findElement(By.xpath("//input[@id='Email']")).clear();
        driver.findElement(By.xpath("//input[@id='Email']")).sendKeys(email);

        driver.findElement(By.xpath("//input[@id='TelephoneNumber']")).clear();
        driver.findElement(By.xpath("//input[@id='TelephoneNumber']")).sendKeys(tel);

        driver.findElement(By.xpath("//input[@id='UserID']")).clear();
        driver.findElement(By.xpath("//input[@id='UserID']")).sendKeys(uname);

        driver.findElement(By.xpath("//input[@id='password']")).clear();
        driver.findElement(By.xpath("//input[@id='password']")).sendKeys(pass);

        driver.findElement(By.xpath("//input[@id='cpassword']")).clear();
        driver.findElement(By.xpath("//input[@id='cpassword']")).sendKeys(cpass);

        driver.findElement(
                By.xpath("//button[@type='submit']//span[@class='ng-scope'][contains(text(),'Create User')]")).click();

    }
}

The DataProvider class is correct no issue in it.

I have called Add class (methods in add class) in Main page(main class)

Main class code

  package pomPagefactory;
    //all imports;
    public class Admin {

    public static WebDriver driver;

    @Test
    public void Page() throws Exception {

        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\srawat\\Downloads\\chromedriver_win32\\chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        Locator loc = PageFactory.initElements(driver, Locator.class);
        loc.Login("uaadmin", "novell");

        loc.people_user();

        loc.create_new_user();
        Add ad=PageFactory.initElements(driver, Add.class);
        ad.create(Add.fname,Add.lname, Add.title, Add.email, Add.tel, Add.uname, 
        Add.pass, Add.cpass);
    }
}

Solution

  • You declared the variables in Add class as static, and then you send them back to create method in Add class. It doesn't make much sense, and the source of the problem, the variables where never initialized so they are null.

    In addition you misunderstood the use of PageFactory, PageFactory.initElements doesn't initialize String class variables, how can it know what value to put there? It initialize WebElement class variables with @FindBy annotation.

    Delete the static variables from Add class, if you need them only in create() pass them to this method, if you need them in class level pass them in the constructor to private variables.