Skip to content

Instantly share code, notes, and snippets.

@Im0rtality
Last active December 25, 2015 05:09
Show Gist options
  • Select an option

  • Save Im0rtality/6922571 to your computer and use it in GitHub Desktop.

Select an option

Save Im0rtality/6922571 to your computer and use it in GitHub Desktop.
Stuff for Behat/Mink presentation
phantom:
extensions:
Behat\MinkExtension\Extension:
base_url: 'http://wikipedia.com'
selenium2:
wd_host: "http://localhost:8643/wd/hub"
javascript_session: selenium2
default_session: selenium2
show_cmd: chrome "%s"
default:
extensions:
Behat\MinkExtension\Extension:
base_url: 'http://wikipedia.com'
selenium2: ~
default_session: selenium2
show_cmd: chrome "%s"
{
"require-dev": {
"behat/behat": "2.4.*@stable",
"phpunit/phpunit": "3.7.*",
"behat/mink-extension": "*",
"behat/mink-selenium2-driver": "*"
},
"config": {
"bin-dir": "bin/"
}
}
<?php
/**
* Load scripts and all other stuff in here.
* For example - doctrine's EntityManager
*/
define("ROOT", __DIR__ . '/../..');
define("APP_ROOT", ROOT . '/app');
define("SRC_ROOT", ROOT . '/src');
// Composer magic
require_once ROOT . "/vendor/autoload.php";
<?php
use Behat\MinkExtension\Context\MinkContext;
/**
* Features context.
*/
class ClientContext extends MinkContext
{
/**
* @Given /^wait (\d+) seconds$/
*/
public function waitSeconds($seconds)
{
$this->getSession()->wait(1000 * $seconds);
}
}
<?php
use Behat\Behat\Context\BehatContext;
require_once "bootstrap.php";
require_once "ClientContext.php";
// Required for assert*() methods for Behat, however Mink has these, but in own context only
require_once ROOT . '/vendor/PHPUnit/phpunit/PHPUnit/Framework/Assert/Functions.php';
/**
* Features context.
*/
class FeatureContext extends BehatContext
{
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters)
{
// Don't crap everything into one place
$this->useContext('client_context', new ClientContext($parameters));
}
}
# Example feature code, taken from other project
Feature: My Feature
In order to blahblahblah
As a blahblahblah
I need it to blahblahblah
Scenario: Do not remember login when not needed
Given I am on "#/splash"
And wait 5 seconds
Then I am on "#/login"
Then show last response
Then I fill in "username" with "test"
And I fill in "password" with "test"
When I press "Sign in"
Then I should be on "#/home"
When I am on "#/splash"
Then wait 5 seconds
Then I am on "#/login"

Installation

Prepare

$ composer update

Before running need to start one of "browsers":

$ java -jar selenium-server-standalone-2.35.0.jar

or

$ phantomjs --webdriver=8643

Running tests

$ bin/behat --ansi features/MyFeature.feature --profile phantom

or

$ bin/behat --ansi features/MyFeature.feature

Selenium2?

Good luck testing rich web application (think AngularJS) using simple HTTP request parsing those headless PHP drivers offer. Here PhantomJS helps. A little. It is much faster than Firefox, does not need to start up - saves time in general. However, tests optimized for Selenium2 @PhantomJS might fail on Selenium2 @real-browser, because those relatively small waits are not enough for loading + rendering + javascripts magic.

HAX

Can we use followings? Yes, I know about need to kill those somehow later, but all in single terminal window

$ phantomjs --webdriver=8643 >> /dev/null &
$ java -jar selenium-server-standalone-2.35.0.jar >> /dev/null &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment