gpt4 book ai didi

java - NoSuchSessionException session ID 为空。调用 quit() 后使用 WebDriver?并行运行 java 测试时

转载 作者:行者123 更新时间:2023-11-29 08:27:12 26 4
gpt4 key购买 nike

获取 NoSuchSessionException session ID 为空。调用 quit() 后使用 WebDriver?通过跨浏览器并行运行 java 测试时出错。

测试执行看起来没问题,但最后出现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)

最佳答案

这里是对正在发生的事情的解释:

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

您可以使用的解决方案:

  • 不要在测试中调用 driver.close() 并让拆卸处理

  • 在拆解中,检查一个有效的 session ,如果存在则只调用 driver.quit()

关于java - NoSuchSessionException session ID 为空。调用 quit() 后使用 WebDriver?并行运行 java 测试时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com