1

Complete code is written to fetch data from excel and login to Gmail, but while trying to do so my browser had opened and also the desired page got opened and as well as login id was picked from excel and stored in the variable sUsername, but unable to locate the xpath as- element=driver.findElement(by.id("Email")); but when I print element it holds "null", where as expected was some address of the locator id. Further by using the address of id I would had used with sendkeys to enter the email address in the text box.

But the following error was displayed:

java.lang.NullPointerException at appModules.SignIN.Execute(SignIN.java:21)

Login class-where the locator issue exists: at - Login1.userName(driver).sendKeys(sUsername);

public class Login1 {

 //private static WebDriver driver=null;
 private static WebElement element=null;

public static WebElement userName(WebDriver driver) 
{
    try {
        System.out.println("aaa");
    System.out.println("bb");
        element=driver.findElement(By.name("Email"));
        System.out.println("ccc");
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(element);
    }

return element;
}
public static WebElement btn_login(WebDriver driver)
{
    element= driver.findElement(By.id("next"));
    return element;
}
public static WebElement passWord(WebDriver driver)
{
    element= driver.findElement(By.id("Passwd"));
    return element;
}
public static WebElement btn_SignIN(WebDriver driver)
{
    element= driver.findElement(By.id("signIn"));
    return element;
}
}

This is the SigniN class where iam getting the java null pointer exception--issue exists: at- Login1.userName(driver).sendKeys(sUsername);

public class SignIN {
private static WebDriver driver=null;

public static void Execute (int iTestCaseRow) 
{
    String sUsername=ExcelUtils1.getCellData(iTestCaseRow,Constant1.col_UserName);
    System.out.println(sUsername);
 //driver.ma3nage().window().maximize();
     //driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

Login1.userName(driver).sendKeys(sUsername);
    //driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    Login1.btn_login(driver).click();
String pass=ExcelUtils1.getCellData(iTestCaseRow, Constant1.col_password1);
Login1.passWord(driver).sendKeys(pass);
Login1.btn_SignIN(driver).click();
}
}

This is where I have instantiate the browser--

public class Utils1 {
    public static WebDriver driver;

    public static WebDriver OpenBrowser(int iTestCaseRow) {
        String sBrowserName;
        System.out.println(iTestCaseRow);
        sBrowserName = ExcelUtils1.getCellData(iTestCaseRow,
                Constant1.col_browser);
        if (sBrowserName.equals("Mozilla")) {
            driver = new FirefoxDriver();
            // Log.info("New driver instantiated");
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            // Log.info("Implicit wait applied on the driver for 10 seconds");
            driver.get(Constant1.URL);
            // Log.info("Web application launched successfully");

        }
        return driver;
    }
}
Sanjay Bhimani
  • 1,593
  • 1
  • 15
  • 29
  • login1 method looks like not pasted correctly. return element is catch block, does not return it. – murali selenium Jan 27 '16 at 16:08
  • What is this `Login1.userName(driver)`? Signature is `WebElement userName()`. Do you ever initialise `driver` in SignIN? Moreover, please paste the whole stacktrace. We cannot see which line is #21 in your listing. – Würgspaß Jan 27 '16 at 16:16
  • Login1 is the class name where I have defined a method userName which return a WebELement which is passed in the Login1.username(driver) and using sendkeys method I pass the username stored in the excel file.No driver is initialise in a main method only once,and line no 21-Login1.userName(driver).sendKeys(sUsername); its in signin class,I hope the above explanation helps..thanks – Arvind Chhajer Jan 28 '16 at 07:55
  • Your driver object is never initialized in SignIn class. So the null pointer exception as `private static WebDriver driver=null;`, u r no where providing Utils class driver object to SignIn class. :) – Vivek Singh Jan 29 '16 at 18:07
  • updated comment----------------I have once already initialise the webdriver driver =new FirefoxDriver(); in the Utils1 class,so there is no need to initialise the driver again as already it is defined as public static Utils1 class , and the flow is like, in sign class I have called the methods defined in Login class where the return statement is written... – Arvind Chhajer Jan 30 '16 at 10:01

2 Answers2

1

It is good practice to deal with internally as well explicit wait for locating element. If there is page related activity then also need to use wait for page to load.

Please follow bellow code For internal Wait

protected WebElement waitForPresent(final String locator) {
    // timeout is your default wait timeout in long.
    return waitForPresent(locator, timeout);
}

For Explicit Wait

protected WebElement waitForPresent(final String locator, long timeout) {
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    WebElement ele = null;
    try {
        ele = wait.until(ExpectedConditions
                .presenceOfElementLocated(locator));
    } catch (Exception e) {
        throw e;
    }
    return ele;
}

protected WebElement waitForNotPresent(final String locator, long timeout) {
    timeout = timeout * 1000;
    long startTime = System.currentTimeMillis();
    WebElement ele = null;
    while ((System.currentTimeMillis() - startTime) < timeout) {
        try {
            ele = findElement(locator);
            Thread.sleep(1000);
        } catch (Exception e) {
            break;
        }
    }
    return ele;
}
Sanjay Bhimani
  • 1,593
  • 1
  • 15
  • 29
0

Just spit balling here, but in addition to the copy/paste issues stated above.. I don't see where you do a 'get' to load the gmail page for the driver instance you are creating? Something like..

driver.get("https://mail.google.com/something");

Also, it would probably be a good idea to put an explicit wait in place for the "Email" field before doing the findElement as the page may still be rendering:

Wait<WebDriver> doFluentWait = fluentWait = new FluentWait<>(driver).withTimeout(PAGE_LOAD_WAIT, TimeUnit.SECONDS)
                                    .pollingEvery(POLLING_INTERVAL, TimeUnit.SECONDS)
                                    .ignoring(NoSuchElementException.class);

and then do something like

 doFluentWait.until(WebDriverUtil.elementIsVisible(By.name("Email")));
Dave
  • 161
  • 1
  • 7