junit TestNG @BeforeClass初始化代码在测试前未运行

2skhul33  于 2023-08-05  发布在  其他
关注(0)|答案(4)|浏览(194)

我正在用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()函数上得到空指针错误。页上的所有测试都在同一页上运行。我不想在页面上的每个测试中使用重复的“find element by id”标记。

dgiusagp

dgiusagp1#

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

}

bqjvbblv

bqjvbblv2#

我从来没有使用过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;

l2osamch

l2osamch3#

我也遇到了同样的问题,因为我做了一个意外的改变。
在@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)

jfgube3f

jfgube3f4#

对我来说,我必须包含alwaysRun = true,这意味着这可能是组的错误配置。

@BeforeClass(alwaysRun = true)
setup() { ... }

字符串

相关问题