0

Class 1: Logintest:

import Utility.BrowserFactory;

import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;

public class Logintest1 {
    BrowserFactory b=new BrowserFactory();
    public WebDriver driver;
  @Test
  public void f() {
      Loginpom lpom = PageFactory.initElements(driver, Loginpom.class);
      b.browserwait();
      lpom.obj();

  }
  @BeforeMethod
  public void beforeMethod()throws Exception {

      b.browser("chrome","https://www.gmail.com");
  }

  @AfterMethod
  public void afterMethod()throws Exception {
      b.browserwait();
      b.closebrowser();
  }

}

Class 2: BrowserFactory:

package Utility;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserFactory  {
public WebDriver driver;
Screenshots s=new Screenshots();

public WebDriver browser(String browsername, String url) throws IOException {
    if(browsername.equalsIgnoreCase("chrome"))
    {
        System.setProperty("webdriver.chrome.driver","E:\\Software\\driver\\chromedriver.exe");
        driver= new ChromeDriver();
        //s.takescreenshot(driver,"E://eclipse projects//GmailSuite//Test Output//Login_Output//browserinitiation.jpeg");
    }
    else if(browsername.equalsIgnoreCase("Firefox") ) {
        System.setProperty("webdriver.firefox.driver","E:\\Software\\driver\\geckodriver.exe");
        driver= new FirefoxDriver();
    }
    s.takescreenshot(driver, "E://eclipse projects//GmailSuite//Test Output//Login_Output//browserinitiation.jpeg");
driver.get(url);    
s.takescreenshot(driver, "E://eclipse projects//GmailSuite//Test Output//Login_Output//url_loading.jpeg");
driver.manage().window().maximize();    
return driver;
}

public void closebrowser() throws IOException {
    driver.quit();
    s.takescreenshot(driver, "E://eclipse projects//GmailSuite//Test Output//Login_Output//browserclosed.jpeg");
}

public void browserwait() {
    driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);

}
}

Class loginPage:

package GmailTests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class Loginpom {
public WebDriver driver;
public Loginpom(WebDriver driver) {
    this.driver=driver;

}
    @FindBy(how=How.XPATH, xpath="//*[@type='email']") WebElement emails;
    @FindBy(id="identifierNext")WebElement nbutton;
    @FindBy(name="password")WebElement pw;
    @FindBy(id="passwordNext")WebElement pwnbutton;
public void obj() { 
    emails.sendKeys("sbhuvani661");
    nbutton.click();
    pw.sendKeys("abc");
    pwnbutton.click();
}

}

Class Screenshot:

package Utility;
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;


public class Screenshots {

//public WebDriver driver;
public void takescreenshot(WebDriver driver,String path)throws IOException {

    File sourcefilename = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(sourcefilename, new File("path"));

}
}

Exception: The above method returns-

java.lang.NullpointerException at org.Selenium.support.PageFactory.DefaultElementLocator.findElement(DefaultElemen
tLocator.java:69) at the line emails.sendKeys("sbhuvani661"); 
and lpom.obj(); 
2

You forgot to instantiate elements of your page.

PageFactory.initElements(...)

Refer to this question to get the details: How to Implement Page Object and Page Factory Pattern in Selenium Webdriver?. Since your fields are not instantiated, they are not deployed in memory, so when you are trying to access method of not existing object in

emails.sendKeys("sbhuvani661");

you get NullPointerException

  • Page factory was initiated in the class Logintest itself. Even though i tried initiating it as a constructor in the pom class but still the null pointer exception is throwing – Bhuvani May 3 '18 at 10:06
  • 1
    When you initiate elements you pass the driver that is null. – Alexey R. May 3 '18 at 10:16
0

I imagine it is null because it is not successfully finding the element in question. Look to make sure that your @FindBy annotation is correct for WebElement emails.

  • WebElement is represented correctly – Bhuvani May 3 '18 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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