Selenium Webdriver中以下代码的空指针异常

时间:2019-01-29 13:52:07

标签: java selenium

        public class NewTest 
          {
        @FindBy(id="btnSearch")
        public WebElement search;
        public WebDriver driver;

         @Test
         public void openMyBlog() {
         driver.manage().window().maximize();
         driver.get("url");

           search.click();
             try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }

      @BeforeClass
      public void beforeClass() {

          System.setProperty("webdriver.chrome.driver", "D:\\Driver\\chromedriver_win32\\chromedriver.exe");
          driver  = new ChromeDriver();
      }

      @AfterClass
      public void afterClass() {
          driver.close();
      }
    }

收到如下错误:

[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 44992
Only local connections are allowed.
Jan 29, 2019 12:59:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: openMyBlog
java.lang.NullPointerException
    at testngproject.NewTest.openMyBlog(NewTest.java:28)
    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:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

我尽力摆脱了这个问题,但未能做到。任何人都可以帮助我解决解决此空指针异常的问题的解决方法

我尽力摆脱了这个问题,但是没有做到。任何人都可以帮助我解决解决此空指针异常的问题

1 个答案:

答案 0 :(得分:1)

在您的程序中添加以下代码,然后尝试重新运行:

const makeImagesWork = () => images.forEach((name, i) => images[i] = `https://loremflickr.com/420/420?lock=${(i + 1) % 2 + i}&random=${name}`);
const box = document.getElementById('box');

images = ["Apple.jpg", "Apple.jpg", "Orange.jpg", "Orange.jpg", "Cake1.jpg", "Cake1.jpg", "Car.jpg", "Car.jpg", "House1.jpg", "House1.jpg", "Cake2.jpg", "Cake2.jpg", "House2.jpg", "House2.jpg", "Cake4.jpg", "Cake4.jpg"];

makeImagesWork(); // for testing
l = images.length;
let output = '';

function shuffle(a) {
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
}

images = shuffle(images);

for (var i = 0; i < l; i++) {
  output += `<div class="col-3 imgs">
                    <img class="img-fluid" src=${String(images[i])} alt="image">
                </div>`
}

box.innerHTML = output;

在使用POM(页面对象模型)时,需要先初始化所有元素,然后PageFactory.initElements()会做到这一点。