代码之家  ›  专栏  ›  技术社区  ›  Ceyhun Ganioğlu

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

  •  1
  • Ceyhun Ganioğlu  · 技术社区  · 7 年前

    我正在用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查找元素”标记。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Roushan    7 年前
    @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();
    
    }
    
        2
  •  1
  •   Davide Patti    7 年前

    我从没用过 测试NG 但我确实使用 测试框架 。尝试复制您的示例,我得到了以下异常:

    [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; 
    
        3
  •  1
  •   Gimhani    5 年前

    由于我所做的一次意外更改,我得到了同样的问题。

    在@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”未初始化,将导致空指针异常。

    在这种情况下,个人 @测试 注释也需要更改为 @测试(启用=错误)