cucumber-mink is a cucumber-js step definition library. You can run your test with any Selenium compatible browser like Phantomjs !
- Node.js
- PhantomJS
- cucumber-js:
npm install -g cucumber
Launch the headless browser (default configuration is Phantomjs)
phantomjs -w
Install cucumber-mink library locally
npm install --save cucumber-mink
Create mink.js
file a the root of your project
var mink = require('cucumber-mink');
module.exports = function () {
mink.call(this);
};
Use pre-defined steps in your features/__.feature
files
// features/home.feature
Feature: I can use cucumber.mink to check the content of my website
Background:
Given I browse "http://localhost:3000"
Scenario: Check Homepage content
Given I am on the homepage
And I should see "Welcome to my awesome application" in the "h1" element
Run your tests
cucumber-js --require mink.js
Run a specific scenario based on the line of scenario
cucumber-js --require mink.js features/test.feature:<LINE_NUMBER>
You can use an environment variable and then reference it in your features files to set the base url of your application.
export CUCUMBER_URL=http://localhost:3000
...
Background:
Given I browse "${CUCUMBER_URL}"
Scenario:
Given I am on "/post/2"
...
The standard step definition methods are overridden by cucumber-mink so that it's easier to call the driver.
#####World.defineStep(String pattern, Fn(Driver, [] stepsInput, Fn cb))
The driver object is injected as the first arguments in the step function. This avoid heavy use of this
keyword.
This affects also siblings methods : World.Given
, World.Then
, World.When
.
I order to keep your features files clean and to follow the DRY principle you can declare your own "Meta" steps that will execute several basic steps available in this library.
Here is an example of how to create a "I am logged in" step for your test suite:
So the initial multi-steps scenario to log into the application could be:
Given I am on "/login"
And I fill in the following:
| input.username | test@test.com |
| input.password | test |
And I press "Login"
And we want to write in our tests only a one liner like:
Given I am logged in
#####Mink.metaStep(Driver, [] stepsArray, Fn callback)
Call multiple callback function in one step effectively creating a "meta" steps. StepsArray should be an Array of objects like:
var stepsArray = [
{
stepFunc: Ext.Navigation.browse,
args: ['/form']
},
{
stepFunc: Ext.Action.click,
args: ['button[type="submit"]']
}
];
Inside your features/
folder, create a new step_definitions/
folder. Step definitions are the glue between features written in Gherkin and the actual SUT (system under test). They are written in JavaScript.
Create a login.js
file like this:
// features/step_definitions/login.js
var Mink = require('cucumber-mink'),
Ext = Mink.Ext;
/////////////////////////
function login (Driver, callback) {
var loginFormArray = [
{ field: 'input.username', value: '[email protected]' },
{ field: 'input.password', value: 'test' }
];
var stepsArray = [
{
stepFunc: Ext.Navigation.browse,
args: ['/login']
},
{
stepFunc: Ext.Form.fillFields,
args: [loginFormArray]
},
{
stepFunc: Ext.Action.click,
args: ['Login']
}
];
return Mink.metaStep(Driver, stepsArray, callback);
}
/////////////////////////
module.exports = function() {
this.Given(/^I am logged in$/, login);
};
Now, use your newly defined step inside a .feature file:
Scenario: I log into the application and see my dashboard
Given I am logged in
Then I should be on "/dashboard"
Important, don't forget to launch your test suite with the correct command: in this case
cucumber-js --require mink.js --require features/step_definitions/
You can pass in any function inside the stepFunc
field, here we use Mink's function available in lib/step_definitions/ext
.
There is a complete example here: meta.js
cucumber-mink comes with support for WebDriverIO out of the box:
-
WebDriverIO
- webdriver module for Node.js. webdriverio. This driver allow you to communicate with any Selenium compatible grid/hub. The driver default settings use Phantomjs/GhostDriver.
var parameters = { driver: { type: 'webdriverio', options : { desiredCapabilities: { browserName: 'phantomjs' }, logLevel: 'silent', port: 8910 } } }; ```
This driver can be used to communicate with various browser, like a locally running [Chrome](https://code.google.com/p/selenium/wiki/ChromeDriver)
or on some distant services like [SauceLabs](https://saucelabs.com/) and [BrowserStack](http://www.browserstack.com/). See [examples](examples/)
The npm module for this library is maintained by: