org.openqa.selenium.NoSuchSessionException:无法在Docker容器中使用Behat / Mink和Selenium2Driver找到ID错误测试会话

问题描述

我正在尝试使用Behat/MinkSelenium2Driver测试Symfony3 Web应用程序,以便也可以测试Javascript功能

该应用程序在docker容器中运行,因此我按照here所述为selenium-hub和chrome添加一个新的docker容器:

# docker-compose.yml
version: '3.5' # Docker Engine release 17.12.0+

networks:
    servicesnet:
      driver: bridge

services:
   apache:
      build:
         context: './apache2'
      container_name: apache-service
      ports:
         - "80:80"
         - "443:443"
      tty: true
      networks:
         - servicesnet
      volumes:
         - ${HOST_APACHE_CONfig}:/etc/apache2
         - ${HOST_PAGES_PATH}:/var/www/localhost/htdocs

   selenium-hub:
      image: selenium/hub:4.0.0-alpha-6-20200730
      container_name: selenium-hub
      ports:
         - "4444:4444"
      networks:
         - servicesnet

   chrome:
      image: selenium/node-chrome:4.0.0-alpha-6-20200730
      volumes:
         - /dev/shm:/dev/shm
      depends_on:
         - selenium-hub
      environment:
         - HUB_HOST=selenium-hub
      networks:
         - servicesnet

当我运行docker-compose up时,它将为新容器输出

chrome          | 2020-08-12 07:36:19,917 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
chrome          | 2020-08-12 07:36:19,918 INFO supervisord started with pid 7
selenium-hub    | 2020-08-12 07:36:19,297 INFO Included extra file "/etc/supervisor/conf.d/selenium-grid-hub.conf" during parsing
selenium-hub    | 2020-08-12 07:36:19,298 INFO supervisord started with pid 7
selenium-hub    | 2020-08-12 07:36:20,301 INFO spawned: 'selenium-grid-hub' with pid 10
selenium-hub    | Starting Selenium Grid Hub...
selenium-hub    | 2020-08-12 07:36:20,311 INFO success: selenium-grid-hub entered RUNNING state,process has stayed up for > than 0 seconds (startsecs)
selenium-hub    | 07:36:20.588 INFO [LoggingOptions.getTracer] - Using OpenTelemetry for tracing
selenium-hub    | 07:36:20.589 INFO [LoggingOptions.createTracer] - Using OpenTelemetry for tracing
selenium-hub    | 07:36:20.607 INFO [EventBusOptions.createBus] - Creating event bus: org.openqa.selenium.events.zeromq.ZeroMqEventBus
selenium-hub    | 07:36:20.638 INFO [BoundZmqEventBus.<init>] - XPUB binding to [binding to tcp://*:4442,advertising as tcp://172.28.0.3:4442],XSUB binding to [binding to tcp://*:4443,advertising as tcp://172.28.0.3:4443]
selenium-hub    | 07:36:20.676 INFO [UnboundZmqEventBus.<init>] - Connecting to tcp://172.28.0.3:4442 and tcp://172.28.0.3:4443
selenium-hub    | 07:36:20.680 INFO [UnboundZmqEventBus.<init>] - Sockets created
selenium-hub    | 07:36:20.681 INFO [UnboundZmqEventBus.lambda$new$2] - Bus started
chrome          | 2020-08-12 07:36:21,136 INFO success: xvfb entered RUNNING state,process has stayed up for > than 0 seconds (startsecs)
chrome          | 2020-08-12 07:36:21,136 INFO success: fluxBox entered RUNNING state,136 INFO success: vnc entered RUNNING state,137 INFO success: selenium-node entered RUNNING state,process has stayed up for > than 0 seconds (startsecs)
selenium-hub    | 07:36:21.308 INFO [Hub.execute] - Started Selenium hub 4.0.0-alpha-6 (revision 5f43a29cfc): http://172.28.0.3:4444
chrome          | 07:36:21.774 INFO [LoggingOptions.getTracer] - Using OpenTelemetry for tracing
chrome          | 07:36:21.775 INFO [LoggingOptions.createTracer] - Using OpenTelemetry for tracing
chrome          | 07:36:21.791 INFO [EventBusOptions.createBus] - Creating event bus: org.openqa.selenium.events.zeromq.ZeroMqEventBus
chrome          | 07:36:21.829 INFO [UnboundZmqEventBus.<init>] - Connecting to tcp://selenium-hub:4442 and tcp://selenium-hub:4443
chrome          | 07:36:21.857 INFO [UnboundZmqEventBus.<init>] - Sockets created
chrome          | 07:36:21.859 INFO [UnboundZmqEventBus.lambda$new$2] - Bus started
chrome          | 07:36:22.121 INFO [NodeServer.execute] - Reporting self as: http://172.28.0.5:5555
chrome          | 07:36:22.175 INFO [NodeOptions.report] - Adding Chrome for {"browserName": "chrome"} 8 times
chrome          | 07:36:22.298 INFO [NodeServer.execute] - Started Selenium node 4.0.0-alpha-6 (revision 5f43a29cfc): http://172.28.0.5:5555
chrome          | 07:36:22.302 INFO [NodeServer.execute] - Starting registration process for node id ff0154a7-ed4b-438a-887c-0a7f3a988cb4
selenium-hub    | 07:36:22.355 INFO [Localdistributor.refresh] - Creating a new remote node for http://172.28.0.5:5555
selenium-hub    | 07:36:22.763 INFO [Localdistributor.add] - Added node ff0154a7-ed4b-438a-887c-0a7f3a988cb4.
selenium-hub    | 07:36:22.770 INFO [Host.lambda$new$0] - Changing status of node ff0154a7-ed4b-438a-887c-0a7f3a988cb4 from DOWN to UP. Reason: http://172.28.0.5:5555 is ok
chrome          | 07:36:22.774 INFO [NodeServer.lambda$execute$0] - Node has been added

然后我为每个测试提供下一个方法

<?PHP

namespace Tests\AppBundle\Controller;

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

abstract class BaseControllerTest extends WebTestCase
{
    /**
     * @var Client
     */
    protected $client;
    /**
     * @var Session
     */
    protected $session;

    public function visitUri($uri)
    {
        $this->client = static::createClient();
        $pass = $this->client->getKernel()->getContainer()->getParameter('http_basic_auth_pass');
        $host = 'localhost'; // I've tried several things here (like 172.28.0.5:5555)

        $driver = new Selenium2Driver('chrome');
        $mink = new Mink(array(
            'chrome' => new Session($driver)
        ));
        $driver->setTimeouts(['page load' => 900000]);

        $mink->setDefaultSessionName('chrome');

        $this->session = $mink->getSession();

        $this->session->visit('http://user:' . $pass . '@' . $host . $uri);
    }
}

我从特定的测试中调用方法

    public function testClickOnSearch()
    {
        $this->visitUri(/mi-custom-uri);
        $page = $this->session->getPage();

        $this->session->wait(
            200000,"typeof jQuery !== 'undefined'"
        );

        $page->findButton('Buton text')->click();
        $this->assertContains('my-custom-uri-2',$this->session->getcurrenturl());
    }

但是我从没有开始会话。如果我转到http://localhost:4444/wd/hub/session/url,则会看到此错误消息:

“ org.openqa.selenium.NoSuchSessionException:无法找到ID为url的会话:n \ n内部版本信息:版本:'4.0.0-alpha-6',修订版:'5f43a29cfc'\ n系统信息:主机:'fca78c7f81e6' ,ip:“ 172.28.0.3”,操作系统名称:“ Linux”,os.arch:“ amd64”,操作系统版本:“ 5.4.0-42-generic”,java.version:“ 1.8.0_252” \ nDriver信息:driver.version:未知”

执行测试,在200秒后抛出此错误

PHP致命错误:在null上调用成员函数click()

我确定有些东西丢失了,但不知道是什么。有想法吗?

解决方法

此错误消息...

org.openqa.selenium.NoSuchSessionException: Unable to find session with ID: url\n
Build info: version: '4.0.0-alpha-6',revision: '5f43a29cfc'\n
System info: host: 'fca78c7f81e6',ip: '172.28.0.3',os.name: 'Linux',os.arch: 'amd64',os.version: '5.4.0-42-generic',java.version: '1.8.0_252'\n
Driver info: driver.version: unknown

...表示 ChromeDriver 无法启动/产生新的浏览上下文 Chrome浏览器会话,该会话反映在记录为:

Driver info: driver.version: unknown

因此,如果继续前进,您会看到错误:

PHP Fatal error: Call to a member function click() on null

,最可能的原因是所使用的二进制文件版本之间的不兼容


解决方案

确保:

  • ChromeDriver 已更新为当前的ChromeDriver v84.0级别。
  • Chrome 已更新为当前的 Chrome版本84.0 级别。 (根据ChromeDriver v84.0 release notes
  • 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA。
  • 进行系统重启
  • 始终在driver.quit()方法内调用tearDown(){},以优雅地关闭和销毁 WebDriver Web Client 实例。

参考文献

您可以在以下位置找到几个相关的详细讨论: