Selenium TestNG:driver.close()和driver.quit()

时间:2017-10-10 10:49:45

标签: java selenium

我编写了一个简单的Selenium TestNG脚本,但在java.lang.NullPointerException方法中收到错误@AfterTest。你能帮忙吗?

    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();
           }

      }

3 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分: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();
         }

  }

答案 2 :(得分:0)

我通过创建三个类来做到这一点:GetProperties、BaseTest 和 Tests。 GetProperties 读取 config.properties 文件并返回带有键和值(url、millis)的 java.util.Properties。

BaseTest 打开和关闭浏览器。

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BaseTest extends GetProperties{

    public static FirefoxDriver geckoDriver;
    public static ChromeDriver chromeDriver;

    @BeforeClass
    public static void driversInit() throws Exception {
        System.setProperty("webdriver.gecko.driver", "your/path/geckodriver");
        geckoDriver = new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver", "your/path/chromedriver");
        chromeDriver = new ChromeDriver();
    }

    @AfterClass
    public static void driversQuit() throws Exception {
        geckoDriver.quit();
        chromeDriver.quit();
    }
}

测试运行测试。

import org.junit.Test;

public class Tests extends BaseTest{
    @Test
    public void testChromeOpenURL() throws InterruptedException {
        chromeDriver.get(url);
        Thread.sleep(millis*10);
    }

    @Test
    public void testFirefoxOpenURL() throws InterruptedException {
        geckoDriver.get(url);
        Thread.sleep(millis*10);
    }
}