0

I am running my Cucumber suite tests with TestNG (Selenium + Java) and getting java.lang.NullPointerException.

I realized the problem is that my @BeforeTest() is being ignored for some reason causing the NullPointer problem.

I am using the TestNG 7.0.0 (but tried to use latest Beta also).

@BeforeTest() 
public void setUp() {
    driver = Web.createChrome(); // it call a method that has the Chromedriver
}

Web.java

public class Web {

public static WebDriver createChrome() {

    System.setProperty("webdriver.chrome.driver", webdriver_path);

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://the-internet.herokuapp.com/");

    return driver;
}
}

Output

java.lang.NullPointerException
at br.dsanders.steps.steps.accessing_the_Tnternet_herokuapp_com_website(steps.java:62)
at ?.Given accessing the Tnternet.herokuapp.com website(testing.feature:9)
drkvader
  • 163
  • 2
  • 12

1 Answers1

1

Try like below:

public class steps {


    WebDriver driver = null;

    public steps() {

        this.driver=Web.createChrome();

    }

@BeforeMethod() 
public void setUp() {
    driver.get("http://the-internet.herokuapp.com/");
  }
}

Note ClassName here is steps if you have other class name then change the class name and constructor name.

Change the @BeforeTest to @BeforeMethod

Source:

What is the difference between BeforeTest and BeforeMethod in TestNG

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • uhumm, yes its working better now, however my @BeforeTest() is still not being executed.. the browser its opening now because it is being called from the Constructor... I've commented my Before and the browser is still opening :( – drkvader Oct 03 '19 at 09:49
  • put some syso and see where it is calling in the execution cycle or it is not executing at all? – Shubham Jain Oct 03 '19 at 09:50
  • you are trying to invoke the driver for each test? – Shubham Jain Oct 03 '19 at 09:52
  • if yes then try to BeforeMethod instead of BeforeTest . source :https://stackoverflow.com/questions/50811759/what-is-the-difference-between-beforetest-and-beforemethod-in-testng – Shubham Jain Oct 03 '19 at 10:00
  • I did the sysouts, and the browser its being called from the Constructor that calls my class Web.java where I have the driver.get . So its not calling the Before. Tried to use the BeforeTest and BeforeMethod and both not worked. I was thinking.. maybe could be a problem in the Cucumber steps declaration? I mean every Cucumber step should be a method and should receive the @Test above.. or not? Just thinking here.. – drkvader Oct 03 '19 at 10:04
  • 1
    there is confusion in your understanding, Test is for TestNG not for cucumber, before is present in cucumber and tetsng both and BeforeMethod is of testng ... so it may be you are getting issue because the sequence of execution is different – Shubham Jain Oct 03 '19 at 10:08
  • change before import from testng to cucumber package it will execute for each scenario of cucumber – Shubham Jain Oct 03 '19 at 10:10
  • 1
    you are totally right, I got confused with TestNG and Cucumber.. I've changed just for @Before and all my suite executed fine. Thank you for your help! – drkvader Oct 03 '19 at 10:22
  • Glad it helps you – Shubham Jain Oct 03 '19 at 10:24