klassi-Js is a debuggable BDD Javascript test automation framework. Built on webdriver.io (Next-gen browser and mobile automation test framework for Node.js) and cucumber-js with integrated Visual, accessibility and API Testing, your test can run locally or in the cloud using Lambdatest , BrowserStack or Sauce Labs
yarn add klassi-js
npm install klassi-js
node ./node_modules/klassi-js/index.js
--help output usage information
--version output the version number
--browser <name> name of browser to use (chrome, firefox). defaults to chrome
--tags <@tagName> name of cucumber tags to run - Multiple TAGS usage (@tag1,@tag2)
--exclude <@tagName> name of cucumber tags to exclude - Multiple TAGS usage(@tag3,@tag5)
--steps <path> path to step definitions. defaults to ./step-definitions
--featureFiles <path> path to feature definitions. defaults to ./features
--pageObjects <path> path to page objects. defaults to ./page-objects
--sharedObjects <paths> path to shared objects - repeatable. defaults to ./shared-objects
--reports <path> output path to save reports. defaults to ./reports
--disableReport disables the test report from opening after test completion
--email sends email reports to stakeholders
--env <path> name of environment to run the framework/test in. default to dev
--reportName <optional> name of what the report would be called i.e. 'Automated Test'
--remoteService <optional> which remote driver service, if any, should be used e.g. browserstack
--extraSettings <optional> further piped configs split with pipes
--updateBaselineImages automatically update the baseline image after a failed comparison or new images
--wdProtocol the switch to change the browser option from using devtools to webdriver
--browserOpen this leaves the browser open after the session completes, useful when debugging test. defaults to false', false
--dlink the switch for projects with their test suite, within a Test folder of the repo
--dryRun the effect is that Cucumber will still do all the aggregation work of looking at your feature files, loading your support code etc but without actually executing the tests
--utam this launches the compiler for salesforce scripts
--useProxy this is in-case you need to use the proxy server while testing'
--skipTag <@tagName> provide a tag and all tests marked with it will be skipped automatically.
--tags @get,@put || will execute the scenarios tagged with the values provided. If multiple are necessary, separate them with a comma (no blank space in between).
--featureFiles features/utam.feature,features/getMethod.feature || provide specific feature files containing the scenarios to be executed. If multiple are necessary, separate them with a comma (no blank space in between).
To upgrade existing projects for use with klassi-js v5, please follow these few steps HERE
You can use the framework without any command line arguments if your application uses the following folder structure, to help with the built in functionality usage.
.
└── features
└── search.feature
└── page-objects
└── search.js
└── shared-objects
└── searchData.js
└── step_definitions
└── search-steps.js
└── reports # folder and content gets created automatically at runtime
└── chrome
├── reportName-01-01-1900-235959.html
└── reportName-01-01-1900-235959.json
.envConfigrc.js # this file will contain all your environment variables (i.e. dev, test, prod etc.,)
.dataConfigrc.js # this file will contain all your project variables #projectName, emailAddresses
.env # this file contains all config username and passcode and should be listed in the gitignore file
cucumber.js # the cucumber configuration file
The following variables are available within the Given()
, When()
and Then()
functions:
Variable | Description |
---|---|
browser |
an instance of webdriverio (the browser) |
wdio |
the raw webdriverio module, providing access to static properties/methods |
pageObjects |
collection of page objects loaded from disk and keyed by filename |
sharedObjects |
collection of shared objects loaded from disk and keyed by filename |
helpers |
a collection of helper methods things webdriver.io does not provide but really should! |
expect |
instance of chai expect to expect('something').to.equal('something') |
assert |
instance of chai assert to assert.isOk('everything', 'everything is ok') |
trace |
handy trace method to log console output with increased visibility |
Klassi-js contains a few helper methods to help along the way, these methods are:
// Load a URL, returning only when the <body> tag is present
await helpers.loadPage('https://duckduckgo.com', 10);
// take image for comparisson
await helpers.takeImage('flower_1-0.png', 'div.badge-link--serp.ddg-extension-hide.js-badge-link');
// compare taken image with baseline image
await helpers.compareImage('flower_1-0.png');
// get the content of an endpoint
await helpers.apiCall('http://httpbin.org/get', 'get');
// writing content to a text file
await helpers.writeToTxtFile(filepath, output);
// reading content froma text file
await helpers.readFromFile(filepath);
// applying the current date to files
await helpers.currentDate();
// get current date and time (dd-mm-yyyy-00:00:00)
await helpers.getCurrentDateTime();
// clicks an element (or multiple if present) that is not visible, useful in situations where a menu needs a hover before a child link appears
await helpers.clickHiddenElement(selector, textToMatch);
// This method is useful for dropdown boxes as some of them have default 'Please select' option on index 0
await helpers.getRandomIntegerExcludeFirst(range);
// Get the href link from an element
await helpers.getLink(selector);
//wait until and element is visible and click it
await helpers.waitAndClick(selector);
// wait until element to be in focus and set the value
await helpers.waitAndSetValue(selector, value);
// function to get element from frame or frameset
await helpers.getElementFromFrame(frameName, selector);
// This will assert 'equal' text being returned
await helpers.assertText(selector, expected);
// This will assert text being returned includes
await helpers.expectToIncludeText(selector, expectedText);
// this asserts that the returned url is the correct one
await helpers.assertUrl(expected);
// this is the generating accessibility reports per page
await helpers.accessibilityReport: async (pageName, count = false || true);
//reading from a json file
await helpers.readFromJson();
//writing data to testData json file in shared objects folder
await helpers.write();
//writing data to a json file
await helpers.writeToJson();
//writing json data from above to UrlData json file
await helpers.writeToUrlsData();
//merging json files
await helpers.mergeJson();
//hide elements
await helpers.hideElements();
//show elements
await helpers.showElements();
//reporting the current date and time
await helpers.reportDateTime();
//API call for GET, PUT, POST and DELETE functionality using PactumJS for API testing
await helpers.apiCall();
//function for recording Accessibility logs from the test run
await helpers.accessibilityReport();
//function for recording total errors from the Accessibility test run
await helpers.accessibilityError();
//Get the href link from an element
await helpers.getLink();
//function to get element from frame or frameset
await helpers.getElementFromFrame();
//Generate random integer from a given range
await helpers.generateRandomInteger();
//this generates the full execution time for a full scenario run
await helpers.executeTime();
//Generates a random 13 digit number
await helpers.randomNumberGenerator();
//Reformats date string into string
await helpers.reformatDateString();
//Sorts results by date
await helpers.sortByDate();
//this filters an item from a list of items
await helpers.filterItem();
//this filters an item from a list of items and clicks on it
await helpers.filterItemAndClick();
//this uploads a file from local system or project folder. Helpful to automate uploading a file when there are system dialogues exist.
await helpers.fileUpload();
By default, the test run using Google Chrome/devtools protocol, to run tests using another browser locally you'll need a local selenium server running, supply the browser name along with the --wdProtocol --browser
switch
Browser | Example |
---|---|
Chrome | --wdProtocol --browser chrome |
Firefox | --wdProtocol --browser firefox |
All other browser configurations are available via 3rd party services (i.e. browserstack | lambdatest | sauceLabs)
Selenium Standalone Server installation
npm install -g selenium-standalone@latest
selenium-standalone install
selenium-standalone start
Visual Regression with Resemble JS
Visual regression testing, the ability to compare a whole page screenshots or of specific parts of the application / page under test. If there is dynamic content (i.e. a clock), hide this element by passing the selector (or an array of selectors) to the takeImage function.
// usage within page-object file:
await helpers.takeImage(fileName, [elementsToHide, elementsToHide]);
await browser.pause(100);
await helpers.compareImage(fileName);
API Testing with PactumJS
Getting data from a JSON REST API
apiCall: async (url, method, auth, body, status) => {
let resp;
const options = {
url,
method,
headers: {
Authorization: `Bearer ${auth}`,
'content-Type': 'application/json',
},
body,
};
if (method === 'GET') {
resp = await helpers.apiCall(url, 'GET', auth);
return resp.statusCode;
}
if (method === 'POST') {
resp = await helpers.apiCall(url, 'POST', auth, body, status);
return resp;
}
}
Accessibility Testing with Axe
Automated accessibility testing feature has been introduced using the Axe-Core OpenSource library.
All the accessibility fuctions can be accessed through the global variable accessibilityLib
.
function | Description |
---|---|
accessibilityLib.getAccessibilityReport('PageName') |
generates the accessibility report with the given page name |
accessibilityLib.getAccessibilityError() |
returns the total number of error count for a particular page. |
accessibilityLib.getAccessibilityTotalError() |
returns the total number of error count for all the pages in a particilar execution |
// usage within page-object file:
When('I run the accesibility analysis for {string}', async function (PageName) {
// After navigating to a particular page, just call the function to generate the accessibility report and the total error count for the page
await helpers.accessibilityReport: async (pageName, count = false || true)
});
HTML and JSON reports will be automatically generated and stored in the default ./reports
folder. This location can be
changed by providing a new path using the --reports
command line switch:
HTML and JSON reports will be automatically generated and stored in the default ./reports/accessibility
folder.This location can be changed by providing a new path using the --reports
command line switch:
Mobile App automation with Appium
Besides the ability to test web applications on mobile environments, the framework allows for the automation of native mobile applications running on Android or iOS in LambdaTest.
klassi-js contains sample tests that can be run by executing the following commands:
yarn run android
Or:
yarn run ios
The environment configuration needs to include the following information:
- envName: Android or iOS. The application will be installed before the test is run and uninstalled on cleanup.
- appName: the application package name. Used when handling and verifying app instalation, removal and can be used in selectors.
- appPath: remote path to the .APK file (Android) or ZIP file (iOS) containing the application. Because the files need to be accessible to the Appium instance running on LambdaTest, remote locations are preferred.
For instance:
{
environment: {
"android": {
"envName": "android",
"appName": "oxford.learners.bookshelf.canary",
"appPath": "https://olb-android-release.s3-accelerate.amazonaws.com/test/olb-5.9.3-canary.apk"
},
}
}
As far as the mobile capabilities set on ./lambdatest/
, please use LambdaTest's application to ensure that you select a correct combination of OS, Appium version and device name that will be accepted by LT.
As for properties that should be set to a specific option, please bear in mind the following considerations:
- build: should be set to "klassi-js Mobile" so test executions for native mobile apps can be filtered easily from web app tests.
- browserName: should be left empty so Lambdatest doesn't interpret that the intention is to test a web application.
- networkThrottling: should be kept as "Regular 4G", during development it was detected that element selection is flaky if the emulators do not keep a steady connection, achieved through this throttling option.
For instance:
{
"projectName": "klassi-js",
"build": "klassi-js Mobile",
"platformName" : "Android",
"browserName": "",
"deviceName" : "Galaxy Tab S7 Plus",
"platformVersion" : "11",
"appiumVersion" : "1.17.0",
"deviceOrientation" : "LANDSCAPE",
"networkThrottling": "Regular 4G",
"console" : "true",
"network" : true,
"visual" : true
}
You can register event handlers for the following events within the cucumber lifecycle.
const {After, Before, AfterAll, BeforeAll, BeforeStep, AfterStep} = require('@cucumber/cucumber');
Event | Example |
---|---|
Before | Before(function() { // This hook will be executed before all scenarios}) |
After | After(function() {// This hook will be executed after all scenarios}); |
BeforeAll | BeforeAll(function() {// perform some shared setup}); |
AfterAll | AfterAll(function() {// perform some shared teardown}); |
BeforeStep | BeforeStep(function() {// This hook will be executed before all steps in a scenario with tagname; |
AfterStep | AfterStep(function() {// This hook will be executed after all steps, and take a screenshot on step failure; |
Most webdriverio methods return a JavaScript Promise that is resolved when the method completes. The easiest way to step in with a debugger is to add a .then
method to the function and place a debugger
statement within it, for example:
When(/^I search DuckDuckGo for "([^"]*)"$/, function (searchQuery, done) {
elem = browser.$('#search_form_input_homepage').then(function(input) {
expect(input).to.exist;
debugger; // <<- your IDE should step in at this point, with the browser open
return input;
})
done(); // <<- let cucumber know you're done
});
To enforce best practices in using Git for version control, this project includes a Husky configuration. Note that breaking the given rules will block the commit of the code.
After committing the staged code, the Husky scripts will enforce the implementation of the Conventional Commits specification.
To summarize them, all commits should follow the following schema:
git commit -m "<type>: <subject>"
Please keep in mind that the subject must be written in lowercase.
To see a demo of the framework without installing it, use this link [HERE]
Please raise bugs via the klassi-js issue tracker, please provide enough information for bug reproduction.
Anyone can contribute to this project, PRs are welcome. In lieu of a formal styleguide, please take care to maintain the existing coding style.
Licenced under MIT License © 2016 Larry Goddard