Jay Jay - 14 days ago 3
Java Question

Sharing TestNG setup across different test classes - Selenium Java

How can I share the same selenium test set up across different test classes? I keep getting nullpointexception and cannot figure out how to solve it. I've been stock in this issue for a while and searched the issue for hours but still haven't solved it. Please help.

This is my setup class

import static org.testng.Assert.fail;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class SettingsClass {

WebDriver driver;

@BeforeTest
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "chromeDriver_path");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized", "chrome.switches", "--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.manage().deleteAllCookies(); // delete all cookies
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

@AfterTest
public void tearDown() throws Exception {
driver.quit();
}


And here is my test class

import org.openqa.selenium.By;
import org.testng.annotations.Test;

import com.Settings;

public class ClassA extends SettingsClass {

boolean acceptNextAlert = true;
StringBuffer verificationErrors = new StringBuffer();

@Test
public void test() throws Exception {
driver.get("http://www.fakeurl.com");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
driver.findElement(By.name("submit")).click();
}


}




Here is the error message I keep getting

java.lang.NullPointerException
at com.ClassA.test(ClassA.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)


Any advice and help would be very much appreciated. Thank you in advance!

Answer

Try,

public class SettingsClass {

    public WebDriver driver;

    @BeforeTest
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "chromeDriver_path");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "chrome.switches", "--disable-extensions");
        driver = new ChromeDriver(options);
        driver.manage().deleteAllCookies(); // delete all cookies
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    @AfterTest
    public void tearDown() throws Exception {
        driver.quit();
    }
}