package org.example.driver;
import cucumber.api.Scenario;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class DriverManager {
public static WebDriver driver;
String browser = "chrome";
String baseUrl ="https://demo.nopcommerce.com/";
public DriverManager(){
PageFactory.initElements(driver,this);
}
public void runOnLocalBrowser() throws IllegalAccessException {
switch (browser){
case "chrome":
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
break;
case "edge":
WebDriverManager.edgedriver().setup();
driver= new EdgeDriver();
break;
case "safari":
driver= new SafariDriver();
break;
case "firefox":
WebDriverManager.firefoxdriver().setup();
driver=new FirefoxDriver();
break;
default:
throw new IllegalAccessException("Unexpected browser");
}
}
public void maxBrowser(){
driver.manage().window().maximize();
}
public void implicitWait(){
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
public void closeBrowse(){
driver.quit();
}
public void sleepBrowser(int ms) throws InterruptedException {
Thread.sleep(ms);
}
public void goToUrl(){
driver.get(baseUrl);
}
public String getUrl(){
return driver.getCurrentUrl();
}
public String getTitle(){
return driver.getTitle();
}
public WebElement waitUntilElementIsClickable(WebElement element){
WebDriverWait wait = new WebDriverWait(driver,30);
return wait.until(ExpectedConditions.elementToBeClickable(element));
}
public void waitForElementVisibility(WebElement element, int timeout, String failureMessage) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.withMessage(failureMessage);
wait.until(ExpectedConditions.visibilityOf(element));
}
public void scrollTo(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
public void takeElementscreenshot(WebElement element, String fileName) {
File scnFile =element.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scnFile, new File("./target/screenshots/" +fileName+ ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void takeScreenshot(Scenario scenario){
byte[] screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenShot, "image/png");
//take a screen shot
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("/Users/khuntn01/Desktop/screanshotTests/Error.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public int generateRandomNumber(){
Random random = new Random();
return random.nextInt(500);
}
public static String getRandomString(int length) {
final String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJLMNOPQRSTUVWXYZ";
StringBuilder result = new StringBuilder();
while(length > 0) {
Random rand = new Random();
result.append(characters.charAt(rand.nextInt(characters.length())));
length--;
}
return result.toString();
}
}
1 Answer
I can suggest the following steps to fix the null pointer exception issue in the provided code:
Initialize the driver instance: The 'driver' instance in the DriverManager class should be initialized properly before using it. We can initialize the driver instance in the 'runOnLocalBrowser' method. For example, in the 'runOnLocalBrowser' method, we can add code to initialize the ChromeDriver instance as shown below:
public void runOnLocalBrowser() throws IllegalAccessException {
switch (browser){
case "chrome":
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
break;
// Add code to initialize other drivers as well
}
}
Check for the null WebElement instance: Before using the WebElement instance in the 'waitUntilElementIsClickable', 'waitForElementVisibility', 'scrollTo', or 'takeElementscreenshot' methods, we need to make sure that it is not null. We can add a null check before using the WebElement instance. For example, in the 'waitUntilElementIsClickable' method, we can add code to check for null WebElement instance as shown below:
public WebElement waitUntilElementIsClickable(WebElement element){
if(element == null){
throw new IllegalArgumentException("Element cannot be null");
}
WebDriverWait wait = new WebDriverWait(driver,30);
return wait.until(ExpectedConditions.elementToBeClickable(element));
}
By following the above steps, you would be able to fix the null pointer exception issue in the driver class. It is important to make sure that all the variables and objects are initialized before using them to avoid such issues.