Hi I'm trying to create a simple automation script in POM, but getting java.lang.NullPointerException during execution of the script.Pls any one help me to resolve the issue.
By creating a Browser Factory, return the WebDriver.
package Com.selenium.utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserFactory {
public static WebDriver BrowserStart(WebDriver driver,String browsername, String url) {
if(browsername.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver", "E:\\Eclipse-Java-Kepler\\New folder\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browsername.equalsIgnoreCase("Firefox")){
// System.setProperty("", "");
}
else if(browsername.equalsIgnoreCase("IE")) {
// System.setProperty("", "");
}
else {
System.out.print("Browser is not supported");
}
driver.get(url);
driver.manage().window().maximize();
return driver;
}
}
Below codes are for Base Class
package Com.selenium.pages;
import org.openqa.selenium.WebDriver;
import Com.selenium.utils.BrowserFactory;
public class BaseClass implements IClass {
public WebDriver WD;
public WebDriver Setup() {
return BrowserFactory.BrowserStart(WD, "chrome", "https://www.udemy.com/");
}
}
Then I created a class, which will contain all the WebElements & methods for perform any action on that page
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 LandingClass {
public WebDriver WD = null;
public LandingClass(WebDriver driver) {
this.WD = driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath=".//button[@class='btn btn-quaternary']")
public WebElement LoginButton;
public void Openlgin() {
LoginButton.click();
}
}
Finally I create a TestNG class to run the test case, but getting "NullPointerException" during execution of the test case
package Com.selenium.tests;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Com.selenium.pages.BaseClass;
import Com.selenium.pages.LandingClass;
public class LoginScenarios extends BaseClass {
LandingClass LC;
@BeforeTest
public void setup() {
super.Setup();
LC = new LandingClass(WD);
}
@Test(priority = 0)
public void OpenLogin() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LC.Openlgin();
}
}