使用testng框架运行selenium测试时出现空异常错误

时间:2018-06-02 18:52:23

标签: java selenium exception selenium-webdriver nullpointerexception

我在testng框架中运行selenium简单脚本但我得到空指针异常,请在下面找到我的代码

package test.maven;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.*;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

public class test {
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath = "C:\\geckodriver.exe";
public WebDriver driver ; 
@AfterTest                            //Jumbled
 public void terminateBrowser(){
 driver.close();
      }
     @BeforeTest                            //Jumbled
      public void launchBrowser() {
          System.out.println("launching firefox browser"); 
          System.setProperty("webdriver.chrome.driver","D:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
          driver.get(baseUrl);
      }
      @Test                                //Jumbled
      public void verifyHomepageTitle() {
          String expectedTitle = "Welcome: Mercury Tours";
          String actualTitle = driver.getTitle();
          Assert.assertEquals(actualTitle, expectedTitle);
     }
     }

1 个答案:

答案 0 :(得分:1)

实例Webdriver对象未初始化,并且在Before Test方法中创建了Local Webdriver Object。请更改之前的测试,如下所示(删除Webdriver)

 @BeforeTest                            //Jumbled
      public void launchBrowser() {
          System.out.println("launching firefox browser"); 
          System.setProperty("webdriver.chrome.driver","D:\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
          driver.get(baseUrl);
      }