Skip to content

Instantly share code, notes, and snippets.

@netraagal
Last active March 16, 2018 15:45
Show Gist options
  • Save netraagal/1f6705b664add318de5af3abc8e8038a to your computer and use it in GitHub Desktop.
Save netraagal/1f6705b664add318de5af3abc8e8038a to your computer and use it in GitHub Desktop.
Symfony, Behat, MinkExtension & Selenium2 Driver - installation

Selenium2 driver with Behat & MinkExtension in a Symfony project

In the MinkExtension, there is differents drivers (more here).

Each one have it's caracteristics.

Generally, the Goutte driver is used to run, but there is more and more websites which have Js to be more interactive.

Goutte driver cannot run JS (it only get the page with it's raw text and do what you want it to do).

But others drivers can, as Selenium, not only get the code as a raw text, but even everything which make it run (scripts, events, ...). Its usefullness is here.

install package

composer require behat/mink-selenium2-driver

or add in your composer.json

{

    "require": {
    
        "php": "^7.0.8",
        
        "behat/behat": "^3.4",
        
        "behat/mink-extension": "^2.3",
        
        "behat/mink-selenium2-driver": "^1.3",
        
    }
    
}

and run composer update

behat.yml settings

default:

  translation:
  
    locale: en
    
  extensions:
  
    Behat\MinkExtension:
    
      default_session: goutte # default, as I don't need selenium2 for tests where there is no js
      
      browser_name: chrome # I choose chrome to work (default is firefox if I remember correctly)
      
      goutte: ~
      
      selenium2:
      
        wd_host: localhost:4444/wd/hub # here is the default setting used by Selenium2 driver (see it's constructor)     

In your file.feature files

If you need to use the Selenium driver and not default Goutte driver, just use the tag @javascript before you Scenario. By default, the correct driver will be triggered.

@javascript

Scenario: some test

Given I am on the homepage

Then  I should see "some text"

Others Scenarios without the tag will use the Goutte driver (a little bit faster).

Scenario: another test

  Given I am on the homepage
  
  Then  I should see "the same text"

FeatureContext.php

You will surely develop functions which will use the Selenuim driver (as it provide more possibilities). Don't forget to add a protection to those functions, to escape the possibility of use them with another driver.

use Behat\Mink\Driver\Selenium2Driver;

class FeatureContext

{

  /**
  
   * @When /^I want to do something with my "(?P<value>[^"]+)"$/
   
   */
   
  public function iWantToDoSometingWith(string value): self
  
  {
  
    if (!$this->getSession()->getDriver() instanceOf Selenium2Driver)
    
      return $this;
     
    // your code
    
    return $this;
    
  }
 
}

If not, you will receive a pretty Exception "The driver you use does not support that, blabla".

Or if you are unlucky, any other Exception which can say anything

("The element you want is not visible", "Impossible to access that", ...).

(I am one of those unluckies)

use of TightVNC

TightVNC is a pretty useful tool which allows you to see what is actually done.

You see at real time your Scenarios run (pretty cool I think :))

Go here

Install the tool, and you just have to insert the url of your app.

(The default password is 'secret')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment