【问题标题】:Selenium is skipping the test and throwing the error "SessionNotCreatedException: Could not start a new session" while executing on containerSelenium 在容器上执行时跳过测试并抛出错误“SessionNotCreatedException:无法启动新会话”
【发布时间】:2021-12-27 10:50:32
【问题描述】:

我正在 selenium 独立 chrome 调试 docker 容器上使用 TestNG 执行 Selenium 测试。

以下是用于实例化远程 Web 驱动程序的 Selenium 代码:​​

ChromeOptions co = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4445"), co);

下面是启动容器的 docker 代码:

docker run --name chromecontainer -d -p 4445:4444 -p 5901:5900 --shm-size="2g" selenium/standalone-chrome-debug:latest

它正在跳过测试并抛出以下错误:

FAILED CONFIGURATION: @BeforeMethod bmeth(org.testng.TestRunner@fcdb78, org.testng.xml.XmlTest@769036db, public void testing.TC_005.vabc() throws java.lang.Exception, [], [TestResult name={null} status=CREATED method=TC_005.vabc()[pri:0, instance:testing.TC_005@1e3708] output={null}])
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Unable to parse remote response: <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="/assets/displayhelpservlet.css" media="all"/>
  <link href="/assets/favicon.ico" rel="icon" type="image/x-icon" />
  <script src="/assets/jquery-3.1.1.min.js" type="text/javascript"></script>
  <script src="/assets/displayhelpservlet.js" type="text/javascript"></script>
  <script type="text/javascript">
    var json = Object.freeze('{"consoleLink": "\u002fwd\u002fhub","type": "Standalone","class": "org.openqa.grid.web.servlet.DisplayHelpHandler$DisplayHelpServletConfig","version": "3.141.59"}');
  </script>
</head>
<body>

Caused by: org.openqa.selenium.json.JsonException: Unable to parse: <!DOCTYPE html>
Caused by: org.openqa.selenium.json.JsonException: Unable to determine type from: <. Last 1 characters read: <

但是如果我在远程 url 中添加/wd/hub,那么测试运行正常。

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4445/wd/hub"), co);

我试图搜索它的原因,但找不到。

如果我们不添加 /wd/hub,任何人都可以解释为什么它会抛出错误,因为根据 Selenium doc https://github.com/SeleniumHQ/docker-selenium#quick-start,它提到它不再需要?

【问题讨论】:

    标签: docker selenium-webdriver selenium-grid


    【解决方案1】:

    我刚刚看了selenium/standalone-chrome-debug:latest第16层,有以下命令:

    RUN /bin/sh -c mkdir -p /opt/selenium /var/run/supervisor /var/log/supervisor   && touch /opt/selenium/config.json   && chmod -R 777 /opt/selenium /var/run/supervisor /var/log/supervisor /etc/passwd   && wget --no-verbose https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar     -O /opt/selenium/selenium-server-standalone.jar   && chgrp -R 0 /opt/selenium ${HOME} /var/run/supervisor /var/log/supervisor   && chmod -R g=u /opt/selenium ${HOME} /var/run/supervisor /var/log/supervisor # buildkit
    

    所以看起来引擎盖下有 Grid 3,所以您必须添加 /wd/hub,这在 Grid 4 中变为可选。

    【讨论】:

    • 那么,如何检查我使用的是 Grid 3 还是 Grid 4 呢?
    • 另外,如果我使用非调试的独立 chrome 容器,那么它可以在不添加 /wd/hub 到远程 url 的情况下运行良好...有什么原因吗?
    • 它必须在日志文件中的某处打印版本(又名容器输出)。像Started Selenium node 4.0.0-rc-2 和其他类似的东西。常规 chrome 图像确实使用了 Grid 4(与调试版本不同)。原因最好从维护它们的人那里知道。可能在 Selenium Slack 频道中。
    • 非常感谢...
    猜你喜欢
    • 2010-11-27
    • 2023-03-10
    • 2022-08-22
    • 1970-01-01
    • 2019-01-14
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多