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