Contexts that we use with Behat 3.x tests on Drupal sites.
This repository is based on Nuvole drupal extension.
Install with Composer:
- Add these lines in "repositories" entry of your composer.json:
{
"type": "vcs",
"url": "https://github.com/mistermoper/drupal-behat"
}
- Require package with composer require:
composer require metadrop/behat-contexts
Each context may have its own configuration. Here is an example with all the contexts added.
Steps to clear caches.
-
Given :path page cache is flushed Clear specific page caches.
-
Given :view view data cache is flushed Clear caches for a specific view. Only available for Drupal 7, pending to implement in D8.
Allow created content owned by logged in user.
- Given own :type content: Create content with the author as the current user.
Helpers to execute cron.
-
Given I run elysia cron Run elysia cron. Only for D7.
-
Given I run the elysia cron :job job Run elysia-cron-job. Only for D7.
-
Given I run the cron of Search API Run search api cron. Only for D7.
-
Given I run the cron of Search API Solr Run search api solr cron. Only for D7.
Simple context to help debugging tests with some steps. Additionally, it hooks in the after step event to add a step that generates an error report on failed steps.
This report includes:
- A file with the HTML page content.
- A file with the current URL and the error exception dump.
- If available, a file with current page state.
-
Then capture full page with a width of :width
Saves a screenshot of current page with the given width to a file.
-
Then capture full page with a width of :width with name :filename in configured directory (screenshots_path).
Saves a screenshot of current page with the given width to a given filename in configured directory (screenshots_path).
-
Then capture full page with width of :width to :path
Saves a screenshot of current page with the given width to a file in the given path. If path is relative screenshots_path config value is used as root.
-
Then capture full page with width of :width to :path with name :filename
Saves a screenshot of current page with the given width to a file in the given path to a given filename. If path is relative screenshots_path config value is used as root.
-
Then save last response
Saves page content to a file.
-
Then save last response to :path
Saves page content to a file in the given path.
-
Then save last response to :path
Halts test for a given amount of seconds. Useful when debugging tests with timing issues. Don't use this step in real tests.
Add DebugContext to your suite.
This is an example when bootstrap directorty is in DRUPALROOT/sites/all/tests/behat/bootstrap.
default:
autoload:
...
suites:
default:
...
contexts:
- Metadrop\Behat\Context\DebugContext:
parameters:
'report_on_error': true
'error_reporting_url': 'https://example.com/sites/default/files/behat/errors'
'error_reporting_path': '/var/www/html/docroot/sites/default/files/behat/errors'
'screenshots_path': '/var/www/html/docroot/sites/default/files/behat/screenshots'
'page_contents_path': '/var/www/html/docroot/sites/default/files/behat/pages'
- Metadrop\Behat\Context\EntityContext:
parameters:
'purge_entities':
- user
- custom_entity
Parameters
- report_on_error: If true error reports are generated on failed steps.
- error_reporting_path: Path where reports are saved.
- error_reporting_url: Url where the error screenshots will be shown. As we can see in example, the url must point to the directory where we save the reports, and the directory must be accesible through website.
- screenshots_path: Path where screenshots are saved. Report screenshots are saved in the report path, here only screenshots from capture full page steps are saved.
- page_contents_path: Path where page contents are saved. Report page contents are saved in the report path, here only page contents from save page content steps are saved.
Agnostic steps related with entities.
-
Given I go to the last entity :entity created Go to last entity created.
-
Given I go to the last entity :entity with :bundle bundle created Go to the last entity created from a specific bundle.
-
Given I go to :subpath of the last entity :entity created Go to last entity created subpath (s.e.:node/1/edit).
-
Given I go to :subpath of the last entity :entity with :bundle bundle created. Go to last entity created subpath (s.e.:node/1/edit) from a specific bundle.
Create files in drupal.
-
Given file with name :filename Create file in drupal file system. Files are extracted from files_path set in behat.
-
Given file with name :filename in the :directory directory Create file in drupal file system in a specific directory. Directory must start with file system (public:// , private://). Default is public:// .
Steps for form elements.
-
Then form :type element :label should be required Check a form element of a specific type (s.e.: input, select) with label is required.
-
Then form :type element :label should not be required Check a form element of a specific type (s.e.: input, select) with label isn't required.
Steps related with node access system. Only for D7.
-
Given the access of last node created is refreshed Refresh node grants from the last node.
-
@Given the access of last node created with :bundle bundle is refreshed Refresh node grants from the last node of a specific content type.
Steps to attach paragraphs to content.
- Given paragraph of :paragraph_type type referenced on the :field_paragraph field of the last content: Create a paragraph with fields and attach it to the last node created.
Steps to check url values
-
Then current url should have the ":param" param with ":value" value Check an url has a specific value in a query parameter.
-
Then current url should not have the ":param" param with ":value" value Check an url hasn't a specific value in a query parameter.
This context provides steps for certain UI elements.
-
Given I select :option from :select chosen.js select box
Selects and option from a Chosen select widget. Only for sinlge selection, it doesn't work with multiple selection enabled or tag style.
No configuration needed.