Parallel execution of features file causes null pointer exception printer out on console

Viewed 365

I have tried to code cucumber parallel execution of feature file, but there is an error printed out on console like this:

[TestNG-PoolService-0] ERROR com.nicholas.StepsDef.ExportProduct - java.lang.NullPointerException

Element info: {Using=xpath, value=//a[@class='log-wrapper']/child::img}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at com.nicholas.StepsDef.AddProduct.navigateToManageProduct(AddProduct.java:65)
    at ✽.navigate to manage product(file:///C:/Users/nicholaswkc/IdeaProjects/cucumber-java-skeleton/src/test/resources/Features/AddProduct.feature:7)

05:38:22.730 [TestNG-PoolService-0] ERROR com.nicholas.StepsDef.ExportProduct - java.lang.NullPointerException
05:38:22.780 [TestNG-PoolService-0] ERROR com.nicholas.StepsDef.AddProduct - org.openqa.selenium.NoSuchSessionException: invalid session id

AddProductPageObject.java

public By lazadaLogo = By.xpath("//a[@class='log-wrapper']/child::img");

AddProduct.java

public class AddProduct {

    private AddProductPageObject page;
    private ChromeDriver driver;
    private Logger log = LogManager.getLogger(AddProduct.class);
    // ======================================================================
    public AddProduct() {
    }

    @Given("Launch the homepage and login")
    public void launchTheHomepageAndLogin() {
        log.info("Start Login");
        try {
            WebDriverManager.chromedriver().setup();
            driver =  new ChromeDriver();
            WebDriverWait timeWait = new WebDriverWait(driver, 30);
            page = PageFactory.initElements(driver, AddProductPageObject.class);
            driver.navigate().to("https://sellercenter.lazada.com.my/apps/seller/login");
            timeWait.until(ExpectedConditions.visibilityOfElementLocated(page.getLazadaSellerLogo()));

            // Input username
            driver.findElement(page.getUsername()).click();
            driver.findElement(page.getUsername()).clear();
            driver.findElement(page.getUsername()).sendKeys("nicholaswkc34@gmail.com");

            // Input password
            driver.findElement(page.getPassword()).click();
            driver.findElement(page.getPassword()).clear();
            driver.findElement(page.getPassword()).sendKeys("wlx_+279295");

            // Click submit btn
            driver.findElement(page.getSignInButton()).click();

            //assertThat(page.getPageTitle()).isEqualto("");

            Wait wait = new Wait();
            wait.implicitWait(driver, 5);

        } catch (Exception e) {
            log.error(e);
        }
    }

    @Given("navigate to manage product")
    public void navigateToManageProduct() {
        WebDriverWait waitProductLink = new WebDriverWait(driver, 30);
        waitProductLink.until(ExpectedConditions.visibilityOfElementLocated(page.getLazadaLogo()));
        driver.findElement(page.getProductLink()).click();

        WebDriverWait waitManagedProductLink = new WebDriverWait(driver, 30);
        waitManagedProductLink.until(ExpectedConditions.visibilityOfElementLocated(page.getManageProductLink()));
        driver.findElement(page.getManageProductLink()).click();
    }

Moreover, when I run using mvn command it pops out org.testng.xml.XmlSuite.setParallel(Ljava/lang/String;)which is highlighted on this group

Questions:

  1. What is causing the error?
  2. When maven surefire plugin error should fix?
2 Answers

I have seen an error occurred at "at com.nicholas.StepsDef.AddProduct.navigateToManageProduct(AddProduct.java:65)", method- page.getLazadaLogo().

I assume a driver object holds null while creating WebDriverWait waitProductLink = new WebDriverWait(driver, 30); object. Generally, WebDriverWait accepts null in its constructor parameter, but it throws error at run time when we tried to use that wait object created using null.

Solutions:

  1. Check what driver holds in step 'navigate to manage product' implementation. Scope of "Launch the homepage and login" driver assignment specific to that particular block. To overcome this issue, make driver variable as static and assign driver object reference to it in step- "Launch the homepage and login" implementation.

  2. In case these two steps run in parallel, then you need to create driver object inside step "navigate to manage product" implementation also.

Solve it by moving the init code to @Before method.

Related