2017-10-10 39 views
-1

我已經寫了一個簡單的Selenium TestNG腳本,但在@AfterTest方法中得到了一個錯誤java.lang.NullPointerException。你能幫忙嗎?Selenium TestNG:driver.close()and driver.quit()

public class TestngDemo1  
     { 
      public WebDriver driver; 
      public String url="https://www.guru99.com/"; 

      @Test 
      public void LaunchURL() throws InterruptedException {   
       System.setProperty("webdriver.chrome.driver", "C:/Users/AB28488/Desktop/javawork 
     space/TestNGProject/drivers/chromedriver.exe"); 
       WebDriver driver= new ChromeDriver(); 
       driver.get(url); 
       Thread.sleep(2000); 
       String eTitle="Meet Guru99 - Free Training Tutorials & Video for IT Courses"; 
       String aTitle=driver.getTitle(); 
       Reporter.log(aTitle); 
       Thread.sleep(3000); 
       Assert.assertEquals(aTitle, eTitle); 
       Reporter.log("This will print if titles match!",true); 
      } 

      @BeforeMethod 
      public void BeforeMethod() { 
       Reporter.log("Before Method"); 
      } 

      @AfterMethod 
      public void afterMethod() { 
       Reporter.log("After method",true); 
      } 

      @AfterTest 
      public void quitDriver() { 
       driver.quit(); 
      } 

     } 
+0

公共WebDriver驅動程序; &WebDriver driver = new ChromeDriver();爲什麼要創建兩個驅動程序實例? – Pradeep

+0

在你測試方法之前添加下面的代碼driver = new ChromeDriver();而不是實例化內部測試方法 – Pradeep

+0

將您的課程轉換爲testng – zsbappa

回答

0

因爲在您的測試,你定義本地可見司機

public class TestngDemo1 
{ 
    // This is your "global" driver 
    public WebDriver driver; 
    public String url="https://www.guru99.com/"; 

    @Test 
    public void LaunchURL() throws InterruptedException 
    { 


     System.setProperty("webdriver.chrome.driver","C:/Users/AB28488/Desktop/javawork 
       space/TestNGProject/drivers/chromedriver.exe"); 
     // This is not the same driver you have defined above as a class field   
     WebDriver driver= new ChromeDriver(); 
    } 

    @AfterTest 
    public void quitDriver() 
    { 
     // Here you're trying to call method of an object that does not exist. 
     driver.quit(); 
    } 

} 

嘗試用這個代替:

public class TestngDemo1 
{ 
    // This is your "global" driver 
    public WebDriver driver; 
    public String url="https://www.guru99.com/"; 

    @Test 
    public void LaunchURL() throws InterruptedException 
    { 


     System.setProperty("webdriver.chrome.driver","C:/Users/AB28488/Desktop/javawork 
       space/TestNGProject/drivers/chromedriver.exe"); 
     driver= new ChromeDriver(); 
    } 

    @AfterTest 
    public void quitDriver() 
    { 
     driver.quit(); 
    } 

} 
+0

謝謝!你能給我更正的代碼嗎? –

+0

不知道什麼是正確的代碼,因爲我不是TestNG的專家。讓我們精心製作一個。首先嚐試更改「WebDriver driver = new ChromeDriver();」到「driver = new ChromeDriver();」 –

+0

非常感謝,全部!它爲我工作:) –

0

下面的代碼使用它會爲你工作:

public class TestngDemo1  
     { 
      public WebDriver driver; 
      public String url="https://www.guru99.com/"; 

      @Test 
      public void LaunchURL() throws InterruptedException 
      { 
      driver.get(url); 
      Thread.sleep(2000); 
      String eTitle="Meet Guru99 - Free Training Tutorials & Video for IT Courses"; 
      String aTitle=driver.getTitle(); 
      Reporter.log(aTitle); 
      Thread.sleep(3000); 
      Assert.assertEquals(aTitle, eTitle); 
      Reporter.log("This will print if titles match!",true); 
     } 

     @BeforeTest 
     public void initDriver() 
     {   
      System.setProperty("webdriver.chrome.driver","C:/Users/AB28488/Desktop/javawork 
space/TestNGProject/drivers/chromedriver.exe"); 
      driver= new ChromeDriver(); 

     } 
     @AfterTest 
     public void quitDriver() 
     { 
      driver.quit(); 
     } 

    } 
相關問題