selenium webdriver:当ruuning testng.xml文件时我收到错误会话ID为空。调用quit()后使用WebDriver?

时间:2016-11-24 09:45:53

标签: selenium selenium-webdriver testng

当我们使用监听器运行XML文件时,它的抛出错误。不使用监听器就可以正常工作。

Codeng.XML下面的代码

  <?xml version="1.0" encoding="UTF-8"?>

 <suite name="Suite One" parallel="none" >

   <listeners>

    <listener class-name="utility.ScreenshotUtility"></listener>    
 

    </listeners>
    <test name="Test One" preserve-order="true"> 

    <classes>


    <class name="stramobiedemo.Nativestarterbackedsingle" />

    </classes>

    </test>


    <test name="Test two" preserve-order="true"> 

    <classes>



    <class name="stramobiedemo.Backedemailsingle" />


    </classes>

    </test>
 
     

    </suit> 

我正在犯错误

 org.openqa.selenium.NoSuchSessionException: Session ID is null. Using   

   WebDriver after calling quit()?

  Build info: version: 'unknown', revision: 'c7b525d', time: '2016-09-01 
  14:52:30 -0700'

   System info: host: 'Sahusofts-MacBook-Air-3.local', ip: '192.168.0.15', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.6', java.version: '1.8.0_101'
Driver info: driver.version: RemoteWebDriver
    at 

  org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:130)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:597)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:654)
    at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:341)
    at utility.ScreenshotUtility.captureScreenShot(ScreenshotUtility.java:62)
    at utility.ScreenshotUtility.onTestFailure(ScreenshotUtility.java:41)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1671)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1655)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1196)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:753)
    at org.testng.TestRunner.run(TestRunner.java:607)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)

我的ScreenshotUtilityclass代码

public class ScreenshotUtility implements ITestListener {

         // This method will execute before starting of Test suite.
         public void onStart(ITestContext tr) {

         }

         // This method will execute, Once the Test suite is finished.
         public void onFinish(ITestContext tr) {

         }

         // This method will execute only when the test is pass.
         public void onTestSuccess(ITestResult tr) {
          captureScreenShot(tr, "pass");
         }

         // This method will execute only on the event of fail test.
         public void onTestFailure(ITestResult tr) {
          captureScreenShot(tr, "fail");
         }

         // This method will execute before the main test start (@Test)
         public void onTestStart(ITestResult tr) {

         }

         // This method will execute only if any of the main test(@Test) get skipped
         public void onTestSkipped(ITestResult tr) {
         }

         public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
         }

         // Function to capture screenshot.
         public void captureScreenShot(ITestResult result, String status) {
          // AndroidDriver driver=ScreenshotOnPassFail.getDriver();
          String destDir = "";
          String passfailMethod = result.getMethod().getRealClass().getSimpleName() + "." + result.getMethod().getMethodName();
          // To capture screenshot.
          File scrFile = ((TakesScreenshot)BaseClass.driver).getScreenshotAs(OutputType.FILE);
          DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
          // If status = fail then set folder name "screenshots/Failures"
          if (status.equalsIgnoreCase("fail")) {
           destDir = "screenshots/Failures";
          }
          // If status = pass then set folder name "screenshots/Success"
          else if (status.equalsIgnoreCase("pass")) {
           destDir = "screenshots/Success";
          }

          // To create folder to store screenshots
          new File(destDir).mkdirs();
          // Set file name with combination of test class name + date time.
          String destFile = passfailMethod + " - " + dateFormat.format(new Date()) + ".png";

          try {
           // Store file at destination folder location
           FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
          } catch (IOException e) {
           e.printStackTrace();
          }
         }

}

我的基类代码

public class BaseClass {

   public static Properties Data= new Properties();
    public static Properties pro = new Properties();
    public static WebDriver driver=  null;

@BeforeClass
public void Initialize() throws IOException 
{
File src= new File ("/Users/sahusoft/Documents/workspace/strap/data.properties");
FileInputStream fis=new FileInputStream(src);
Data.load(fis);
src = new File("/Users/sahusoft/Documents/workspace/strap/strapmobileobj.properties");
fis= new FileInputStream(src);
pro.load(fis);
String browser = Data.getProperty("BrowserType");
setBrowser(browser);
driver.get(Data.getProperty("url"));
driver.manage().window().maximize(); 
driver.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
}

public void setBrowser(String browser)
{
    if(browser.equalsIgnoreCase("Chrome")){
        System.setProperty("webdriver.chrome.driver","/Users/sahusoft/Downloads/chromedriver 2");
        driver=new ChromeDriver();
}

     if(browser.equalsIgnoreCase("firefox"))
    {
        driver=new FirefoxDriver();

    }
    else if(browser=="safari")
      {
        System.setProperty("webdriver.chrome.driver","");
        driver=new InternetExplorerDriver();
    }
}





@AfterClass
public void endbrowser()
{
    driver.quit();
}

}

1 个答案:

答案 0 :(得分:0)

我在这里看不到与您listeners相关的任何错误。因为错误本身是不言自明的。

org.openqa.selenium.NoSuchSessionException:会话ID为空。 在调用quit()后使用WebDriver?

它说,你在退出后使用WebDriver instance。检查您是否已正确实施了注释。好像你的一个测试方法是在退出后使用驱动程序实例。