2016-11-24 258 views
0

当我们用侦听器运行XML文件时,它的抛出错误。没有使用监听器它的工作正常。selenium webdriver:当ruuning testng.xml文件时,我收到错误会话ID为空。调用quit()后使用WebDriver?

下面的代码

<?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下面的代码的testng.xml

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

}

回答

0

我在这里看不到有关您的listeners的任何错误。由于错误本身就是自我解释。

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

它说,你在呼唤退出后使用WebDriver instance。检查您是否正确实施了注释。看起来像你的测试方法之一是在其退出后使用驱动程序实例。

+0

当我们删除在XML文件听者,其做工精细,如果我在XML文件中添加IAM听者得到错误 – yogesh