Behat/Mink with Selenium 2 throws "Could not open connection"

CBergau picture CBergau · Jan 19, 2014 · Viewed 10.6k times · Source

I've started writing Acceptance Tests with Behat, Mink Extension, Selenium 2 and Symfony. I've followed the instructions, which can be seen here: http://extensions.behat.org/mink/

If i want to start my first feature with "./bin/behat" it says:

christian@christian-VirtualBox:/var/www/wiglee$ ./bin/behat 
Feature: User Registration
  In order to use ProjectNameHere
  As a user
  I should be able to register

  @javascript
  Scenario: User Registration                 # features/user.registration.feature:7
    Given I register as "[email protected]" # InheritedFeatureContext::registerAs()
      Could not open connection
    And I logout                              # InheritedFeatureContext::logout()

1 scenario (1 failed)
2 steps (1 skipped, 1 failed)

I started the selenium server, and the output tells me the connection should be okay:

INFO: Got a request to create a new session: {platform=ANY, browserVersion=8, browserName=firefox, deviceType=tablet, name=Behat test, selenium-version=2.31.0, browser=firefox, deviceOrientation=portrait, version=8, max-duration=300}

How i started Selenium Hub:

java -jar ./selenium-server-standalone-2.39.0.jar -role hub

How i added a Selenium Node (without the '.' between local and host):

java -jar ./selenium-server-standalone-2.39.0.jar -role node -hub http://local.host:4444/grid/register

Here is my behat.yml:

default:
  context:
    parameters:
      browser: firefox
    class:  'InheritedFeatureContext'
  extensions:
    SensioLabs\Behat\PageObjectExtension\Extension: ~
    Behat\MinkExtension\Extension:
      base_url:  'http://localhost/nameofproject/web/app_dev.php'
      selenium2: ~
      default_session: 'selenium2'

# No context:
no_context:
  paths:
    bootstrap: 'NON_EXISTING_FOLDER'
  filters:
    tags:      '~@javascript'

# Context based on inheritance:
inheritance:
  context:
    class:      'InheritedFeatureContext'

# Context based on traits:
traits:
  paths:
    bootstrap:  'features/php54_bootstrap'
  context:
    class:      'TraitedFeatureContext'

# Context based on subcontexting:
subcontexts:
  context:
    class:      'SubcontextedFeatureContext'

Here is the verbose output:

Given I register as "[email protected]" # InheritedFeatureContext::registerAs()
      exception 'WebDriver\Exception\UnknownError' with message 'Error forwarding the new session Empty pool of VM for setup {platform=ANY, browserVersion=8, browserName=firefox, deviceType=tablet, name=Behat test, selenium-version=2.31.0, browser=firefox, deviceOrientation=portrait, version=8, max-duration=300}' in vendor/instaclick/php-webdriver/lib/WebDriver/Exception.php:157
      Stack trace:
      #0 vendor/instaclick/php-webdriver/lib/WebDriver/AbstractWebDriver.php(140): WebDriver\Exception::factory(13, 'Error forwardin...')
      #1 vendor/instaclick/php-webdriver/lib/WebDriver/WebDriver.php(76): WebDriver\AbstractWebDriver->curl('POST', '/session', Array, Array)
      #2 vendor/behat/mink-selenium2-driver/src/Behat/Mink/Driver/Selenium2Driver.php(260): WebDriver\WebDriver->session('firefox', Array)
      #3 vendor/behat/mink/src/Behat/Mink/Session.php(62): Behat\Mink\Driver\Selenium2Driver->start()
      #4 vendor/behat/mink/src/Behat/Mink/Mink.php(114): Behat\Mink\Session->start()
      #5 vendor/sensiolabs/behat-page-object-extension/src/SensioLabs/Behat/PageObjectExtension/Context/PageFactory.php(71): Behat\Mink\Mink->getSession()
      #6 vendor/sensiolabs/behat-page-object-extension/src/SensioLabs/Behat/PageObjectExtension/Context/PageObjectContext.php(27): SensioLabs\Behat\PageObjectExtension\Context\PageFactory->createPage('Registration')
      #7 features/bootstrap/InheritedFeatureContext.php(27): SensioLabs\Behat\PageObjectExtension\Context\PageObjectContext->getPage('Registration')
      #8 [internal function]: InheritedFeatureContext->registerAs('cbergau86@gmail...')
      #9 vendor/behat/behat/src/Behat/Behat/Definition/Annotation/Definition.php(155): call_user_func_array(Array, Array)
      #10 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(157): Behat\Behat\Definition\Annotation\Definition->run(Object(InheritedFeatureContext))
      #11 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(126): Behat\Behat\Tester\StepTester->executeStepDefinition(Object(Behat\Gherkin\Node\StepNode), Object(Behat\Behat\Definition\Annotation\Given))
      #12 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(95): Behat\Behat\Tester\StepTester->executeStep(Object(Behat\Gherkin\Node\StepNode))
      #13 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\StepTester->visit(Object(Behat\Gherkin\Node\StepNode))
      #14 vendor/behat/behat/src/Behat/Behat/Tester/ScenarioTester.php(148): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\StepTester))
      #15 vendor/behat/behat/src/Behat/Behat/Tester/ScenarioTester.php(87): Behat\Behat\Tester\ScenarioTester->visitStep(Object(Behat\Gherkin\Node\StepNode), Object(Behat\Gherkin\Node\ScenarioNode), Object(InheritedFeatureContext), Array, false)
      #16 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\ScenarioTester->visit(Object(Behat\Gherkin\Node\ScenarioNode))
      #17 vendor/behat/behat/src/Behat/Behat/Tester/FeatureTester.php(88): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\ScenarioTester))
      #18 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\FeatureTester->visit(Object(Behat\Gherkin\Node\FeatureNode))
      #19 vendor/behat/behat/src/Behat/Behat/Console/Command/BehatCommand.php(150): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\FeatureTester))
      #20 vendor/behat/behat/src/Behat/Behat/Console/Command/BehatCommand.php(128): Behat\Behat\Console\Command\BehatCommand->runFeatures(Object(Behat\Gherkin\Gherkin))
      #21 vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php(241): Behat\Behat\Console\Command\BehatCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #22 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(881): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #23 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(Behat\Behat\Console\Command\BehatCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #24 vendor/behat/behat/src/Behat/Behat/Console/BehatApplication.php(68): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #25 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(121): Behat\Behat\Console\BehatApplication->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #26 vendor/behat/behat/bin/behat(32): Symfony\Component\Console\Application->run()
      #27 {main}

      Next exception 'Behat\Mink\Exception\DriverException' with message 'Could not open connection' in vendor/behat/mink-selenium2-driver/src/Behat/Mink/Driver/Selenium2Driver.php:262
      Stack trace:
      #0 vendor/behat/mink/src/Behat/Mink/Session.php(62): Behat\Mink\Driver\Selenium2Driver->start()
      #1 vendor/behat/mink/src/Behat/Mink/Mink.php(114): Behat\Mink\Session->start()
      #2 vendor/sensiolabs/behat-page-object-extension/src/SensioLabs/Behat/PageObjectExtension/Context/PageFactory.php(71): Behat\Mink\Mink->getSession()
      #3 vendor/sensiolabs/behat-page-object-extension/src/SensioLabs/Behat/PageObjectExtension/Context/PageObjectContext.php(27): SensioLabs\Behat\PageObjectExtension\Context\PageFactory->createPage('Registration')
      #4 features/bootstrap/InheritedFeatureContext.php(27): SensioLabs\Behat\PageObjectExtension\Context\PageObjectContext->getPage('Registration')
      #5 [internal function]: InheritedFeatureContext->registerAs('cbergau86@gmail...')
      #6 vendor/behat/behat/src/Behat/Behat/Definition/Annotation/Definition.php(155): call_user_func_array(Array, Array)
      #7 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(157): Behat\Behat\Definition\Annotation\Definition->run(Object(InheritedFeatureContext))
      #8 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(126): Behat\Behat\Tester\StepTester->executeStepDefinition(Object(Behat\Gherkin\Node\StepNode), Object(Behat\Behat\Definition\Annotation\Given))
      #9 vendor/behat/behat/src/Behat/Behat/Tester/StepTester.php(95): Behat\Behat\Tester\StepTester->executeStep(Object(Behat\Gherkin\Node\StepNode))
      #10 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\StepTester->visit(Object(Behat\Gherkin\Node\StepNode))
      #11 vendor/behat/behat/src/Behat/Behat/Tester/ScenarioTester.php(148): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\StepTester))
      #12 vendor/behat/behat/src/Behat/Behat/Tester/ScenarioTester.php(87): Behat\Behat\Tester\ScenarioTester->visitStep(Object(Behat\Gherkin\Node\StepNode), Object(Behat\Gherkin\Node\ScenarioNode), Object(InheritedFeatureContext), Array, false)
      #13 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\ScenarioTester->visit(Object(Behat\Gherkin\Node\ScenarioNode))
      #14 vendor/behat/behat/src/Behat/Behat/Tester/FeatureTester.php(88): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\ScenarioTester))
      #15 vendor/behat/gherkin/src/Behat/Gherkin/Node/AbstractNode.php(42): Behat\Behat\Tester\FeatureTester->visit(Object(Behat\Gherkin\Node\FeatureNode))
      #16 vendor/behat/behat/src/Behat/Behat/Console/Command/BehatCommand.php(150): Behat\Gherkin\Node\AbstractNode->accept(Object(Behat\Behat\Tester\FeatureTester))
      #17 vendor/behat/behat/src/Behat/Behat/Console/Command/BehatCommand.php(128): Behat\Behat\Console\Command\BehatCommand->runFeatures(Object(Behat\Gherkin\Gherkin))
      #18 vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php(241): Behat\Behat\Console\Command\BehatCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #19 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(881): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #20 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(Behat\Behat\Console\Command\BehatCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #21 vendor/behat/behat/src/Behat/Behat/Console/BehatApplication.php(68): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #22 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(121): Behat\Behat\Console\BehatApplication->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #23 vendor/behat/behat/bin/behat(32): Symfony\Component\Console\Application->run()
      #24 {main}

Here is my composer.json:

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.4.*",
    "doctrine/orm": ">=2.2.3,<2.4-dev",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*",
    "symfony/assetic-bundle": "2.3.*",
    "symfony/swiftmailer-bundle": "2.3.*",
    "symfony/monolog-bundle": "2.3.*",
    "sensio/distribution-bundle": "2.3.*",
    "sensio/framework-extra-bundle": "2.3.*",
    "sensio/generator-bundle": "2.3.*",
    "incenteev/composer-parameter-handler": "~2.0",
    "friendsofsymfony/rest-bundle": "dev-master",
    "friendsofsymfony/user-bundle": "~2.0@dev",
    "jms/serializer-bundle": "dev-master",
    "behat/symfony2-extension": "*",
    "behat/mink-extension": "*",
    "behat/mink-browserkit-driver": "*",
    "behat/mink-selenium2-driver": "*",
    "behat/mink-goutte-driver": "*",
    "phpunit/phpunit": "3.7.*",
    "sensiolabs/behat-page-object-extension": "*",
    "jms/i18n-routing-bundle": "1.1.0",
    "knplabs/knp-menu-bundle": "v1.1.2",
    "friendsofsymfony/message-bundle": "dev-master"
},

Answer

Jakub Zalas picture Jakub Zalas · Feb 2, 2014

I recently fought the same issue and managed to get it working with selenium grid.

The issue in my case was related to default capabilities Mink sends. The hub tries to find a proper node to run, which would accept requested capabilities. In my case issue was a browser version. It finally worked when I set it to an empty string:

# behat.yml
default:
  extensions:
    Behat\MinkExtension\Extension:
      base_url: 'http://localhost/'
      browser_name: phantomjs
      selenium2:
        wd_host: http://127.0.0.1:4444/wd/hub
        capabilities:
          version: ''

Solution originally posted on: Running Behat in parallel with Selenium grid