TestNG @BeforeClass初始化代码在Test之前没有运行

时间:2018-03-24 09:10:12

标签: java selenium junit testng

我正在用Selenium,TestNG编写测试代码。我正在尝试在@BeforeClass部分设置一些元素,因为我在同一页面上重复这些操作。但我得到空指针异常。有没有办法解决这个问题?

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();
}

我在user.clear()pass.clear()login.click()函数上收到空指针错误。页面上的所有测试都在同一页面上运行。我不想在id"中使用重复" find元素。页面上每个测试的标签。

3 个答案:

答案 0 :(得分:2)

@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

尝试更改注释

@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();

}

答案 1 :(得分:1)

我从未使用 TestNG ,但我确实使用 Junit 。试图复制你的例子,我获得了以下例外:

[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)

原因是因为在我导入的类中我使用的是Junit的BeforeClass:

import org.junit.BeforeClass;
import org.testng.annotations.Test;

而不是TestNG。

如果您的空指针异常相同,请确保导入的类是正确的:

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; 

答案 2 :(得分:1)

我由于意外更改而遇到了同样的问题。

当您遇到以下困境时(在启用测试方法的情况下将该类设置为禁用的测试类),您可能还会在@BeforeClass或@BeforeTest中初始化的对象上获得空指针异常。

@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();
    }

}

在这里,当您通过仅为类放置@Test(enabled=false)来运行全套件时,由于那里存在@Test批注,因此套件仍将在类中运行单独的测试。但是,由于@BeforeClass并未针对整个类运行,因此对象'a'不会初始化,并且会导致空指针异常。

在这种情况下,也需要将各个@Test注释更改为@Test(enabled=false)