提问者:小点点

NoSuchSessionException会话ID为null。在并行运行java测试时,调用退出()?后使用WebDriver


获取NoSuchSessionException会话ID为null。在通过跨浏览器并行运行java测试时,调用退出()后使用WebDriver?错误。

测试执行看起来不错,但最终出现了NoSuchSessionException错误。我确实知道应该在某个地方初始化驱动程序,但不清楚如何做到这一点。

项目信息:selenium WD,java,testng,maven。测试正在jenkins上构建并在saucelabs上运行。

主类看起来像:

  public class NewCustomerReg extends RemoteTestBase {
  @Test (dataProvider = "browsers")

  public void RegisterNewUser (String browser, String version, String os, 
  Method method) throws Exception {

    this.createRemoteDriver(browser, version, os, method.getName());
    Application app = new Application(driver);

    app.homePage().homePageDisplayed();
    Log.info("Validate Home Page");

    app.homePage().registerToOrder.click();
    Log.info("Click Register To Order link on the home page");

    app.registerToOrderPage().registerAsNewCustomer.click();
    Log.info("Click on Register As New Customer button");

    driver.close();
 }

由主类扩展的RemoteTestBase:

public class RemoteTestBase {

public WebDriver driver;
private static String baseUrl;
ConfigFileReader configRead;
protected PropertyLoader propertyRead;
public Logger Log = Logger.getLogger(BasicTest_Local.class.getName());
private static final String SAUCE_ACCESS_KEY = 
System.getenv("SAUCE_ACCESS_KEY");
private static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");

@BeforeMethod

@DataProvider(name = "browsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) throws 
JSONException {

    String browsersJSONArrayString  = 
    System.getenv("SAUCE_ONDEMAND_BROWSERS");
    System.out.println(browsersJSONArrayString);
    JSONArray browsersJSONArrayObj = new JSONArray(browsersJSONArrayString);

    Object[][] browserObjArray = new Object[browsersJSONArrayObj.length()] 
    [3];
    for (int i=0; i < browsersJSONArrayObj.length(); i++) {
        JSONObject browserObj = 
    (JSONObject)browsersJSONArrayObj.getJSONObject(i);
        browserObjArray[i] = new Object[]{browserObj.getString("browser"), 
    browserObj.getString("browser-version"), browserObj.getString("os")};
    }
    return browserObjArray;
   }

   void createRemoteDriver(String browser, String version, String os, String 
   methodName) throws Exception {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    Class<? extends RemoteTestBase> SLclass = this.getClass();
    capabilities.setCapability("browserName", browser);
    if (version != null) {
        capabilities.setCapability("browser-version", version);
    }
    capabilities.setCapability("platform", os);
    capabilities.setCapability("name", SLclass.getSimpleName());
    capabilities.setCapability("tunnelIdentifier", "hdsupply");


    driver = (new RemoteWebDriver(new URL("http://" + SAUCE_USERNAME + ":" + 
    SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"), capabilities));

    randomuser = new RandomDataSelect();
    configRead = new ConfigFileReader();
    propertyRead = new PropertyLoader();
    baseUrl = propertyRead.getProperty("site.url");
    getURL();
}



private void getURL () {

    driver.get(baseUrl);
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    this.annotate("Visiting page..." + driver.toString());


}

private void printSessionId() {

    String message = String.format("SauceOnDemandSessionID=%1$s job- 
    name=%2$s",
            (((RemoteWebDriver) driver).getSessionId()).toString(), "some job 
    name");
    System.out.println(message);
}



@AfterMethod(description = "Throw the test execution results into saucelabs")
public void tearDown(ITestResult result) throws Exception {

    ((JavascriptExecutor) driver).executeScript("sauce:job-result=" + 
 (result.isSuccess() ? "passed" : "failed"));
    printSessionId();
    driver.quit();
}
void annotate(String text)
{
    ((JavascriptExecutor) driver).executeScript("sauce:context=" + text);
}
}

错误日志:

Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03- 
11T20:26:55.152Z'
System info: host: 'gfmwsb01lds.hsi.hughessupply.com', ip: '10.224.196.74', 
os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.8.1.el6.x86_64', 
java.version: '1.8.0_161'
Driver info: driver.version: RemoteWebDriver
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using 
WebDriver after calling quit()?
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03- 
11T20:26:55.152Z'
System info: host: 'gfmwsb01lds.hsi.hughessupply.com', ip: '10.224.196.74', 
os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.8.1.el6.x86_64', 
java.version: '1.8.0_161'
Driver info: driver.version: RemoteWebDriver
at 
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.
java:125)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.
java:545)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions
$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:779)
at com.hdsupplysolutions.tests.RemoteTestBase.getURL(RemoteTestBase.java:83)
at 
com.hdsupplysolutions.tests.RemoteTestBase.createRemoteDriver(RemoteTestBase.
java:75)
at 
com.hdsupplysolutions.tests.***.RegisterNewUser(NewCustomerRegi.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at 
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocation
Helper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(
TestMethodWithDataProviderMethodWorker.java:71)
at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(
TestMethodWithDataProviderMethodWorker.java:14)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.
java:624)
at java.lang.Thread.run(Thread.java:748)

共1个答案

匿名用户

以下是对正在发生的事情的解释:

您正在AccsterNewUser中调用driver. close()。这将关闭当前窗口。如果没有打开的窗口,驱动程序将退出。因此,当您的tearDown调用river.退出()时,会话已经结束,您会收到错误消息。

您可以使用的解决方案:

  • 不要在测试中调用driver. close(),让拆解处理它

  • 在拆解中,检查有效会话并仅在存在时调用driver.退出()