Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Behat Drupal IntegrationDocumentation
Release 1.1
Brendan MacDonald
Mar 28, 2017
Contents
1 Introduction 3
2 System Requirements 5
3 Installation 7
4 Adding it to an existing project 9
5 Initial setup 11
6 Configuration 13
7 Writing tests 15
8 Test results 19
9 Indices and tables 21
i
ii
Behat Drupal Integration Documentation, Release 1.1
This tools requires a local Drupal installation to work.
In case you want to test a remote installation this module will have to be installed there.
Once it’s installed you can execute it via ssh as you would do in your local environment.
Contents:
Contents 1
Behat Drupal Integration Documentation, Release 1.1
2 Contents
CHAPTER 1
Introduction
This is an extension of Behat Drupal Extension
The goal of this project is to create a framework to integrate Behat tests for Drupal as seamless and flexible as possible.
This works is sponsored and supported by Cameron & Wilding, a London Drupal Agency
3
Behat Drupal Integration Documentation, Release 1.1
4 Chapter 1. Introduction
CHAPTER 2
System Requirements
Required
1. Check your PHP version:
php --version
It must be higher than 5.3.5
PHP will also need to have the following libraries installed:
• curl
• mbstring
• xml
These dependency comes from Behat Drupal Extension requirements.
2. Check for Java:
java -version
It must be 1.7+. It will be required for Selenium.
Optional
Download chromedriver in case you want integration with this browser.
5
Behat Drupal Integration Documentation, Release 1.1
6 Chapter 2. System Requirements
CHAPTER 3
Installation
These are the steps to install the tool from different sources.
Install via Composer
1. Select a location for the framework
Create a folder, ideally in a Test folder in your project, outside your Drupal webroot.
2. Create a composer JSON file Composer
Create a composer.json file in the test folder root:
{"require": {
"cw/behat_test": "*"},"config": {
"bin-dir": "bin/"},"autoload": {
"psr-4": {"CWTest\\": "src/"
}}
}
3. From the Test folder created st step 1, run:
composer install
7
Behat Drupal Integration Documentation, Release 1.1
Install via Github
1. Select a location for the framework and clone it:
git clone https://github.com/cameronandwilding/CWTest_Behat --branch={8.x|7.x}→˓{folder}
8 Chapter 3. Installation
CHAPTER 4
Adding it to an existing project
To add the tool to an existing project follow the next steps
1. Select a location for the framework
Create a folder, ideally in a Test folder in your project, outside your Drupal webroot.
2. Add the following to the componser.json file of the project:
{"require": {
"cw/behat_test": "*"},"config": {
"bin-dir": "bin/"},"autoload": {
"psr-4": {"CWTest\\": "src/"
}}
}
3. Follow the Installation instructions.
9
Behat Drupal Integration Documentation, Release 1.1
10 Chapter 4. Adding it to an existing project
CHAPTER 5
Initial setup
1. Create the Behat folder structure:
Run the bootstrap shell script:
cd bin./cwtest-bootstrap.shcd..
2. Update your local configuration:
In your Test folder, edit Behat/behat.local.yml
Update:
the base_url to your local site url
the drupal_root value to the path to your local drupal installation.
3. Configure Chrome - Optional Step:
This is only required if you want to run tests on Chrome. Skip if you don’t.
(By default, Firefox works out-of-the-box.)
Download chromedriver
Save it to /usr/local/bin
4. Verify Setup Successful:
Navigate to the Behat folder inside your Test folder:
cd Behat
Execute the following:
./run-behat.sh setup firefox
11
Behat Drupal Integration Documentation, Release 1.1
Selenium will launch and run a test. You should see 1 scenarios (1 passed) in the terminal window after 15-20seconds.
5. Running tests:
To run tests, execute the following:
./run-behat.sh {tag} {profile}
12 Chapter 5. Initial setup
CHAPTER 6
Configuration
behat.yml
This file contains most of the configuration settings that are required for behat to run.
Every new feature file that gets created will require that a new entry is made to this file.
ACTION: Follow the example of the login from lines 3-15. Copy and paste this inside the default profile, and updatethe login values with the correct values.
• file - /Behat/behat.yml
• Suites
• Profiles
behat.local.yml
This is a mandatory file that contains the local configuration for the machine were the tests are running.
13
Behat Drupal Integration Documentation, Release 1.1
14 Chapter 6. Configuration
CHAPTER 7
Writing tests
It is a good idea to read through the LOGIN feature, page, and context files while reading through the followingdescriptions.
In the following, XXXX is the name of the page being tested, e.g. Basic, Article, Login, etc.
“ACTION” indicates a step that you have to do.
The basic process for writing any test would be:
Scenarios
Decide on a business scenario that you would like to automate.
This often comes from a User Story or piece of functionality that you’d like to test.
For the rest of the following, let’s think of a login scenario where a user is going:
1. open the login page.
2. enter a username and password.
3. click the login button.
Features
This file contains the high-level test scenarios written in a Gherkin syntax.
These files are located in Behat/features/.
They all follow the naming convention XXXX.feature.
For example, in the LoginPage.feature, there are tests to ensure a valid login is successful.
ACTION: Create a .feature file, using the template provided, and write your scenario into the .featurefile.
15
Behat Drupal Integration Documentation, Release 1.1
Follow the syntax used in other tests.
Where possible, re-use existing sentences from the .feature file as these will already have been automated.
If you are creating a new sentence, keep it short but descriptive.
• template - /Sample_Files/Behat/features/LoginPage.feature
• Gherkin
Page.php
This file contains the path, page objects, and getters/setters for all the fields on the page XXXX.
These files are located in src/Util/.
They all follow the naming convention XXXXPage.php.
For example, in the LoginPage.php, there are the username, password, and login button objects detailed.
ACTION: Create a Page.php file, and add the objects to it.
For Create/Edit/View content types, you generally want to add every object that an end-user would use tothe XXXXPage.php file.
Using the template provided, create your XXXXPage.php file.
Take care to separate textfields, buttons, frames, etc, and follow the syntax and naming conventions fromother PAGE files.
Where possible, always use IDs for your objects. If IDs are not available, consider using name, data-drupal-selector, or xpath.
• template - /Sample_Files/src/Util/ArticlePage.php
Context.php
This file contains all of the functions that are specific to the XXXX page.
These files are located in src/Context.
They all follow the naming convention XXXXContext.php.
For example, in the LoginContext.php, there are functions to fill in the username and password fields, andpress the login button.
ACTION: Create a XXXXContext.php file, and add the relevant functions to interact with the objectsfrom the Page.php file.
This file will detail function for interacting with your objects.
The number of functions you write will vary from context to context - typically, the more complicated aUI is, the more functions will be required.
Follow the syntax and naming conventions from other CONTEXT files.
Keep all functions as short as possible, ideally doing one thing each, like filling in a text field.
• template - /Sample_Files/src/Context/ArticleContext.php
• Step definitions
• Hooks
16 Chapter 7. Writing tests
Behat Drupal Integration Documentation, Release 1.1
• Contexts
7.4. Context.php 17
Behat Drupal Integration Documentation, Release 1.1
18 Chapter 7. Writing tests
CHAPTER 8
Test results
The results of all tests will be stored in /Results/Behat/Twig_***.html
19
Behat Drupal Integration Documentation, Release 1.1
20 Chapter 8. Test results
CHAPTER 9
Indices and tables
• genindex
• modindex
• search
21