Skip to content

Instantly share code, notes, and snippets.

@sminnee
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/fbf1bf991a112a53b185 to your computer and use it in GitHub Desktop.
Save sminnee/fbf1bf991a112a53b185 to your computer and use it in GitHub Desktop.
Behat and SilverStripe
# Behat integration test setup (see behat.org).
# More information about running these tests can be found under
# https://github.com/silverstripe-labs/silverstripe-behat-extension.
# It is safe to remove this file for normal website operation.
default:
filters:
tags: "~@todo"
formatter:
name: pretty
extensions:
SilverStripe\BehatExtension\MinkExtension:
# Adjust "base_url" to your own website URL.
# Can be set via environment variables or _ss_environment.php/$_FILE_TO_URL_MAPPING as well.
#
# base_url: http://localhost/
# TODO Dynamically set through LocatorProcessor
files_path: %behat.paths.base%/framework/tests/behat/features/files/
default_session: selenium2
javascript_session: selenium2
selenium2:
browser: firefox
SilverStripe\BehatExtension\Extension: ~
chrome:
filters:
tags: "~@todo"
formatter:
name: pretty
extensions:
SilverStripe\BehatExtension\MinkExtension:
# Adjust "base_url" to your own website URL.
# Can be set via environment variables or _ss_environment.php/$_FILE_TO_URL_MAPPING as well.
#
# base_url: http://localhost/
# TODO Dynamically set through LocatorProcessor
files_path: %behat.paths.base%/framework/tests/behat/features/files/
default_session: selenium2
javascript_session: selenium2
selenium2:
browser: chrome
SilverStripe\BehatExtension\Extension: ~
<?php
namespace Mysite\Test\Behaviour;
use SilverStripe\BehatExtension\Context\SilverStripeContext,
SilverStripe\BehatExtension\Context\BasicContext,
SilverStripe\BehatExtension\Context\LoginContext,
SilverStripe\BehatExtension\Context\FixtureContext,
SilverStripe\BehatExtension\Context\EmailContext;
//SilverStripe\Framework\Test\Behaviour\CmsFormsContext,
//SilverStripe\Framework\Test\Behaviour\CmsUiContext;
// PHPUnit
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
/**
* Features context
*
* Context automatically loaded by Behat.
* Uses subcontexts to extend functionality.
*/
class FeatureContext extends SilverStripeContext {
/**
* @var FixtureFactory
*/
protected $fixtureFactory;
/**
* 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) {
parent::__construct($parameters);
$this->useContext('BasicContext', new BasicContext($parameters));
$this->useContext('LoginContext', new LoginContext($parameters));
// $this->useContext('CmsFormsContext', new CmsFormsContext($parameters));
// $this->useContext('CmsUiContext', new CmsUiContext($parameters));
// $this->useContext('EmailContext', new EmailContext($parameters));
$fixtureContext = new FixtureContext($parameters);
$fixtureContext->setFixtureFactory($this->getFixtureFactory());
$this->useContext('FixtureContext', $fixtureContext);
// Use blueprints to set user name from identifier
$factory = $fixtureContext->getFixtureFactory();
$blueprint = \Injector::inst()->create('FixtureBlueprint', 'Member');
$blueprint->addCallback('beforeCreate', function($identifier, &$data, &$fixtures) {
if(!isset($data['FirstName'])) $data['FirstName'] = $identifier;
});
$factory->define('Member', $blueprint);
}
public function setMinkParameters(array $parameters) {
parent::setMinkParameters($parameters);
if(isset($parameters['files_path'])) {
$this->getSubcontext('FixtureContext')->setFilesPath($parameters['files_path']);
}
}
/**
* @return FixtureFactory
*/
public function getFixtureFactory() {
if(!$this->fixtureFactory) {
$this->fixtureFactory = \Injector::inst()->create('BehatFixtureFactory');
}
return $this->fixtureFactory;
}
public function setFixtureFactory(FixtureFactory $factory) {
$this->fixtureFactory = $factory;
}
}

Selenium

First, we will download selenium.

It can be nice to put it in its down directory, such as this. On Windows, choose your own:

mkdir ~/selenium
cd ~/selenium

Then download this link. 'wget' is an OSX download tool, otherwise just download this link and put it in your directory

wget http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar

And the chrome driver of your OS from here:

http://chromedriver.storage.googleapis.com/index.html?path=2.8/

Then, in that directory, run this command to start selenium locally:

java -jar selenium-server-standalone-2.44.0.jar -Dwebdriver.chrome.driver=chromedriver

Leave this running in a separate tab and open a new terminal tab to keep working.

Behat extesnion

In your project, add the composer module:

composer require silverstripe/behat-extension:*

Configuration

Put the attached behat.yml into the root of your project. It will give you both Chrome and Firefox support.

Running the tests

Now that you have everything, you can run the tests in Chrome like this:

vendor/bin/behat @framework --profile chrome

Writing your own test

  • Create mysite/tests/behat/, mysite/tests/behat/features, mysite/tests/behat/features/bootstrap
  • Copy my-site.feature into mysite/tests/behat/features
  • Copy FeatureContext.php into mysite/tests/behat/features/bootstrap
# features/login.feature
Feature: Site
As an site owner
I want to know that the basics of my CMS and site work
So that I can add new content pages with ease
Scenario: Nav
Given a "page" "Home" with "URL"="home" and "Content"="Welcome"
And the "page" "Home" is published
And a "page" "About us" with "URL"="about-us" and "Content"="This is my site"
And the "page" "About us" is published
When I go to "/"
Then I should see "About us"
When I go to "about-us"
Then I should see "This is my site"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment