Below is my base class, for initializing and managing a Selenium WebDriver instance. This class ensures that a single WebDriver instance is created and configured, which can be reused across different tests.
Code:
public WebDriver getDriver() {
if (driver == null) {
synchronized (BaseClass.class) {
if (driver == null) {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
Map<String, Object> prefs = new HashMap<>();
prefs.put("profile.default_content_setting_values.popups", 2);
options.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(140, TimeUnit.SECONDS);
objWait = new WebDriverWait(driver, Duration.ofSeconds(60));
}
}
}
return driver;
}
@BeforeClass
public void initializeDriver() {
try {
driver = getDriver();
driver.get(Locators.URL);
Thread.sleep(3000);
} catch (Exception e) {
System.out.println(" Exception in before class driver instantiation: " + e);
}
}
Below is my GuestUser.java class public class GuestUser extends CommonUtils
code :
`
`public void guestUserActions() {
try {
enableEnableDelivery();
refresh();
deliveryAddress();
CloseButtonForSignInPopup();
disableEnableDelivery();
refresh();
writeConsole("Guest User delivery actions completed");
enableEnablePickup();
refresh();
pickupAddress();
CloseButtonForSignInPopup();
disableEnablePickup();
refresh();
WaitTime(3);
writeConsole("Guest User pickup actions completed");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}``
for the above case I have created a test class to execute the script. GuestUser.Test
code:
public class GuestUserTest extends GuestUser implements Locators {
CategoryPage categoryPage = new CategoryPage();
@Test
public void guestTest() throws Exception {
guestUserActions();
categoryPage.gettingInsideCategories();
}
Output:
Exception java.lang.NullPointerException
Driver is not getting intialised properly when it is getting into the next method in @test
categoryPage.gettingInsideCategories(). Could anyone please help ?
Kracker Krish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.