Layrelinn
Layrelinn

Reputation: 23

NullPointerException while trying to run my tests from testng.xml

So, the issue is next - I'm trying to refactor my base test to do multiple browser parallel testing, but after running my code from the testng.xml file I've got the NullPointerExeption. My BaseTest:

public class BaseTest {

    String browser = System.getProperty("browser");
    WebDriver driver = null;

    @Parameters("browser")
    @BeforeMethod(alwaysRun = true)
    public void setUp() {
        System.out.println("Current browser is " + browser);

        if (browser.equalsIgnoreCase("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
        } else if (browser.equalsIgnoreCase("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("edge")) {
            WebDriverManager.edgedriver();
            driver = new EdgeDriver();
        }
        driver.manage().window().maximize();
        driver.get("https://demo.prestashop.com/");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        BasePage.setDriverThreadLocal(driver);
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown() {
        getDriver().quit();
    }

}

My testng.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" parallel="methods">
    <test thread-count="2" name="Test" parallel="methods">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="SubscribeWithInvalidEmailTest"/>
            <class name="CheckLanguagesTest"/>
        </classes>
    </test>
</suite>

I would appreciate it if you could provide me any help.

Upvotes: 2

Views: 705

Answers (1)

Mehul Aggarwal
Mehul Aggarwal

Reputation: 56

There are two things-

  1. You are trying to get a browser property using System.getProperty("browser") but you haven't set it anywhere before, so, browser variable will always be null. Please set the property elsewhere first.
  2. You are passing a parameter browser but your setUp() function is not accepting any arguments, and thus your browser variable remains null and gives you the NullPointerException error. You should use it like below
    @Parameters("browser")
    @BeforeMethod(alwaysRun = true)
    public void setUp(String browser) {
        System.out.println("Current browser is " + browser); 

and now your browser variable will have the value which is passed by the testng.xml to your code and should not give the NullPointerException anymore.

If you still facing any issue then I recommend you to please share the stack trace which can be helpful in further debugging.

Upvotes: 1

Related Questions