在@BeforeTest中执行代码后,它为@Test提供了空指针异常

时间:2017-09-08 07:31:48

标签: selenium nullpointerexception testng

执行@BeforeTest java.lang.NullPointerException @Test Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 34173 Only local connections are allowed. Sep 08, 2017 10:40:43 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS FAILED CONFIGURATION: @AfterTest Aftertest java.lang.NullPointerException at SampleTesting.Test1.Aftertest(Test1.java:71) 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:108) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146) at org.testng.TestRunner.afterRun(TestRunner.java:958) at org.testng.TestRunner.run(TestRunner.java:606) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76) FAILED: Test1 java.lang.NullPointerException at SampleTesting.Test1.Test1(Test1.java:27) 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:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76) =============================================== Default test Tests run: 1, Failures: 1, Skips: 0 Configuration Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 1, Skips: 0 Configuration Failures: 1, Skips: 0 =============================================== 部分。

显示错误:

package SampleTesting;
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeMethod;
    import org.testng.*;

    import java.util.Date;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.Select;

    public class NameClassTest {

        WebDriver driver;
        ChromeOptions options;


      @Test
      public void Test1() {
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          WebElement tabAgreement = driver.findElement(By.xpath("***Value***"));
          tabAgreement.click();

          WebElement btnNew = driver.findElement(By.xpath("***Value***"));
          btnNew.click();

          Select lstRecordType = new Select(driver.findElement(By.id***Value***));
          lstRecordType.selectByVisibleText("MSA");

          WebElement btnContinue = driver.findElement(By.xpath("***Value***"));
          btnContinue.click();

          WebElement txtAgreementName = driver.findElement(By.xpath("***Value***"));
          txtAgreementName.clear();
          txtAgreementName.sendKeys("***Value***");

          Date date = new Date();
          System.out.println("Date is "+ date);


      }
      @BeforeTest
      public void BeforeTest() {
          options = new ChromeOptions();
          options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
          String exepath = 

        "C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
              System.setProperty("webdriver.chrome.driver",exepath);
              WebDriver driver = new ChromeDriver(options);
              driver.manage().window().maximize();
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
              driver.get("***URL***");
              WebElement userName = driver.findElement(By.id("username"));
              WebElement password = driver.findElement(By.id("password"));
              WebElement btnLogin = driver.findElement(By.id("Login"));

              userName.sendKeys("****userName****");
              password.sendKeys("****password****");
              btnLogin.click();

          }

          @AfterTest
          public void Aftertest() {
              WebElement drpUserName = driver.findElement(By.id***Value***);
              drpUserName.click();

              WebElement lnkLogout = driver.findElement(By.xpath(***Value***));
              lnkLogout.click();

              driver.close();

          }

        }

使用的代码

            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
            <suite name="Suite_SuiteName">
              <test name="Test_TestName">
                <classes>
                  <class name="SuiteName.ClassName"/>
                </classes>
              </test> <!-- Test_TestName -->
            </suite> <!-- Suite_SuiteName -->

TestNG.xml文件

@BeforeTest

它在@Test下成功执行了该块,但当它来到Null Pointer exception部分时它会发出错误消息 - renderConversations(){ let conversationContent = this.state.conversationArray.map((convObj, i) => { return <View key={i} style={[globalStyle.conversationContainer,globalStyle.shadow]}> <Text style= {globalStyle.conversationText}>{ convObj.text }</Text> <Text style= {globalStyle.conversationText}> { convObj.actionButtons.map((el, j) => { return /*Code*/ }) } </Text> </View> }) return conversationContent } 。我不确定我在代码中犯了什么错误。

3 个答案:

答案 0 :(得分:0)

这是因为您在WebDriver driver = new ChromeDriver(options);注释方法中提到了@BeforeMethod。所以你的webdriver实例仅限于该方法。

因此,如果进入@test带注释的方法,它会发现驱动程序未初始化,因此抛出NullPointerException

只需从此处删除WebDriver

即可
 @BeforeTest
  public void BeforeTest() 
  {
      options = new ChromeOptions();
      options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
      String exepath = "C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
      System.setProperty("webdriver.chrome.driver",exepath);
      driver = new ChromeDriver(options);  // remove "WebDriver" from this line
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("***URL***");

如果仍有问题,请告诉我。谢谢:))

答案 1 :(得分:0)

@BeforeTest带注释的方法中,您有这一行

WebDriver driver = new ChromeDriver(options);

这会导致您的代码遮蔽类级数据成员,因此当您的@Test方法尝试访问驱动程序时,您会点击NullPointerException

要解决此问题,请在WebDriver driver = new ChromeDriver(options);带注释的方法中将driver = new ChromeDriver(options);更改为@BeforeTest

在旁注中,我建议您使用以下其中一项而不是@BeforeTest,因为@BeforeTest方法每<test>个代码只调用一次。因此,如果您有两个依赖@BeforeTest方法的测试类,那么您再次关注NullPointerException

  1. @BeforeClass - 以便每个测试类实例化一次webdriver,然后由类中的所有@Test方法共享。问题是你只需要依靠顺序执行,因为并行执行会导致测试方法相互踩踏。
  2. @BeforeMethod - 这样就可以为每个@Test方法设置一次webdriver。这会导致浏览器针对每种测试方法分离。另一方面,如果你想并行运行方法,你需要使用ThreadLocal。如果顺序执行没问题,那么你不需要做任何额外的事情。

答案 2 :(得分:0)

尝试在@BeforeTests之外创建WebDriver的对象 如下所述,

public class nameClassTest {
WebDriver driver = new ChromeDriver(options);

  @BeforeTest
      public void BeforeTest()
{

}

将解决您的问题。