Skip to content

Instantly share code, notes, and snippets.

@dinarcon
Created April 11, 2019 20:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinarcon/29d01dd38351403e671abf257c9e5c99 to your computer and use it in GitHub Desktop.
Save dinarcon/29d01dd38351403e671abf257c9e5c99 to your computer and use it in GitHub Desktop.
Drupal 8 BDD with Behat
default:
suites:
default:
contexts:
- FeatureContext
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\DrushContext
- NuvoleWeb\Drupal\DrupalExtension\Context\AutocompleteContext
extensions:
DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~
Behat\MinkExtension:
goutte: ~
default_session: goutte
javascript_session: default
browser_name: chrome
base_url: "http://localhost:8888"
sessions:
default:
chrome:
api_url: "http://localhost:9222"
Bex\Behat\ScreenshotExtension:
screenshot_taking_mode: failed_steps
image_drivers:
local:
screenshot_directory: ./features/screenshots
clear_screenshot_directory: true
NuvoleWeb\Drupal\DrupalExtension:
blackbox: ~
api_driver: drupal
drupal:
drupal_root: .
# Located at ./features/
Feature: Contact form
In order to send a message to the site administrators
As a visitor
I should be able to use the site-wide contact form
Scenario: A visitor can use the site-wide contact form
Given I am at "contact/feedback"
When I fill in the "name" field with "John Doe"
And I fill in the "email address" field with "john@doe.com"
And I fill in the "subject" field with "Hello world"
And I fill in the "message" field with "Lorem Ipsum"
And I press "Send message"
Then I should see the text "Your message has been sent."
@api
Scenario: A authenticated can use the site-wide contact form
Given I am logged in as a user with the "Authenticated user" role
And I am at "contact/feedback"
And I should not see the "name" field
And I should not see the "email address" field
And I fill in the "subject" field with "Hello world"
And I fill in the "message" field with "Lorem Ipsum"
And I press "Send message"
Then I should see the text "Your message has been sent."
# Located at ./features/
Feature: Article administration
In order administer articles
As an administrator
I should be the proper permissions
@api @javascript
Scenario: An administrator can create an article
Given I am logged in as a user with the "Administrator" role
And I am at "node/add/article"
When I fill in "Title" with "Tofu is the best"
And I fill in the autocomplete "Tags" with "Vegan"
And I press "Save"
Then I should see the text "Article Tofu is the best has been created."
And I should see the text "Vegetarian"
<?php
use Behat\Mink\Exception\ExpectationException;
use Behat\MinkExtension\Context\RawMinkContext;
// Located at ./features/bootstrap/
/**
* FeatureContext class defines custom step definitions for Behat.
*/
class FeatureContext extends RawMinkContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
/**
* @When I fill in the :arg1 field with :arg2
*/
public function iFillInTheFieldWith($name, $value)
{
$selector = $this->getFieldSelector($name);
$field = $this->fixStepArgument($selector);
$value = $this->fixStepArgument($value);
$this->getSession()->getPage()->fillField($field, $value);
}
/**
* @Given I should not see the :name field
*/
public function iShouldNotSeeTheField($name)
{
$selector = $this->getFieldSelector($name);
$field = $this->fixStepArgument($selector);
$element = $this->getSession()->getPage()->findField($field);
if ($element) {
throw new ExpectationException(sprintf("The field '%s' was present on the page %s and was not supposed to be", $element, $this->getSession()->getCurrentUrl()), $this->getSession());
}
}
/**
* Returns selector for specified field/
*
* @param string $field
*
* @return string
*/
protected function getFieldSelector($field) {
$selector = $field;
switch ($field) {
case 'email address':
$selector = 'mail';
break;
case 'subject':
$selector = 'subject[0][value]';
break;
case 'message':
$selector = 'message[0][value]';
break;
}
return $selector;
}
/**
* Returns fixed step argument (with \\" replaced back to ")
*
* @param string $argument
*
* @return string
*/
protected function fixStepArgument($argument)
{
return str_replace('\\"', '"', $argument);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment