2012-11-09 17:35:25 +01:00
# SilverStripe Integration for Behat
2012-07-11 21:22:11 +02:00
2015-04-07 13:15:22 +02:00
[![Build Status ](https://travis-ci.org/silverstripe-labs/silverstripe-behat-extension.svg?branch=master )](https://travis-ci.org/silverstripe-labs/silverstripe-behat-extension)
2012-11-09 17:35:25 +01:00
## Overview
[Behat ](http://behat.org ) is a testing framework for behaviour-driven development.
2014-08-02 08:30:27 +02:00
Because it primarily interacts with your website through a browser,
2012-11-09 17:35:25 +01:00
you don't need any specific integration tools to get it going with
2014-08-02 08:30:27 +02:00
a basic SilverStripe website, simply follow the
2012-11-09 17:35:25 +01:00
[standard Behat usage instructions ](http://docs.behat.org/ ).
This extension comes in handy if you want to go beyond
interacting with an existing website and database,
for example make changes to your database content which
would need to be rolled back to a "clean slate" later on.
It provides the following helpers:
* Provide access to SilverStripe classes in your Behat contexts
* Set up a temporary database automatically
* Reset the database content on every scenario
2012-11-09 20:30:56 +01:00
* Prebuilt Contexts for SilverStripe's login forms and other common tasks
2012-11-09 17:35:25 +01:00
* Creating of member fixtures with predefined permissions
* YML fixture definitions inside your Behat scenarios
* Waiting for jQuery Ajax responses (rather than fixed wait timers)
2012-11-09 20:30:56 +01:00
* Captures JavaScript errors and logs them through Selenium
* Saves screenshots to filesystem whenever an assertion error is detected
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
In order to achieve this, the extension makes one basic assumption:
2012-11-09 17:35:25 +01:00
Your Behat tests are run from the same application as the tested
SilverStripe codebase, on a locally hosted website from the same codebase.
This is important because we need access to the underlying SilverStripe
PHP classes. You can of course use a remote browser to do the actual testing.
Note: The extension has only been tested with the `selenium2` Mink driver.
## Installation
2013-05-09 16:26:24 +02:00
Simply [install SilverStripe through Composer ](http://doc.silverstripe.org/framework/en/installation/composer ).
2013-05-09 17:07:28 +02:00
Skip this step if adding the module to an existing project.
2012-11-09 17:35:25 +01:00
2014-08-02 08:30:27 +02:00
composer create-project silverstripe/installer my-test-project 4.x-dev
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
Switch to the newly created webroot, and add the SilverStripe Behat extension.
cd my-test-project
2017-01-05 10:10:21 +01:00
composer require --dev silverstripe/behat-extension:"@stable"
2013-05-09 16:26:24 +02:00
2017-01-05 10:10:21 +01:00
Now get the latest Selenium2 server (requires [Java ](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html )):
2012-11-09 17:35:25 +01:00
2017-01-05 10:10:21 +01:00
composer require --dev se/selenium-server-standalone:"2.x@stable"
2012-11-09 17:35:25 +01:00
2016-04-20 01:56:42 +02:00
Download the latest [Firefox ESR ](https://www.mozilla.org/en-US/firefox/organizations/all/ ) (Extended Support Release).
It might be older than your currently installed Firefox.
It's important to have a browser that's [supported by Selenium-Webdriver ](http://docs.seleniumhq.org/docs/01_introducing_selenium.jsp#selenium-webdriver )
2015-02-16 01:03:18 +01:00
2013-05-09 17:07:28 +02:00
Now install the SilverStripe project as usual by opening it in a browser and following the instructions.
2014-08-02 08:30:27 +02:00
Protip: You can skip this step by using `[SS_DATABASE_CHOOSE_NAME]` in a global
[`_ss_environment.php` ](http://doc.silverstripe.org/framework/en/topics/environment-management )
2013-05-09 17:07:28 +02:00
file one level above the webroot.
2013-10-17 00:43:20 +02:00
Unless you have [`$_FILE_TO_URL_MAPPING` ](http://doc.silverstripe.org/framework/en/topics/commandline#configuration )
2013-05-09 16:26:24 +02:00
set up, you also need to specify the URL for your webroot. Either add it to the existing `behat.yml` configuration file
2014-08-02 08:30:27 +02:00
in your project root, or set is as an environment variable in your terminal session:
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
export BEHAT_PARAMS="extensions[SilverStripe\BehatExtension\MinkExtension][base_url]=http://localhost/"
2012-12-03 09:57:39 +01:00
2013-05-09 16:26:24 +02:00
## Usage
2012-12-03 09:57:39 +01:00
2016-04-04 03:17:07 +02:00
### Prevent Firefox from Automatically Updating
The moment you open Firefox, it's going to try and update itself out of the stone age. To prevent this, open a new tab and go to `about:config` . There, change the following settings to `false` :
- `app.update.auto`
- `app.update.enabled`
- `app.update.silent`
Firefox will already have started the update, so close and delete it. The settings you changed should be stored as preferences, apart from the application files you've just deleted. Reinstall that ancient version. The next time you open it, and go to "About Firefox", you should see a button desperately pleading with you to "check for updates". Don't click that if you know what's good for you...
2013-05-09 16:26:24 +02:00
### Starting the Selenium Server
2012-11-09 17:35:25 +01:00
2013-05-09 16:40:05 +02:00
You can run the server locally in a separate Terminal session:
2012-11-09 17:35:25 +01:00
2017-01-05 10:10:21 +01:00
vendor/bin/selenium-server-standalone
2015-02-16 01:03:18 +01:00
2017-01-05 10:10:21 +01:00
In some cases it may be necessary to start a specific version of Firefox
2015-02-16 01:03:18 +01:00
2017-01-05 10:10:21 +01:00
vendor/bin/selenium-server-standalone -Dwebdriver.firefox.bin="/Applications/Firefox31.app/Contents/MacOS/firefox-bin"
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
### Running the Tests
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
Now you can run the tests (for example for the `framework` module):
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
vendor/bin/behat @framework
2012-11-09 17:35:25 +01:00
2015-10-20 00:39:36 +02:00
Or even run a single scenario by it's name (supports regular expressions):
vendor/bin/behat --name 'My scenario title' @framework
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
This will start a Firefox browser by default. Other browsers and profiles can be configured in `behat.yml` .
2012-11-09 17:35:25 +01:00
2013-12-17 22:56:45 +01:00
For example, if you want to start a Chrome Browser you can following the instructions provided [here ](docs/chrome-behat.md ).
2013-11-27 19:35:14 +01:00
## Tutorials
2013-10-22 11:20:45 +02:00
2013-11-27 19:35:14 +01:00
* [Tutorial: Testing Form Submissions ](docs/tutorial.md )
* [Tutorial: Webservice Mocking with Phockito and TestSession ](docs/webservice-mocking.md )
2017-01-09 22:14:14 +01:00
* [Tutorial: Setting up Behat on CircleCI ](docs/circleci-tutorial.md )
2013-10-22 11:20:45 +02:00
2013-05-09 16:26:24 +02:00
## Configuration
2012-11-09 17:35:25 +01:00
2012-12-07 01:11:30 +01:00
The SilverStripe installer already comes with a YML configuration
2013-05-09 16:26:24 +02:00
which is ready to run tests on a locally hosted Selenium server,
located in the project root as `behat.yml` .
2012-11-09 17:35:25 +01:00
2014-08-02 08:30:27 +02:00
You should ensure that you have configured SS_BASE_URL in your `.env` .
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
Generic Mink configuration settings are placed in `SilverStripe\BehatExtension\MinkExtension` ,
which is a subclass of `Behat\MinkExtension\Extension` .
2012-11-09 17:35:25 +01:00
2012-11-14 00:29:20 +01:00
Overview of settings (all in the `extensions.SilverStripe\BehatExtension\Extension` path):
2012-11-09 17:35:25 +01:00
2012-11-14 00:29:20 +01:00
* `ajax_steps` : Because SilverStripe uses AJAX requests quite extensively, we had to invent a way
2012-11-09 17:35:25 +01:00
to deal with them more efficiently and less verbose than just
Optional `ajax_steps` is used to match steps defined there so they can be "caught" by
[special AJAX handlers ](http://blog.scur.pl/2012/06/ajax-callback-support-behat-mink/ ) that tweak the delays. You can either use a pipe delimited string or a list of substrings that match step definition.
2014-08-02 08:30:27 +02:00
* `ajax_timeout` : Milliseconds after which an Ajax request is regarded as timed out,
2012-11-14 00:29:20 +01:00
and the script continues with its assertions to avoid a deadlock (Default: 5000).
2013-10-09 14:29:42 +02:00
* `screenshot_path` : Absolute path used to store screenshot of a last known state
2014-08-02 08:30:27 +02:00
of a failed step.
2012-11-14 00:29:20 +01:00
Screenshot names within that directory consist of feature file filename and line
number that failed.
2012-11-09 17:35:25 +01:00
2014-08-02 08:30:27 +02:00
Example: behat.yml
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
default:
2014-08-02 08:30:27 +02:00
suites:
framework:
paths:
- %paths.modules.framework%/tests/behat/features
contexts:
- SilverStripe\Framework\Tests\Behaviour\FeatureContext
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\EmailContext
- SilverStripe\BehatExtension\Context\LoginContext
-
SilverStripe\BehatExtension\Context\FixtureContext:
- %paths.modules.framework%/tests/behat/features/files/
2013-05-09 16:26:24 +02:00
extensions:
2014-08-02 08:30:27 +02:00
SilverStripe\BehatExtension\MinkExtension:
default_session: selenium2
javascript_session: selenium2
selenium2:
browser: firefox
SilverStripe\BehatExtension\Extension:
screenshot_path: %paths.base%/artifacts/screenshots
2012-11-09 20:30:56 +01:00
2013-10-18 17:00:07 +02:00
## Module Initialization
You're all set to start writing features now! Simply create `*.feature` files
anywhere in your codebase, and run them as shown above. We recommend the folder
2014-08-02 08:30:27 +02:00
structure of `tests/behat/features` , since its consistent with the common location
2013-10-18 17:00:07 +02:00
of SilverStripe's PHPUnit tests.
Behat tests rely on a `FeatureContext` class which contains step definitions,
2014-08-02 08:30:27 +02:00
and can be composed of other subcontexts, e.g. for SilverStripe-specific CMS steps
2013-10-18 17:00:07 +02:00
(details on [behat.org ](http://docs.behat.org/quick_intro.html#the-context-class-featurecontext )).
Since step definitions are quite domain specific, its likely that you'll need your own context.
The SilverStripe Behat extension provides an initializer script which generates a template
in the recommended folder structure:
2014-08-02 08:30:27 +02:00
vendor/bin/behat --init @mymodule --namespace="MyVendor\MyModule"
2013-10-18 17:00:07 +02:00
2014-08-02 08:30:27 +02:00
Note: namespace is mandatory
2013-10-18 17:00:07 +02:00
2014-08-02 08:30:27 +02:00
You'll now have a class located in `mymodule/tests/behat/src/FeatureContext.php` ,
which will have a psr-4 class mapping added to composer.json by default.
Also a folder for your features with `mymodule/tests/behat/features` will be created.
A `mymodule/behat.yml` is built, with a default suite named after the module.
2013-10-18 17:00:07 +02:00
## Available Step Definitions
2012-11-09 20:30:56 +01:00
The extension comes with several `BehatContext` subclasses come with some extra step defintions.
2014-08-02 08:30:27 +02:00
Some of them are just helpful in general website testing, other's are specific to SilverStripe.
2012-11-09 20:30:56 +01:00
To find out all available steps (and the files they are defined in), run the following:
2012-11-14 15:21:12 +01:00
vendor/bin/behat @mymodule --definitions=i
2012-11-09 20:30:56 +01:00
Note: There are more specific step definitions in the SilverStripe `framework` module
for interacting with the CMS interfaces (see `framework/tests/behat/features/bootstrap` ).
2013-06-05 15:36:34 +02:00
In addition to the dynamic list, a cheatsheet of available steps can be found at the end of this guide.
2012-11-09 20:30:56 +01:00
2013-06-04 01:11:07 +02:00
## Fixtures
Since each test run creates a new database, you can't rely on existing state unless
2014-08-02 08:30:27 +02:00
you explicitly define it.
2013-06-04 01:11:07 +02:00
### Database Defaults
The easiest way to get default data is through `DataObject->requireDefaultRecords()` .
Many modules already have this method defined, e.g. the `blog` module automatically
creates a default `BlogHolder` entry in the page tree. Sometimes these defaults can
be counterproductive though, so you need to "opt-in" to them, via the `@database-defaults`
tag placed at the top of your feature definition. The defaults are reset after each
scenario automatically.
### Inline Definition
If you need more flexibility and transparency about which records are being created,
use the inline definition syntax. The following example shows some syntax variations:
Feature: Do something with pages
As an site owner
I want to manage pages
Background:
# Creates a new page without data. Can be accessed later under this identifier
2014-08-02 08:30:27 +02:00
Given a "page" "Page 1"
2013-06-04 01:11:07 +02:00
# Uses a custom RegistrationPage type
2014-08-02 08:30:27 +02:00
And an "error page" "Register"
# Creates a page with inline properties
And a "page" "Page 2" with "URLSegment"="page-1" and "Content"="my page 1"
# Field names can be tabular, and based on DataObject::$field_labels
2013-06-05 14:17:28 +02:00
And the "page" "Page 3" has the following data
2013-06-04 01:11:07 +02:00
| Content | < blink > |
| My Property | foo |
| My Boolean | bar |
# Pages are published by default, can be explicitly unpublished
2014-08-02 08:30:27 +02:00
And the "page" "Page 1" is not published
2013-06-04 01:11:07 +02:00
# Create a hierarchy, and reference a record created earlier
2014-08-02 08:30:27 +02:00
And the "page" "Page 1.1" is a child of a "page" "Page 1"
# Specific page type step
And a "page" "My Redirect" which redirects to a "page" "Page 1"
2013-06-05 14:17:28 +02:00
And a "member" "Website User" with "FavouritePage"="=>Page.Page 1"
2013-06-04 01:11:07 +02:00
@javascript
Scenario: View a page in the tree
Given I am logged in with "ADMIN" permissions
And I go to "/admin/pages"
Then I should see "Page 1" in CMS Tree
* Fixtures are created where you defined them. If you want the fixtures to be created
2014-08-02 08:30:27 +02:00
before every scenario, define them in [Background ](http://docs.behat.org/guides/1.gherkin.html#backgrounds ).
2013-06-04 01:11:07 +02:00
If you want them to be created only when a particular scenario runs, define them there.
2014-08-02 08:30:27 +02:00
* Fixtures are cleared between scenarios.
2013-06-04 01:11:07 +02:00
* The basic syntax works for all `DataObject` subclasses, but some specific
notations like "is not published" requires extensions like `Hierarchy` to be applied to the class
2013-06-05 15:44:30 +02:00
* Record types, identifiers, property names and property values need to be quoted
* Record types (class names) can use more natural notation ("registration page" instead of "Registration Page")
* Record types support the `$singular_name` notation which is also used to reference the types throughout the CMS.
Record property names support the `$field_labels` notation in the same fashion.
2013-06-04 01:11:07 +02:00
* Property values may also use a `=>` symbol to indicate relationships between records.
The notation is `=><classname>.<identifier>` . For `has_many` or `many_many` relationships,
multiple relationships can be separated by a comma.
2012-11-09 17:35:25 +01:00
2013-05-09 16:26:24 +02:00
## Writing Behat Tests
2012-11-14 15:21:12 +01:00
2013-05-09 16:26:24 +02:00
### Directory Structure
2012-11-14 15:21:12 +01:00
2013-05-09 16:26:24 +02:00
As a convention, SilverStripe Behat tests live in a `tests/behat` subfolder
2014-08-02 08:30:27 +02:00
of your module. You can create it with the following commands:
2012-11-14 15:21:12 +01:00
2014-08-02 08:30:27 +02:00
mkdir -p mymodule/tests/behat/features/
mkdir -p mymodule/tests/behat/src/
2012-11-14 15:21:12 +01:00
2013-05-09 16:26:24 +02:00
### FeatureContext
The generic [Behat usage instructions ](http://docs.behat.org/ ) apply
here as well. The only major difference is the base class from which
to extend your own `FeatureContext` : It should be `SilverStripeContext`
rather than `BehatContext` .
2014-08-02 08:30:27 +02:00
Example: mymodule/tests/behat/src/FeatureContext.php
2013-05-09 16:26:24 +02:00
< ?php
namespace MyModule\Test\Behaviour;
2014-08-02 08:30:27 +02:00
use SilverStripe\BehatExtension\Context\SilverStripeContext;
2013-05-09 16:26:24 +02:00
class FeatureContext extends SilverStripeContext
{
}
2012-11-14 15:21:12 +01:00
2014-02-24 23:33:12 +01:00
### Screen Size
2014-08-02 08:30:27 +02:00
In some Selenium drivers you can
2014-02-24 23:33:12 +01:00
define the desired browser window size through a `capabilities` definition.
By default, Selenium doesn't support this though, so we've added a workaround
through an environment variable:
BEHAT_SCREEN_SIZE=320x600 vendor/bin/behat
2014-03-01 02:47:29 +01:00
### Inspecting PHP sessions
Behat is executed from CLI, which in turn triggers web requests in a browser.
2014-08-02 08:30:27 +02:00
This browser session is associated PHP session information such as the logged-in user.
2014-03-01 02:47:29 +01:00
After every request, the session information is persisted on disk as part
of the `TestSessionEnvironment` , in order to share it with Behat CLI.
Example: Retrieve the currently logged-in member
2017-05-16 04:42:47 +02:00
use SilverStripe\TestSession\TestsessionEnvironment;
$env = Injector::inst()->get(TestSessionEnvironment::class);
2014-03-01 02:47:29 +01:00
$state = $env->getState();
if(isset($state->session['loggedInAs'])) {
$member = \Member::get()->byID($state->session['loggedInAs']);
} else {
$member = null;
}
2012-11-09 17:35:25 +01:00
## FAQ
2012-12-07 01:11:30 +01:00
### FeatureContext not found
This is most likely a problem with Composer's autoloading generator.
Check that you have "SilverStripe" mentioned in the `vendor/composer/autoload_classmap.php` file,
and call `composer dump-autoload` if not.
2017-01-09 23:07:12 +01:00
### How do I wait for asynchronous actions in my steps?
Sometimes you want to wait for an AJAX request or CSS animation to complete before
calling the next step/assertion. Mink provides a [wait() method ](http://mink.behat.org/en/latest/guides/session.html )
for this purpose - just let the execution wait until a JavaScript expression satisfies your criteria.
It's pretty common to make this expression a CSS selector.
The Behat tests come with built-in support to wait for any pending `jQuery.ajax()` requests,
check `BasicContext->handleAjaxBeforeStep()` and the `ajax_steps` configuration option.
2012-11-09 17:35:25 +01:00
### Why does the module need to know about the framework path on the filesystem?
Sometimes SilverStripe needs to know the URL of your site. When you're visiting
your site in a web browser this is easy to work out, but if you're executing
scripts on the command-line, it has no way of knowing.
To work this out, this module is using [file to URL mapping ](http://doc.silverstripe.org/framework/en/topics/commandline#configuration ).
### How does the module interact with the SS database?
The module creates temporary database on init and is switching to the alternative
database session before every scenario by using `/dev/tests/setdb` TestRunner
endpoint.
It also populates this temporary database with the default records if necessary.
It is possible to include your own fixtures, it is explained further.
2012-11-09 20:30:56 +01:00
### Why do tests pass in a fresh installation, but fail in my own project?
Because we're testing the interface directly, any changes to the
2014-08-02 08:30:27 +02:00
viewed elements have the potential to disrupt testing.
2012-11-09 20:30:56 +01:00
By building a test database from scratch, we're trying to minimize this impact.
Some examples where things can go wrong nevertheless:
* Thirdparty SilverStripe modules which install default data
* Changes to the default interface language
* Configurations which remove admin areas or specific fields
Currently there's no way to exclude offending modules from a test run.
You either have to adjust the tests to work around these changes,
or run tests on a "sandbox" projects without these modules.
2012-11-09 17:35:25 +01:00
### How do I debug when something goes wrong?
First, read the console output. Behat will tell you which steps have failed.
SilverStripe Behaviour Testing Framework also notifies you about some events.
It tries to catch some JavaScript errors and AJAX errors as well although it
is limited to errors that occur after the page is loaded.
Screenshot will be taken by the module every time the step is marked as failed.
Refer to configuration section above to know how to set up the screenshot path.
If you are unable to debug using the information collected with the above
methods, it is possible to delay the step execution by adding the following step:
2014-07-29 01:24:58 +02:00
And I put a breakpoint
2012-11-09 17:35:25 +01:00
2014-08-02 08:30:27 +02:00
This will stop the execution of the tests until you press the return key in the
2014-07-29 01:24:58 +02:00
terminal. This is very useful when you want to look at the error or developer console
2012-11-09 17:35:25 +01:00
inside the browser or if you want to interact with the session page manually.
2013-12-18 14:57:37 +01:00
### Can I set breakpoints through XDebug?
If you have [XDebug ](http://xdebug.org ) set up, breakpoints are your friend.
The problem is that you can only connect the debugger to the PHP execution
in the CLI, or in the browser, not both at the same time.
First of all, ensure that `xdebug.remote_autostart` is set to `Off` ,
otherwise you'll always have an active debugging session in CLI, never in the browser.
Then you can choose to enable XDebug for the current CLI run:
XDEBUG_CONFIG="idekey=macgdbp" vendor/bin/behat
Or you can use the `TESTSESSION_PARAMS` environment variable to pass additional
parameters to `dev/testsession/start` , and debug in the browser instead.
TESTSESSION_PARAMS="XDEBUG_SESSION_START=macgdbp" vendor/bin/behat @app
The `macgdbp` IDE key needs to match your `xdebug.idekey` php.ini setting.
2017-01-05 10:10:21 +01:00
### How do I set up continuous integration through Travis?
2012-11-09 17:35:25 +01:00
2017-01-05 10:10:21 +01:00
Check out the [travis.yml ](https://github.com/silverstripe/silverstripe-framework/blob/master/.travis.yml )
in `silverstripe/framework` for a good example on how to set up Behat tests through [travis-ci.org ](http://travis-ci.org ).
Note that the [Travis CI Environment ](https://docs.travis-ci.com/user/ci-environment#Firefox )
does not default to the latest [Firefox ESR ](https://www.mozilla.org/en-US/firefox/organizations/all/ ) release, but an older version.
(31.0 in January 2017). You should try to run your tests locally with the same Firefox version,
and [download the correct Firefox release ](https://ftp.mozilla.org/pub/firefox/releases/ )).
Alternatively, [configure Travis ](https://docs.travis-ci.com/user/firefox/ ) to use a newer version.
Don't forget to disable auto-updates in your Firefox settings.
2012-11-09 17:35:25 +01:00
2013-06-05 15:36:34 +02:00
## Cheatsheet
2013-06-07 16:25:34 +02:00
This is a manually categorized list of available commands
when both the `cms` and `framework` modules are installed.
It's based on the `vendor/bin/behat -di @cms` output.
2013-06-05 15:36:34 +02:00
### Basics
Then /^(?:|I )should see "(?P< text > (?:[^"]|\\")*)"$/
- Checks, that page contains specified text.
Then /^(?:|I )should not see "(?P< text > (?:[^"]|\\")*)"$/
- Checks, that page doesn't contain specified text.
Then /^(?:|I )should see text matching (?P< pattern > "(?:[^"]|\\")*")$/
- Checks, that page contains text matching specified pattern.
Then /^(?:|I )should not see text matching (?P< pattern > "(?:[^"]|\\")*")$/
- Checks, that page doesn't contain text matching specified pattern.
Then /^the response should contain "(?P< text > (?:[^"]|\\")*)"$/
- Checks, that HTML response contains specified string.
Then /^the response should not contain "(?P< text > (?:[^"]|\\")*)"$/
- Checks, that HTML response doesn't contain specified string.
Then /^(?:|I )should see "(?P< text > (?:[^"]|\\")*)" in the "(?P< element > [^"]*)" element$/
- Checks, that element with specified CSS contains specified text.
Then /^(?:|I )should not see "(?P< text > (?:[^"]|\\")*)" in the "(?P< element > [^"]*)" element$/
- Checks, that element with specified CSS doesn't contain specified text.
Then /^the "(?P< element > [^"]*)" element should contain "(?P< value > (?:[^"]|\\")*)"$/
- Checks, that element with specified CSS contains specified HTML.
Then /^(?:|I )should see an? "(?P< element > [^"]*)" element$/
- Checks, that element with specified CSS exists on page.
Then /^(?:|I )should not see an? "(?P< element > [^"]*)" element$/
- Checks, that element with specified CSS doesn't exist on page.
Then /^(?:|I )should be on "(?P< page > [^"]+)"$/
- Checks, that current page PATH is equal to specified.
Then /^the (?i)url(?-i) should match (?P< pattern > "([^"]|\\")*")$/
- Checks, that current page PATH matches regular expression.
Then /^the response status code should be (?P< code > \d+)$/
- Checks, that current page response status is equal to specified.
Then /^the response status code should not be (?P< code > \d+)$/
- Checks, that current page response status is not equal to specified.
Then /^(?:|I )should see (?P< num > \d+) "(?P< element > [^"]*)" elements?$/
- Checks, that (?P< num > \d+) CSS elements exist on the page
Then /^print last response$/
- Prints last response to console.
Then /^show last response$/
- Opens last response content in browser.
Then /^I should be redirected to "([^"]+)"/
Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/
2014-06-13 05:05:32 +02:00
Then /^the "([^"]*)" table should contain "([^"]*)"$/
Then /^the "([^"]*)" table should not contain "([^"]*)"$/
Given /^I click on "([^"]*)" in the "([^"]*)" table$/
2013-06-05 15:36:34 +02:00
### Navigation
Given /^(?:|I )am on homepage$/
- Opens homepage.
When /^(?:|I )go to homepage$/
- Opens homepage.
Given /^(?:|I )am on "(?P< page > [^"]+)"$/
- Opens specified page.
When /^(?:|I )go to "(?P< page > [^"]+)"$/
- Opens specified page.
When /^(?:|I )reload the page$/
- Reloads current page.
When /^(?:|I )move backward one page$/
- Moves backward one page in history.
When /^(?:|I )move forward one page$/
- Moves forward one page in history
### Forms
When /^(?:|I )press "(?P< button > (?:[^"]|\\")*)"$/
- Presses button with specified id|name|title|alt|value.
When /^(?:|I )follow "(?P< link > (?:[^"]|\\")*)"$/
- Clicks link with specified id|title|alt|text.
When /^(?:|I )fill in "(?P< field > (?:[^"]|\\")*)" with "(?P< value > (?:[^"]|\\")*)"$/
- Fills in form field with specified id|name|label|value.
When /^(?:|I )fill in "(?P< value > (?:[^"]|\\")*)" for "(?P< field > (?:[^"]|\\")*)"$/
- Fills in form field with specified id|name|label|value.
When /^(?:|I )fill in the following:$/
- Fills in form fields with provided table.
When /^(?:|I )select "(?P< option > (?:[^"]|\\")*)" from "(?P< select > (?:[^"]|\\")*)"$/
- Selects option in select field with specified id|name|label|value.
When /^(?:|I )additionally select "(?P< option > (?:[^"]|\\")*)" from "(?P< select > (?:[^"]|\\")*)"$/
2014-05-05 02:32:47 +02:00
- Selects additional option in select field with specified id|name|label|value.
When /^I select the "([^"]*)" radio button$/
- Selects a radio button with the given id|name|label|value
2013-06-05 15:36:34 +02:00
When /^(?:|I )check "(?P< option > (?:[^"]|\\")*)"$/
- Checks checkbox with specified id|name|label|value.
When /^(?:|I )uncheck "(?P< option > (?:[^"]|\\")*)"$/
- Unchecks checkbox with specified id|name|label|value.
When /^(?:|I )attach the file "(?P[^"]*)" to "(?P< field > (?:[^"]|\\")*)"$/
- Attaches file to field with specified id|name|label|value.
Then /^the "(?P< field > (?:[^"]|\\")*)" field should contain "(?P< value > (?:[^"]|\\")*)"$/
- Checks, that form field with specified id|name|label|value has specified value.
Then /^the "(?P< field > (?:[^"]|\\")*)" field should not contain "(?P< value > (?:[^"]|\\")*)"$/
- Checks, that form field with specified id|name|label|value doesn't have specified value.
Then /^the "(?P< checkbox > (?:[^"]|\\")*)" checkbox should be checked$/
- Checks, that checkbox with specified in|name|label|value is checked.
Then /^the "(?P< checkbox > (?:[^"]|\\")*)" checkbox should not be checked$/
- Checks, that checkbox with specified in|name|label|value is unchecked.
Given /^(?:|I )attach the file "(?P[^"]*)" to "(?P< field > (?:[^"]|\\")*)" with HTML5$/
When /^I fill in the "(?P< field > ([^"]*))" HTML field with "(?P< value > ([^"]*))"$/
When /^I fill in "(?P< value > ([^"]*))" for the "(?P< field > ([^"]*))" HTML field$/
When /^I append "(?P< value > ([^"]*))" to the "(?P< field > ([^"]*))" HTML field$/
2013-06-07 16:25:34 +02:00
Then /^the "(?P< locator > ([^"]*))" HTML field should contain "(?P< html > ([^"]*))"$/
2013-06-05 15:36:34 +02:00
2013-12-19 05:02:10 +01:00
When /^(?:|I )fill in the "(?P< field > (?:[^"]|\\")*)" dropdown with "(?P< value > (?:[^"]|\\")*)"$/
2013-06-05 15:36:34 +02:00
- Workaround for chosen.js dropdowns or tree dropdowns which hide the original dropdown field.
When /^(?:|I )fill in "(?P< value > (?:[^"]|\\")*)" for "(?P< field > (?:[^"]|\\")*)" dropdown$/
- Workaround for chosen.js dropdowns or tree dropdowns which hide the original dropdown field.
2013-11-28 11:03:38 +01:00
Given /^I select "([^"]*)" from "([^"]*)" input group$/
- Check an individual input button from a group of inputs
- Example: I select "Admins" from "Groups" input group
(where "Groups" is the title of the CheckboxSetField or OptionsetField form field)
2013-06-05 15:36:34 +02:00
### Interactions
Given /^I press the "([^"]*)" button$/
2015-10-22 05:13:47 +02:00
Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element$/
2013-06-05 15:36:34 +02:00
Given /^I type "([^"]*)" into the dialog$/
2014-05-16 00:25:53 +02:00
Given /^I (?:press|follow) the "([^"]*)" (?:button|link), confirming the dialog$/
Given /^I (?:press|follow) the "([^"]*)" (?:button|link), dismissing the dialog$/
2014-08-02 08:30:27 +02:00
2015-10-22 05:27:48 +02:00
Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, confirming the dialog$/
Given /^I (click|double click) "([^"]*)" in the "([^"]*)" element, dismissing the dialog$/
2014-05-16 00:25:53 +02:00
2013-06-05 15:36:34 +02:00
Given /^I confirm the dialog$/
Given /^I dismiss the dialog$/
### Login
Given /^I am logged in with "([^"]*)" permissions$/
- Creates a member in a group with the correct permissions.
Given /^I am not logged in$/
When /^I log in with "(?< username > [^"]*)" and "(?< password > [^"]*)"$/
Given /^I should see a log-in form$/
2013-11-28 10:50:58 +01:00
Then /^I will see a "bad" log-in message$/
2013-06-05 15:36:34 +02:00
### CMS UI
Then /^I should see an edit page form$/
2014-08-02 08:30:27 +02:00
2013-06-05 15:36:34 +02:00
Then /^I should see the CMS$/
Then /^I should see a "([^"]*)" notice$/
Then /^I should see a "([^"]*)" message$/
Given /^I should see a "([^"]*)" button in CMS Content Toolbar$/
When /^I should see "([^"]*)" in CMS Tree$/
When /^I should not see "([^"]*)" in CMS Tree$/
When /^I expand the "([^"]*)" CMS Panel$/
When /^I click the "([^"]*)" CMS tab$/
Then /^I can see the preview panel$/
Given /^the preview contains "([^"]*)"$/
Given /^the preview does not contain "([^"]*)"$/
### Fixtures
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)" (:?which )?redirects to (?:(an|a|the) )"(?< targetType > [^"]+)" "(?< targetId > [^"]+)"$/
- Find or create a redirector page and link to another existing page.
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)"$/
- Example: Given a "page" "Page 1"
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)" with (?< data > .*)$/
- Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)" has the following data$/
- Example: And the "page" "Page 2" has the following data
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)" is a (?< relation > [^\s]*) of (?:(an|a|the) )"(?< relationType > [^"]+)" "(?< relationId > [^"]+)"/
- Example: Given the "page" "Page 1.1" is a child of the "page" "Page1"
2014-11-11 03:59:24 +01:00
Note that this change is not published by default
2013-06-05 15:36:34 +02:00
Given /^(?:(an|a|the) )"(?< type > [^"]+)" "(?< id > [^"]+)" is (?< state > [^"]*)$/
- Example: Given the "page" "Page 1" is not published
2014-11-11 03:47:00 +01:00
- Example: Given the "page" "Page 1" is published
- Example: Given the "page" "Page 1" is deleted
2013-06-05 15:36:34 +02:00
Given /^there are the following ([^\s]*) records$/
- Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing.
Given /^(?:(an|a|the) )"member" "(?< id > [^"]+)" belonging to "(?< groupId > [^"]+)"$/
- Example: Given a "member" "Admin" belonging to "Admin Group"
Given /^(?:(an|a|the) )"member" "(?< id > [^"]+)" belonging to "(?< groupId > [^"]+)" with (?< data > .*)$/
Given /^(?:(an|a|the) )"group" "(?< id > [^"]+)" (?:(with|has)) permissions (?< permissionStr > .*)$/
- Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section"
2014-08-02 08:30:27 +02:00
2014-11-11 01:31:35 +01:00
Given /^I assign (?:(an|a|the) )"(?< type > [^"]+)" "(?< value > [^"]+)" to (?:(an|a|the) )"(?< relationType > [^"]+)" "(?< relationId > [^"]+)"$/
- Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1"
2013-06-05 15:36:34 +02:00
2015-03-13 13:41:02 +01:00
Given /^I assign (?:(an|a|the) )"(?< type > [^"]+)" "(?< value > [^"]+)" to (?:(an|a|the) )"(?< relationType > [^"]+)" "(?< relationId > [^"]+)" in the "(?< relationName > [^"]+)" relation$
- Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation
2014-11-13 23:47:17 +01:00
Given /^the CMS settings have the following data$/
- Example: Given the CMS settings has the following data
- Note: It only works with the SilverStripe CMS module installed
2014-04-16 01:09:18 +02:00
### Environment
2014-11-13 23:47:17 +01:00
Given /^the current date is "([^"]*)"$/
Given /^the current time is "([^"]*)"$/
### Email
Given /^there should (not |)be an email (to|from) "([^"]*)"$/
2014-08-02 08:30:27 +02:00
2014-11-13 23:47:17 +01:00
Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/
Given /^the email should (not |)contain "([^"]*)"$/
- Example: Given the email should contain "Thank you for registering!"
When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/
When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/
When /^I click on the "([^"]*)" link in the email"$/
Given /^I clear all emails$/
Then /^the email should (not |)contain the following data:$/
Example: Then the email should contain the following data:
2014-04-16 01:09:18 +02:00
2015-07-21 06:27:09 +02:00
Then /^there should (not |)be an email titled "([^"]*)"$/
Then /^the email should (not |)be sent from "([^"]*)"$/
Then /^the email should (not |)be sent to "([^"]*)"$/
2016-08-18 23:20:05 +02:00
When /^I click on the http link "([^"]*)" in the email$/
- Example: When I click on the http link "http://localhost/changepassword" in the email
2014-08-02 08:30:27 +02:00
2013-11-15 14:05:28 +01:00
### Transformations
Behat [transformations ](http://docs.behat.org/guides/2.definitions.html#step-argument-transformations )
have the ability to change step arguments based on their original value,
for example to cast any argument matching the `\d` regex into an actual PHP integer.
* `/^(?:(the|a)) time of (?<val>.*)$/` : Transforms relative time statements compatible with [strtotime() ](http://www.php.net/manual/en/datetime.formats.relative.php ). Example: "the time of 1 hour ago" might return "22:00:00" if its currently "23:00:00".
2014-08-02 08:30:27 +02:00
* `/^(?:(the|a)) date of (?<val>.*)$/` : Transforms relative date statements compatible with [strtotime() ](http://www.php.net/manual/en/datetime.formats.relative.php ). Example: "the date of 2 days ago" might return "2013-10-10" if its currently the 12th of October 2013.
* `/^(?:(the|a)) datetime of (?<val>.*)$/` : Transforms relative date and time statements compatible with [strtotime() ](http://www.php.net/manual/en/datetime.formats.relative.php ). Example: "the datetime of 2 days ago" might return "2013-10-10 23:00:00" if its currently the 12th of October 2013.
2013-11-15 14:05:28 +01:00
2012-11-09 17:35:25 +01:00
## Useful resources
* [SilverStripe CMS architecture ](http://doc.silverstripe.org/sapphire/en/trunk/reference/cms-architecture )
* [SilverStripe Framework Test Module ](https://github.com/silverstripe-labs/silverstripe-frameworktest )
* [SilverStripe Unit and Integration Testing ](http://doc.silverstripe.org/sapphire/en/trunk/topics/testing )