My previous blog refers to using WebDriverIO as the automation tool for NodeJS Application. In this blog, we will see how we can use BDD Framework along with WebDriverIO for NodeJS applications.
Cucumber is the automation tool that is based on Behaviour Driven Development(BDD). You can easily use cucumber features into JavaScript . Install cucumber.js, which is a JavaScript implementation for running cucumber specs on Node.js.
BDD Framework has three layers
1) Layer 1: Steps in Gherkin syntax
Gherkins is a business readable language that helps you describe the behaviour of your application. It supports many spoken languages. Gherkin is widely used in the agile methodology by product owners to describe the acceptance criteria for user stories. The same description in Gherkin syntax can be used by test engineers to automate tests in a easily readable format. Gherkin files should always be written in business language and not technical language
Gherkin Example :
Given I have logged into the application And I view my dashboard
Then I should be able to view my notifications
2) Layer 2: CucumberJS file for the Gherkin Steps:
You can create separate JS file for each page in the application to follow the Page Object Model(POM).
Login.steps.js:
import { defineSupportCode } from 'cucumber';
var LoginMethods = require('../pages/login.page.js');
defineSupportCode(({ Given, When, Then }) => {
When(/^I have logged into the application$/,
LoginMethods.login
);
Then(/^I have successfully logged into the application$/,
LoginMethods.verifyLogin
);
});
Dashboard.steps.js:
import { defineSupportCode } from 'cucumber';
var DashboardMethods = require('../pages/dashboard.page.js');
defineSupportCode(({ Given, When, Then }) => {
When(/^I view my dashboard$/,
DashboardMethods.validatePage
);
Then(/^I should be able to view my notificationst$/,
DashboardMethods.validateNotifications
);
]
});
3) Layer 3: WebDriverIO methods with Chai framework for assertions.
login.page.js
import testData from "../constants/testData.json";
import { expect } from "chai";
module.exports = {
openPage: function() {
browser.url(testData.environments.dev);
browser.windowHandleFullscreen();
browser.pause(2000);
},
login: function(type) {
let username,password;
username = testData.login.validUserEmail
password = testData.login.validUserPwd
browser.waitForVisible('#userid')
browser.setValue('#userid',username)
browser.setValue('#pwd',password)
},
submitForm: function() {
browser.click('#submit')
expect(browser.getText('#dashboardHeader')).to.eql("My Dashboard")
}
}