Js Test Automation Frameworks 20
Js Test Automation Frameworks 20
JavaScript
CONTENTS
Test Automation
− A General Approach to Testing
− Testing JavaScript Applications
Frameworks
Frameworks
− WebDriverIO
− Nightwatch
− Puppeteer
Six Essential Frameworks for Creating − Playwright
Web development has changed dramatically over the last three 2. Command – The logic of the test case that is intended
decades. At the advent of the Web, nearly all applications consisted to verify some behavior
of static content rendered in one or two browsers. Since that time,
3. Postconditions – Assertions that must be true once
the Web has evolved into a colossal collection of dynamic web pages
the test is complete
and applications rendered in half-a-dozen or more browsers on
behalf of billions of users. Written formally, using Hoare Logic, a test case can be expressed as:
test frameworks have also morphed to keep pace with this ever-
where:
evolving ecosystem. In this Refcard, we will delve into the conceptual
• {P} are the preconditions of the test case
underpinnings of modern JavaScript test automation frameworks
and explore six of the most popular frameworks available today. • {C} is the command to be executed
Along this journey, we will compare the different design and • {Q} are the postconditions of the test case that
architecture decisions of each framework and how well these various we expect to be to be true
trade-offs are suited for different requirements and contexts.
1
NEW
NEW
NEW
NEW
REFCARD | JAVASCRIPT TEST AUTOMATION FR AMEWORKS
We can write tests in natural language — called Behavior-Driven This tight relationship between JavaScript and the browser allows
Development (BDD) — using the following clauses: us to programmatically execute basic actions we take for granted,
such as clicking a button or hovering over a specific Hypertext
1. Given – The environment and context of the test case
Markup Language (HTML) element. Due to JavaScript's capability to
2. When – The logic that is under test
interact with the browser and execute these actions, JavaScript test
3. Then – The assertions about the results of the test
frameworks must also be able to inspect the results of these actions,
Lastly, we can translate our test cases into code, as seen in the
pseudocode example below of our previous BDD test case:
// Given
a = 3
b = 5
// When
For example, if we click a button, we may want to assert that a different
result = a + b
HTML element changes to a specific color, a dialog box is displayed,
Node.Js (Node), a vast majority of the JavaScript applications we see interoperability with all browsers that support the
today are browser-based. This is unsurprising, as JavaScript has a standard, but it can be inefficient and lack support for
significant advantage over nearly all other programming languages browser-specific features
in this realm: All modern browsers include a JavaScript engine that 2. Non-Standardized – Uses browser-specific protocols that
allows JavaScript code to be executed natively within the browser, have not been standardized, which may be more efficient
which brings its own set of advantages and challenges. and feature-rich, but it may not support all browsers
3. Proprietary – Uses a customized mechanism — such as Note that the -y flag — “yes” — accepts all prompts during the
proxying or code injection — to interact with browsers, which initialization process, removing the need for manual interaction.
can be efficient and feature-rich, but it is more complex and Next, we must install the WebDriverIO Command Line Interface
may introduce quirky limitations (CLI) tool through NPM and configure the project using the
wdio config command:
Each of these categories has its advantages and disadvantages, and
the frameworks that use them excel and deteriorate in different ways. npm i --save-dev @wdio/cli
In the following section, we will look at six of the more prominent npx wdio config -y
1. World Wide Web Consortium (W3C) WebDriver – WebDriverIO This specs field specifies the location of our test case specifications.
and Nightwatch By default, a test file, example.e2e.js, will be created under the
./test/specs directory. We can run this test using the following
2. Chrome DevTools Protocol (CDP) – Puppeteer and Playwright
command:
3. Proprietary – TestCafe and Cypress
npx wdio run wdio.conf.js
In this section, we will delve into how we can install and run each
framework, walk through an exploratory example of a simple test
The example.e2e.js test will open a browser window and attempt
case, and compare each framework to understand which is the best
to authenticate using a login form. Once the test is complete, we can
fit for our use cases. Note: The examples that follow assume Node
see the successful results of our test case in the WebDriverIO output:
and the Node Package Manager (NPM) are already installed.
Spec Files: 1 passed, 1 total (100% completed) in
WEBDRIVERIO 00:00:08
WebDriverIO is an open-source project that utilizes the W3C
WebDriver standard to interact with browsers in a truly cross- For more information, see the following WebDriverIO resources:
compatible manner. All modern web browsers, including Chrome, • What is WebDriver.IO?
Firefox, Safari, and Edge, support the WebDriver standard, which • Automation Protocol
means that WebDriverIO supports nearly all browsers out of the box. • Getting Started
Unfortunately, the Web has changed significantly since the WebDriver • Boilerplate Projects
standard was devised, and it lags in its support for modern web • NPM
development and testing.
NIGHTWATCH
To supplement deficiencies of the WebDriver standard, WebDriverIO
Nightwatch is an open-source project similar to WebDriverIO — both
also supports CDP, which enables the framework to use the
use the WebDriver standard to interact with the browser — but
standardized WebDriver interactions, while also allowing us to
Nightwatch focuses more on usability. This focus on ease-of-use
perform additional, more modern interactions with the browser.
results in cleaner syntax, selection through JavaScript and CSS or
This CDP interaction occurs over WebSockets to the browser, which
XPath, and support for Page Object Models (POMs).
means WebDriverIO can interact with the browser without the need
The usability of Nightwatch is also reflected in how quickly a test case
for a patched browser or external service.
can be created. Similar to WebDriverIO, we must first create a new
To run a WebDriverIO test, we must first create a new Node project: Node project:
To install Nightwatch, we can run the following command: WebDriverIO, on the other hand, exposes many of these details,
which allows us to fine-tune and tweak our test configuration as
npm i --save-dev nightwatch geckodriver \
needed. In general, Nightwatch should be preferred when simplicity
chromedriver
and ease-of-use are a priority, while WebDriverIO should be used
when a greater level of control is required.
We can then run an example script, ecosia.js — contained in
node_modules/nightwatch/examples/tests/ — by running the
For more information, see the following Nightwatch resources:
following command:
• Getting Started
npx nightwatch \ • Developer Guide
node_modules/nightwatch/examples/tests/ecosia.js • GitHub
• NPM
The ecosia.js script performs the following operations:
PUPPETEER
1. Opens the Ecosia Search Engine — https://www.ecosia.org/
Puppeteer is an open-source project maintained by the Google
2. Asserts that the title is Ecosia
Chrome DevTools team that provides an abstracted API that
3. Asserts that the search box is visible interacts with Chrome and Chromium browsers over CDP. By default,
4. Enters nightwatch into the search box Puppeteer runs headlessly, but it can be configured to run using
5. Asserts that the Search button is visible Chrome or Chromium — in the same manner as WebDriverIO and
6. Clicks the Search button Nightwatch — as well.
After we run this test case, we can see that all four of our assertions
mkdir puppeteer-examples
have passed: cd puppeteer-examples/
npm init -y
[Ecosia.org Demo] Test Suite
============================
ℹ Connected to localhost on port 4444 (9932ms).
Next, we need to install the Puppeteer packages:
We can then run this script using the following command: This command ensures that the browsers we require — Chromium,
Firefox, and WebKit — are also installed and ready for use. We can
node example.js
install these packages using the following commands:
Once the command completes, we can see a screenshot of https:// npm i --save-dev playwright
example.com/ in the current directory: sudo npx playwright install-deps
(async () => {
for (const browserType of ['chromium',
'firefox', 'webkit']) {
const browser = await playwright[browserType].
This is only a small fraction of the capability that Puppeteer contains. launch();
Puppeteer also allows us to programmatically perform nearly all the const context = await browser.newContext();
actions that we can perform manually in a browser, including: const page = await context.newPage();
await page.goto('http://whatsmyuseragent.
• Providing input
org/');
• Interacting with forms await page.screenshot({ path: `example-
• Navigating Single-Page Applications (SPAs) ${browserType}.png` });
node example.js
PLAYWRIGHT
Playwright is the spiritual successor to Puppeteer. It was created
Once execution completes, we can see the following three
by Microsoft in 2020 (by the same team that created the original
screenshots (note the contents of each page differs based on
Puppeteer at Google) with the goal of bringing the same rich
the browser in which it was loaded):
functionality supported by Puppeteer to all mainstream browsers.
Instead of utilizing standard APIs for Firefox and WebKit browser Screenshot 1: Chromium
engines, Playwright patches these engines to enable support for
its APIs. This allows Playwright to support Chromium, Firefox, and
WebKit on Windows, Linux, and MacOS at the expense of requiring
that test cases be executed in a different browser than the one a user
will experience (i.e., a patched browser).
mkdir playwright-examples
cd playwright-examples/
npm init -y
Once we have created the new project, we can install the playwright
package. We must also install the Playwright dependencies using the SEE SCREENSHOTS 2 AND 3 ON NEXT PAGE
Screenshot 2: Firefox TestCafe is a powerful tool, and its architecture differs greatly from
the previous frameworks we have seen. This proxy architecture
ensures that tests are executed quickly and a vast array of browsers
are supported. The catch is that events — such as a button click —
are not executed natively in a browser, which differs from the user
experience and can lead to disparity in testing.
To run a TestCafe test case, we must first create a new Node project:
mkdir testcafe-examples
Screenshot 3: WebKit cd testcafe-examples/
npm init -y
Although Playwright extends the features of Puppeteer to a wider fixture `Getting Started`
array of browsers, it does not replace Puppeteer. While it does offer a .page `http://devexpress.github.io/testcafe/
greater degree of interoperability, Playwright requires that patched example`;
• NPM
Finally, we can execute our test case using the following command:
TESTCAFE
npx testcafe chrome example.js
TestCafe is an open-source project that acts as a proxy server and
injects the code used for testing. In practice, TestCafe executes a
The testcafe command accepts two arguments: (1) the alias of the
test case that exercises a developer-specified URL, which is changed
browser to use and (2) the test script to execute. In our case, we want
by TestCafe to the URL of the proxy server, injecting the HTML and
to execute our test case in Chrome, so we supply an alias of chrome.
JavaScript necessary to execute the test. This page is then delivered
to the browser and the test case is executed. When more advanced A complete list of browser aliases can be found on the Test Cafe
interaction is needed, developers can use Client Scripts and Client Browsers page.
Functions. TestCafe also automatically waits for page objects to load,
which reduces the burden on developers when accessing certain
elements on a web page.
Once the test successfully completes, the testcafe tool outputs the Once we create a new project, we can install the cypress package:
following, denoting that our test case, My first test, has passed:
npm i --save-dev cypress
Running tests in:
- Chrome 93.0.4577.63 / Linux 0.0
With the cypress package installed, we can create a new test in the
Getting Started
file, cypress/integration/example_spec.js:
✓ My first test
mkdir -p cypress/integration/
touch cypress/integration/example_spec.js
1 passed (8s)
For more information, see the following TestCafe resources: Once we create the example_spec.js file, we can add the following
content — provided by Cypress:
• Getting Started
• Guides
describe('My First Test', () => {
• GitHub
it('clicking "type" navigates to a new url', ()
• NPM
=> {
cy.visit('https://example.cypress.io')
CYPRESS
Cypress is an open-source project that runs test cases directly
cy.contains('type').click()
within the browser. Unlike other test frameworks that make remote
requests to the API of the browser or a standardized API, Cypress
cy.url().should('include', '/commands/
runs as a Node server process, which means that it runs in the same
actions')
event-loop as our application under test. This has many distinct
})
advantages over alternative approaches, including:
})
• Faster test execution
• Access to the network layer and web traffic Next, we can open the Cypress App using the following command:
• Access to useful debugging information With the dashboard open, we can click on our example_spec.js
file to open it:
Like TestCafe, Cypress also supports automatic waiting, which
removes the need for developers to add artificial waits for page
elements to load. This architecture does have some drawbacks,
though, including:
mkdir cypress-examples
cd cypress-examples/
npm init -y
Apart from running our tests visually, we can also execute them
using the command line, which removes the need to open our test WRITTEN BY JUSTIN ALBANO,
SOFTWARE ENGINEER, IBM
case in a browser.
Justin Albano is a Software Engineer at IBM
For more information, see the following Cypress resources: responsible for building software-storage and
backup/recovery solutions for some of the largest
• Getting Started Guide
worldwide companies, focusing on Spring-based REST API and
• GitHub MongoDB development. When not working or writing, he can be
found practicing Brazilian Jiu-Jitsu, playing or watching hockey,
• NPM
drawing, or reading.
CONCLUSION
The Web has changed dramatically over the last 30 years, and the
JavaScript automated test ecosystem has evolved alongside it to
better suit the needs of modern web application development.