Reputation: 63
I am writing test codes with Selenium, TestNG. I am trying to set some elements on the @BeforeClass
section as I am repeating those actions on the same page. But I am getting null pointer exception. Is there a way to fix this issue?
public class RunTest {
private static WebElement user;
private static WebElement pass;
private static WebElement login;
@BeforeClass
public static void driveraBaglan() {
//Driver is declared here. I removed it to give the simple code.
user = driver.findElement(By.id("user"));
pass = driver.findElement(By.id("pass"));
login = driver.findElement(By.xpath("//button[contains(.,'Log In')]"));
statusMessage = driver.findElement(By.id("login-status-message")).getText();
}
@Test(priority=1)
public void loginNoInfo() {
user.clear();
pass.clear();
}
I get the null pointer error on user.clear()
, pass.clear()
and login.click()
functions. All tests on the page runs on the same page. I don't want to use repeated "find element by id" tags on each test on the page.
Upvotes: 2
Views: 6512
Reputation: 89
For me I had to include alwaysRun = true, which implies that it was probably a misconfiguration of the groups somehow.
@BeforeClass(alwaysRun = true)
setup() { ... }
Upvotes: 0
Reputation: 1375
I got this same issue by an accidental change I've done.
You may also get the null pointer exception on objects you are initializing in @BeforeClass or @BeforeTest, when you have a dilemma situation as below (where the class is set to as a disabled test class while having enabled test methods).
@Test(enabled=false)
public class TestClass{
A a;
@BeforeClass
public void doBeforeClass(){
a=new A();
//
}
@Test
public void test1(){
a.callMethod1();
}
@Test
public void test2(){
a.callMethod2();
}
}
Here, when you are running full-suite by putting @Test(enabled=false)
only for the class, still the suite will run individual tests in the class since @Test
annotations are present there. However, since @BeforeClass
is not running for whole class, object 'a' is not initialized and will cause null pointer exceptions.
In such cases, the individual @Test
annotations too need to be changed to @Test(enabled=false)
Upvotes: 1
Reputation: 3461
I never used TestNG but I do use Junit. Trying to replicate your example, I obtained the following exception:
[TestNG] Running:
/Users/davide.patti/Library/Caches/IntelliJIdea2017.3/temp-testng-customsuite.xml
java.lang.NullPointerException
at TestSO.loginNoInfo(TestSO.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
The reason was because, in my imported classes I was using the BeforeClass of Junit:
import org.junit.BeforeClass;
import org.testng.annotations.Test;
and not of TestNG.
If your null pointer exception is the same, be sure that your imported classes are the correct ones:
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Upvotes: 1
Reputation: 4420
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked
try to change the annotation
@BeforeTest
public static void driveraBaglan() {
//Driver is declared here. I removed it to give the simple code.
user = driver.findElement(By.id("user"));
pass = driver.findElement(By.id("pass"));
login = driver.findElement(By.xpath("//button[contains(.,'Log In')]"));
statusMessage = driver.findElement(By.id("login-status-message")).getText();
}
Upvotes: 2