Webdriverio Tutorial
Webdriverio Tutorial
i
WebdriverIO
Audience
This tutorial is designed for professionals working in software testing who want to hone
their skills on a robust automation testing tool like WebdriverIO. It is implemented in
[Link] and comes under the umbrella of Selenium.
Prerequisites
Prior to going through this tutorial, you should have a fair knowledge on JavaScript and
object oriented programming concepts. Besides, a good understanding of basics in
testing is important to proceed with this tutorial.
All the content and graphics published in this e-book are the property of Tutorials Point
(I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or
republish any contents or a part of contents of this e-book in any manner without written
consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely
as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I)
Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of
our website or its contents including this tutorial. If you discover any errors on our
website or in this tutorial, please notify us at contact@[Link]
i
WebdriverIO
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
7. WebdriverIO — [Link].................................................................................................................. 15
ii
WebdriverIO
iv
1. WebdriverIO – Introduction WebdriverIO
Now-a-days, the front end of the majority of applications is developed with the
JavaScript frameworks like React, Angular, and so on. WebdriverIO is really useful for
testing these applications.
This is because WebdriverIO coding is also done in JavaScript. This tool falls under the
roof of Selenium and also there are some additional APIs. If we are aware of Selenium,
then gaining knowledge in WebdriverIO is a simple task.
WebdriverIO can also be used for testing normal applications but if we are using
WebdriverIO for verifying any application implemented in React, Angular, Polymerer
[Link], and so on, we can enjoy an additional edge in building a robust framework.
If we are creating Selenium tests in JavaScript, then WebdriverIO should be the choice.
There are other tools like Cypress which is based on the JavaScript framework but it
does not fall under the umbrella of Selenium.
If we follow the npm trends for WebdriverIO downloads for the last few years, we shall
observe an upward trend towards the use of WebdriverIO available from the link
mentioned below:
[Link]
1
WebdriverIO
Reports
Some of the reports generated in WebdriverIO are as follows:
Allure
Spec
JUnit
HTML
JSON
Cucumber JSON
Services
Some of the services offered by WebdriverIO are as follows:
Appium
Docker
Selenium Standalone
ChromeDriver
Firefox Profile
DevTools
Testing Frameworks
Some of the testing frameworks supported by WebdriverIO are as follows:
Cucumber
2
WebdriverIO
Jasmine
Mocha
3
2. WebdriverIO — Prerequisite WebdriverIO
[Link]
Step 1: Based on the local operating system we have for example - macOS, Linux or
Windows, we need to select the link for download.
Step 2: A zip file gets downloaded after clicking the Download button. Click on this zip
file and the Visual Studio Code application should be available on the machine.
Step 3: Double-click on Visual Studio Code and it gets launched along with the welcome
page. The following screen will appear on your computer:
4
WebdriverIO
5
3. WebdriverIO — Architecture WebdriverIO
NodeJS
WebdriverIO
JavaScript
JSON Wire Protocol
Services
Browsers
Application
Thus the JavaScript implemented by the end-user passes a request using the
WebdriverIO via Nodejs to the Services (in the format of an HTTP command). The entire
process is done following the JSON Wire Protocol.
Services send the request to the browsers like Chrome, Firefox, and so on to execute a
test against the application under test. Thus the Services can be termed as a middle-
layer between the browser and the automation framework.
6
4. WebdriverIO — Getting Started with NodeJS WebdriverIO
WebdriverIO coding is done using JavaScript. For this, NodeJS has to be installed since it
is a JavaScript engine. Only after its installation, we can execute WebdriverIO tests. The
steps to configure NodeJS are listed below:
[Link]
Step 2: As per the local operating system (Windows, Mac or Linux) we are using, click
on the link to download the Installer. The following screen will appear on your computer:
Step 3: Once the installer is downloaded, click on it. Navigate to the [Link] Installer
welcome screen. Click on Continue. The following screen will appear on your computer:
7
WebdriverIO
Step 4: Agree to the terms of agreement of Nodejs. The following screen will appear on
your computer:
8
WebdriverIO
Step 6: Once the success message of Nodejs installation is displayed, click on Close. The
following screen will appear on your computer:
9
WebdriverIO
Step 7: To check if Nodejs is installed successfully, open the terminal and run the
command:
node
10
5. WebdriverIO — Installation of NPM WebdriverIO
Once Nodejs has been installed, we have to create a NPM folder. NPM is actually the
package manager for writing tests in JavaScript. The official page for NPM is available in
the below link:
[Link]
Once we launch this page, enter WebdriverIO in the search box and click on Search, to
get the npm packages for WebdriverIO. The following screen will appear on your
computer:
Step 2: Open the terminal and move from the current directory to the directory of the
empty folder that we have created.
npm init -y
The y parameter is given to set the default values. The following screen will appear on
your computer:
11
WebdriverIO
Step 4: The output obtained on running the command in Step 3 says that all the default
configurations have been captured within the [Link] file. It is generated within the
folder we have created (named webdriverIO) in Step 1.
This [Link] contains all the dependencies which we need to work with the
WebdriverIO project. To get any package under NPM, we can refer to the link:
[Link]
12
6. WebdriverIO — VS Code Installation WebdriverIO
In this chapter, let us understand how to install the Visual Studio (VS) Code in
WebdriverIO.
The steps to install the Visual Studio Code are listed below:
[Link]
Step 2: Depending on the local operating system we have for example - macOS, Linux
or Windows, we need to choose the link for download.
Step 3: A zip file gets downloaded after clicking the Download button. After downloading
this file has completed, click on it and the Visual Studio Code application should become
available for use.
13
WebdriverIO
Step 4: Double-click it and the Visual Studio Code application should launch along with
the welcome page.
Step 5: Click on the Open folder link and import the folder that contains the
[Link] file. The details of how the [Link] file got created are discussed in
detail in the Chapter titled Installation of NPM.
14
7. WebdriverIO — [Link] WebdriverIO
Once the [Link] file gets generated, we need to install other npm packages for
WebdriverIO. The details of how the [Link] file got created are discussed in detail
in the Chapter titled Installation of NPM.
The necessary packages for WebdriverIO in the NPM registry can be found in the below
link:
[Link]
For installation of WebdriverIO, we have to run the below command from the terminal:
npm i webdriverio
or
15
WebdriverIO
After the command gets executed successfully, the [Link] now displays the
WebdriverIO version installed.
We can verify if the WebdriverIO has installed successfully, if the folder node_modules
created within the project contains the webdriverio folder.
16
WebdriverIO
17
8. WebdriverIO — Mocha Installation WebdriverIO
[Link]
18
WebdriverIO
After the command has been executed successfully, the Mocha version installed gets
reflected within the [Link] file.
19
9. WebdriverIO — Selenium Standalone Server WebdriverIO
Installation
WebdriverIO works under the roof of Selenium. To establish communication with the
browser, we are required to install the Selenium standalone server.
Or,
npm i selenium-standalone.
20
WebdriverIO
After the command has been executed successfully, the Selenium standalone server
package version installed gets reflected within the [Link] file.
21
10. WebdriverIO — Configuration File generation WebdriverIO
WebdriverIO tests are controlled from a Configuration file. It is often considered the
heart of WebdriverIO. It contains details on what test cases to be executed, browser on
which the tests should run, global information - timeout, reports, screenshots and so on.
To get the Test Runner, we have to install the WebdriverIO CLI dependencies. To install
this and save it in the [Link] file, we have to run the below mentioned command:
After this command has been executed successfully, the version of CLI dependency shall
be reflected within the [Link] file. The following screen will appear on your
computer:
22
WebdriverIO
After this command has been executed successfully, the configuration file called the
[Link] gets created within our project. Also, the [Link] file should now
contain some more dependencies under the devDependencies field.
23
WebdriverIO
Apart from the dependencies marked in the above image, we have to add one more
dependency so that the WebdriverIO commands can execute synchronously.
npm install
To run a Configuration file from the test runner, we have to run the below given
command:
24
WebdriverIO
If we expand this folder, we shall find two sub-folders - pageobjects and specs
containing JavaScript files created by default. These are basically sample tests provided
to guide the first time users to get accustomed with the Mocha framework.
[Link]
As per the Mocha testing framework, all the test files are known as the spec files and
they should reside within the specs folder.
25
WebdriverIO
describe: This is higher in hierarchy than the it block. A test file can have
multiple describe blocks. A describe block represents a test suite. It has two
arguments - description of the test suite and an anonymous function.
it: This is lower in hierarchy than the describe block. A describe can have multiple
it blocks. An it block represents a test case and should be mandatory within a
describe block It has two arguments - description of the test case and an
anonymous function. The actual WebdriverIO code is implemented within the it
[Link] to Create Mocha File
Step 1: Right-click on the specs folder (which is within the test folder), then select New
File. The following screen will appear on your computer:
26
WebdriverIO
In the above code, the browser is the global object exposed by the WebdriverIO.
Please note: We cannot run this individual file directly. We shall take the help of the
Configuration file in order to execute it.
27
11. WebdriverIO — VS Code Intellisense WebdriverIO
Once we have completed installation of the Visual Studio Code, we should add the
intellisense in the editor so that once we begin writing the WebdriverIO commands, the
auto-suggestions of the WebdriverIO methods are displayed.
The details on how to do a VS Code installation are discussed in detail in the Chapter
titled VS Code Installation.
This is a very important feature that should be added so that the end-users do not need
to memorize the raw code for the WebdriverIO.
Step 1: Click on the New File button appearing to the right of the WebdriverIO project.
Step 2: Enter the file name as [Link]. Here, we have to specify the path of the
spec files where we are implementing our test.
If we want to apply intellisense feature to all the spec files within the test folder, we can
specify the relative path as test/spec/*.js.
28
WebdriverIO
{
"include": [
//relative path of all spec files
"test/specs/*.js",
"**/*.json",
"node_modules/@wdio/sync",
"node_modules/@wdio/mocha-framework"
]
}
Step 4: In the spec file, start writing a WebdriverIO object or a method and we shall
obtain the entire auto - suggestions.
29
WebdriverIO
30
12. WebdriverIO — [Link] file WebdriverIO
WebdriverIO tests are controlled from the Configuration file. It is often considered the
heart of WebdriverIO. It contains details on which spec files to be executed, browser on
which the tests should run, global information - base URL, timeout, reports, screenshots
and so on.
After this command has been executed successfully, the Configuration file called the
[Link] gets created within our project.
Within this file, we have to specify the path of the spec file that we want to execute
within the specs parameter.
By default, the path provided is: ./test/specs/**/*.js. This means any .js file under
the sub-folder specs (which is under the folder test) should be picked for execution.
31
WebdriverIO
To execute the test with the help of the [Link] file, we have to run the command:
After the command has been executed successfully, the page title of the application
launched is obtained in the console.
However, a lot of the logs got captured in the console. This is because the parameter
logLevel is set to info by default in the [Link] file.
32
WebdriverIO
In order to get rid of some of the logs and to obtain only those which the test case
directs, we can set this parameter to silent.
33
WebdriverIO
After the command has been executed successfully, we find lesser logs and the page title
of the application launched - About Careers at Tutorials Point - Tutorialspoint is obtained
in the console.
34
13. WebdriverIO — Xpath Locator WebdriverIO
For this, our first job is to identify the element. We can create an xpath for an element
for its identification. The rules to create a xpath expression are discussed below:
//tagname[@attribute='value']
For example,
//img[@alt='tutorialspoint']
Let us see the html code of the highlighted link - Home. The following screen will appear
on your computer:
//a[@title='TutorialsPoint - Home'].
35
WebdriverIO
In the WebdriverIO code, we can specify the xpath expression of an element in the
below format:
Let us identify the text highlighted in the below image and obtain its text:
//li[@class='heading']
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
36
WebdriverIO
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation
37
WebdriverIO
After the command has been executed successfully, the text of the element - About
Tutorialspoint is printed in the console.
We can create an xpath for an element for its identification. However, there are
scenarios where there are no HTML attributes or tagname available to uniquely identify
an element.
In such a situation, we can create an xpath for an element with the help of the text
visible on the page by using the text function. The text function is case-sensitive.
The rule to create a xpath expression with visible text is discussed below:
//tagname[text()='displayed text'].
For example,
//li[text()='WebdriverIO']
Let us identify the element highlighted in the below image with the help of the visible
text in xpath:
38
WebdriverIO
The xpath of the above highlighted element using the text() function shall be as follows:
//li[text()='About Tutorialspoint']
To begin, follow Steps 1 to 5 from the Chapter - Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
39
WebdriverIO
});
The details on how to create a Configuration file are discussed in detail in the Chapter
[Link] file and Chapter titled Configuration File generation
After the command has been executed successfully, the text of the element - About
Tutorialspoint is printed in the console.
40
14. WebdriverIO — CSS Locator WebdriverIO
For this, our first job is to identify the element. We can create a css for an element for
its identification. The rules to create a css expression are discussed below:
tagname[attribute='value']
Here, the tagname is optional. We can also specifically use the id and class attribute to
create a css expression.
With id, the format of a css expression should be tagname#id. For example, input#txt
[here input is the tagname and the txt is the value of the id attribute].
For example,
[Link]-txt
Here, input is the tagname and the cls-txt is the value of the class attribute.
In the WebdriverIO code, we can specify the css expression of an element in the below
format:
Let us identify the text highlighted in the below image and obtain its text:
41
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
42
WebdriverIO
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
[Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the text of the element - About
Tutorialspoint is printed in the console.
43
15. WebdriverIO — Link Text Locator WebdriverIO
We can identify an anchor element with a matching text. In the WebdriverIO code, we
have the option to specify the link of an element in the below format:
Let us identify the link highlighted in the below image and click on it:
The link highlighted in the above image has a tagname - a and the anchor text - Terms
of Use.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
44
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the title of the page after clicking -
Terms of Use - Tutorialspoint is printed in the console.
45
WebdriverIO
We can identify an anchor element with a matching text. In the WebdriverIO code, we
have the option to specify the partial link of an element in the below format:
The partial link text is similar to link text with the only difference being that it assists in
scenarios where a few characters of an anchor element are fixed and the remaining ones
are dynamic.
Let us identify the link highlighted in the below image and click on it:
The link highlighted in the above image has a tagname - a and the anchor text - Terms
of Use.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
46
WebdriverIO
// launch url
[Link]('[Link]
//identify element with partial link text then click
$("*=Terms of").click()
[Link]('Page title after click: ' + [Link]())
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the title of the page after clicking -
Terms of Use - Tutorialspoint is printed in the console.
47
16. WebdriverIO — ID Locator WebdriverIO
For this, our first job is to identify the element. We can use the id attribute for an
element for its identification. It is a very useful locator and speeds up the execution of
automation tests in comparison to all the locators.
In the WebdriverIO code, we have the option to specify the value of the id attribute of an
element in the below format:
$('=#value of id attribute')
Let us identify the element highlighted in the below image and click on it:
48
WebdriverIO
The link highlighted in the above image has a tagname - a and the id attribute value -
redirect.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
49
WebdriverIO
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the title of the page after clicking -
The Internet is printed in the console.
50
17. WebdriverIO — Tag Name Locator WebdriverIO
For this, our first job is to identify the element. We can use the HTML tagname for an
element for its identification. In the WebdriverIO code, we have the option to specify the
tagname of an element in the below format:
$('<element tagname>')
Let us identify the element highlighted in the below image and obtain its text:
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
51
WebdriverIO
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the text of the element - About
Careers at Tutorials Point is printed in the console.
52
18. WebdriverIO — Class Name Locator WebdriverIO
For this, our first job is to identify the element. We can use the class name attribute for
an element for its identification. It is a very useful locator and speeds up the execution
of automation tests in comparison to xpath.
In the WebdriverIO code, we have the option to specify the value of the class name
attribute of an element in the below format:
Let us identify the text highlighted in the below image and obtain its text:
The element highlighted in the above image has the class attribute value as heading.
53
WebdriverIO
// launch url
[Link]('[Link]
//identify element with Class Name then obtain text
[Link]($(".heading").getText() + " - is the text.")
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation. The following
screen will appear on your computer:
After the command has been executed successfully, the text of the element - About
Tutorialspoint is printed in the console.
54
19. WebdriverIO — Name Locator WebdriverIO
For this, our first job is to identify the element. We can use the name attribute for an
element for its identification. This locator is deprecated now and is only compatible with
old browsers that are based on JSONWireProtocol or Appium.
In the WebdriverIO code, we have the option to specify the value of the name attribute
of an element in the below format:
$('[name attribute=''value'']')
Let us identify the edit box highlighted in the below image and enter text:
The element highlighted in the above image has the name attribute value as search.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
55
WebdriverIO
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
56
20. WebdriverIO — Expect statement for WebdriverIO
assertions
In assertion, we can compare an expected result of a test with an actual. If both are
similar, a test should pass, else it should fail. The expect statement in WebdriverIO can
be applied on the browser, a mock object, or an element.
We have to add a NodeJS library called Chai. Chai library contains the expect statement
that is used for the Assertion.
We have to add the below statement in our code to implement the Chai Assertion:
const e = require('chai').expect
toHaveUrl
It checks whether the browser has opened a particular [Link] syntax is as follows:
expect(browser).toHaveUrl('[Link]
toHaveUrlContaining
expect(browser).toHaveUrlContaining('tutorialspoint')
toHaveUrl
toBeDisplayed
57
WebdriverIO
const e = $('#loc')
expect(e).toBeDisplayed()
toExist
const e = $('#loc')
expect(e).toExist()
toBePresent
const e = $('#loc')
expect(e).toBePresent()
toBeExisting
It is similar to toExist.
toBeFocussed
const e = $('#loc')
expect(e).toBeFocussed()
toHaveAttribute
const e = $('#loc')
expect(e).toHaveAttribute('name', 'search')
toHaveAttr
It is similar to toExist.
toHaveAttributeContaining
58
WebdriverIO
const e = $('#loc')
expect(e).toHaveAttributeContaining('name', 'srch')
toHaveElementClass
const e = $('#loc')
expect(e).toHaveElementClass('name', { message: 'Not available!', })
toHaveElementClassContaining
const e = $('#loc')
expect(e).toHaveElementClassContaining('nam')
toHaveElementProperty
const e = $('#loc')
expect(e).toHaveElementProperty('width', 15)
//verify negative scenario
expect(e).[Link]('width', 20)
toHaveValue
const e = $('#loc')
expect(e).toHaveValue('Selenium', { ignoreCase: false})
toHaveValueContaining
const e = $('#loc')
expect(e).toHaveValueContaining('srch')
toBeClickable
const e = $('#loc')
expect(e).toBeClickable()
toBeDisabled
const e = $('#loc')
expect(e).toBeDisabled()
//verify negative scenario
expect(e).[Link]()
toBeEnabled
const e = $('#loc')
expect(e).toBeEnabled()
toBeSelected
toBeChecked
toHaveHref
const e = $('<a>')
expect(e).toHaveHref('[Link]
toHaveLink
It is same as toHaveHref.
toHaveHrefContaining
const e = $('<a>')
expect(e).toHaveHrefContaining('[Link]')
60
WebdriverIO
toHaveLinkContaining
toHaveId
const e = $('#loc')
expect(e).toHaveId('loc')
toHaveText
const e = $('#loc')
expect(e).toHaveText('Learning WebdriverIO')
toHaveTextContaining
const e = $('#loc')
expect(e).toHaveTextContaining('Learning WebdriverIO')
toBeDisplayedInViewpoint
const e = $('#loc')
expect(e).toBeDisplayedInViewpoint()
toBeRequested
const m = [Link]('**/api/list*')
expect(m).toBeRequested()
toBeRequestedTimes
61
WebdriverIO
const m = [Link]('**/api/list*')
expect(m).toBeRequestedTimes(2)
To begin, follow the steps 1 to 5 from the Chapter titled Happy path flow with
webdriverIO which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
62
WebdriverIO
After the command has been executed successfully, we find the result as 1 failed. Since
the Expected: is Terms of Use - Tuter and the Received: output is Terms of Use -
Tutorialspoint.
Also, the WebdriverIO expect statement has highlighted the part of the text where the
Expected: and the Received: texts are not matching.
63
21. WebdriverIO — Happy path flow WebdriverIO
Let us create a simple happy flow to demonstrate how to create a basic WebdriverIO
test:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation
Step 6: Add the below code within the Mocha spec file created.
Step 7: Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
64
WebdriverIO
Step 8: On investigating further on the output, we shall see the test within the spec file
[Link] is marked as PASSED.
The browser version and operating system on which the test has been executed, session
id, name of the spec file, test suite name - Tutorialspoint Application, test case name -
Happy Flow, duration of test execution, and so on, have also been captured in the
console.
65
22. WebdriverIO — General Browser Commands WebdriverIO
Some of the general browser commands used in WebdriverIO are listed below:
[Link](URL)
This command is used to launch an application whose URL is passed as a parameter.
[Link]('[Link]
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with webdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
[Link]()
66
WebdriverIO
This command is used to get the title of a page presently launched in the browser. The
value is returned in the form of a string. This command does not accept any parameters.
If the page has no title, a null string is returned.
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titledHappy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
[Link]()
This command is used to get the URL of a page presently launched in the browser. The
value is returned in the form of a string. This command does not accept any parameters.
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
67
WebdriverIO
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
[Link]()
This command is used to get the page source of a page presently launched in the
browser. The value is returned in the form of a string. This command does not accept
any parameters.
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
68
WebdriverIO
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
[Link]()
This command is used to maximise the present browser window.
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
69
WebdriverIO
[Link]("[Link]
//maximize browser
[Link]()
});
});
70
23. WebdriverIO — Handling Browser Size WebdriverIO
While working on automation tests in WebdriverIO, we may be required to set the size of
the window and obtain the size of the window. The window size refers to the window
height and width.
[Link](250, 450)
This command is used to set the window size. Here, the window size shall be set to width
- 250 and height - 450.
[Link](250, 450)
[Link]()
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
71
WebdriverIO
[Link](500, 450)
//get window size
[Link]([Link]())
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the dimension of the browser
window- {width: 500, height: 450} is printed in the console.
72
24. WebdriverIO — Browser Navigation WebdriverIO
Commands
Some of the browser navigation commands used in WebdriverIO are listed below:
[Link](URL)
[Link]('[Link]
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
73
WebdriverIO
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
74
WebdriverIO
After the command has been executed successfully, the page title obtained on navigating
back in browser history - About Careers at Tutorials Point - Tutorialspoint is printed.
Then, the page title obtained on navigating forward in browser history - Free Online IDE
and Terminal is printed.
Finally, the page title obtained after page refresh - Free Online IDE and Terminal is
printed.
75
25. WebdriverIO — Handling Checkboxes and WebdriverIO
Dropdowns
We can handle checkboxes in the UI while automating a test using WebdriverIO. The
checkboxes are identified in the html code with the tagname as input and type as
checkbox.
76
WebdriverIO
click()
let p = $('#loc')
[Link]()
isSelected()
let p = $('#loc')
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
77
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, all the Assertions are executed as
per expectation and we have received a passed test.
Handling Dropdowns
We can handle drop downs in the UI while automating a test using WebdriverIO. The
static drop downs are identified in the html code with the tagname as select and its
options have the tagname as option.
78
WebdriverIO
selectByVisibleText
This method is used to select an option which matches with the visible text of an option
passed as a parameter to this method.
let p = $('#loc')
[Link]('By Subject')
selectByAttribute
This method is used to select an option which matches with the value of any attribute
passed as a parameter to this method.
let p = $('#loc')
[Link]('value', 'subject')
selectByIndex
This method is used to select an option which matches with the index/position of an
option passed as a parameter to this method. The index starts with 0.
let p = $('#loc')
[Link](1)
getValue()
This method is used to get the attribute value of an option selected in the dropdown.
let p = $('#loc')
79
WebdriverIO
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
80
WebdriverIO
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, first the value of the option selected
with the option index - name is printed in the console.
Then, the value of the option selected with the option visible text - subject is printed in
the console.
Finally, the value of the option selected with the option attribute value - name is printed
in the console.
81
26. WebdriverIO — Mouse Operations WebdriverIO
WebdriverIO can perform operations like hovering a mouse on an element by using the
moveTo method. This method shall move the mouse to the middle of the element.
let p = $('#loc')
[Link]()
In the below image, on hovering over the Mouse Hover button, the Top and Reload
buttons get displayed.
On moving the mouse out of the Mouse Hover button, the Top and Reload buttons get
hidden.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
82
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the boolean value is printed in the
console. This is returned by the isDisplayed() function which returns true if an element is
displayed on the page.
83
27. WebdriverIO — Handling Child Windows/Pop WebdriverIO
ups
A new child window can open on clicking a link or a button. WebdriverIO by default has
control over the main browser window, in order to access the elements on the child
window, the WebdriverIO control has to be switched from the main page to the child
window.
[Link]()
This method yields the window handle ids of all the currently opened browser windows in
the form of a list. If there are two opened windows, the zero index of the list has the
handle id of the parent window and the first index shall point to the window handle of
the child.
var x = [Link]()
[Link]()
This method yields the window handle id of the browser which is in focus.
let l = [Link]()
This method is used to switch focus from one browser window to another opened window
whose window handle id is passed as a parameter to this method.
[Link](x)
In the below image, on clicking the Sign in with Apple button, a child window opens
having the browser title as Sign in with Apple ID. Let us try to switch to the child window
and access elements there.
84
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
85
WebdriverIO
$('#apple-signin-button').click()
//get all window handle ids in list
var l = [Link]()
//switch to child window
[Link](l[1])
//get page title of child window
[Link]([Link]() + ' - Page title of child window')
//close child window
[Link]()
//switch to parent window
[Link](l[0])
//get page title of parent window
[Link]([Link]() + ' - Page title of parent window')
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, first the page title of the child
window - Sign in with Apple ID gets printed in the console. Then, the page title of the
parent window - Sign In | Indeed Accounts get printed in the console.
86
28. WebdriverIO — Hidden Elements WebdriverIO
WebdriverIO can handle hidden elements. There are occasions when submenus get
displayed only on hovering over the main menu. These submenus are initially hidden
with the CSS property - display:none.
In the below image, on hovering over the Sign in menu, the Sign in button gets
displayed.
On moving the mouse out of the Sign in menu, the Sign in button gets hidden.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
87
WebdriverIO
[Link]('[Link]
//identify element then hover mouse
const e = $("#nav-link-accountList")
[Link]()
[Link](2000)
//click on hidden element
$('=Sign in').click()
//get page title
[Link]([Link]() + ' - Page title after click')
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the page title obtained by clicking
the hidden Sign in button - Amazon Sign-In gets printed in the console.
88
29. WebdriverIO — Frames WebdriverIO
The frames in an html code are represented by the frames/iframe tag. WebdriverIO can
handle frames by switching from the main page to the frame.
[Link]('<frame id/index/locator>')
This method is used to switch focus from the main page to a frame. The frame id, index
or locator is passed as a parameter to this method.
[Link](x)
To switch the focus from the frame to the main page, we have to pass null as a
parameter to the [Link] method.
Let us see the html code of an element inside a frame and obtain the text - BOTTOM
inside it.
The tagname highlighted in the above image is frame and the value of its name attribute
is frame-bottom.
89
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation
90
WebdriverIO
After the command has been executed successfully, the text inside the frame - BOTTOM
gets printed in the console.
91
30. WebdriverIO — Drag and Drop WebdriverIO
WebdriverIO can perform mouse operations like drag and drop using the dragAndDrop
method. With this, we execute clicking and holding events on the present object
(source), then pass the object to the target element. Finally, release the mouse.
let p = $('#loc')
let t = $('#target')
[Link](t)
Let us perform the drag and drop functionality for the below elements:
In the above image, the element with the name - Drag me to my target has to be
dragged and dropped on the element - Dropped!
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which is as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
92
WebdriverIO
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
93
WebdriverIO
After execution, the element with the name - Drag me to my target has been dragged
and dropped on the element - Dropped!
94
31. WebdriverIO — Double Click WebdriverIO
WebdriverIO can perform mouse operations like double click using the doubleClick
method. With this, we can perform double clicking on the given element on the
webpage.
let p = $('#loc')
[Link]()
Here, it is seen that on double clicking the Double-Click me To See Alert button, an alert
box gets generated.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
95
WebdriverIO
// launch url
[Link]('[Link]
[Link]/2014/09/[Link]')
//identify element then double click
$("button").doubleClick()
//get Alert Text
[Link]([Link]() + ' - Alert Text')
//accept Alert
[Link]()
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
[Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the Alert text is generated on
double-click - You double clicked me.. Thank You.. gets printed in the console.
96
32. WebdriverIO — Cookies WebdriverIO
[Link]
This is used to set a single cookie or multiple cookies for the present page. To set a
cookie for a page, we have to first launch and stay on that page.
Here, cookie is the cookie object or object array and can contain the following values:
[Link]
This is used to get a cookie from the existing page. If the cookie name is provided as a
parameter to this method, then that particular cookie shall be obtained. Else, all the
cookies from the present page shall be obtained.
97
WebdriverIO
Or,
[Link]
This is used to delete a cookie from the existing page. If the cookie name is provided as
a parameter to this method, then that particular cookie shall be deleted. Else, all the
cookies from the present page shall be deleted.
Or,
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
98
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
99
WebdriverIO
After the command has been executed successfully, first the cookie details having the
name as topic1 get printed in the console. Then, both the cookie details having names as
topic1 and topic2 get displayed.
100
WebdriverIO
Then, we deleted the cookie with the name topic2, so the other cookies got printed in
the console. Finally, on deleting all the cookies, an empty array is printed in the console.
101
33. WebdriverIO — Handling Radio Buttons WebdriverIO
We can handle radio buttons in the UI while automating a test using WebdriverIO. The
radio buttons are identified in the html code with the tagname as input and type as
radio.
click()
const l = $('.rad')
[Link]()
isSelected()
const l = $('.rad')
[Link]()
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
102
WebdriverIO
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
103
WebdriverIO
After the command has been executed successfully, the boolean value is printed in the
console. This is returned by the isSelected() function which returns true as the radio
button is selected in the previous step.
104
34. WebdriverIO — Chai Assertions on WebdriverIO
webelements
Chai is an assertion library for nodes. It is mainly used in the BDD and TDD framework.
It can easily be integrated with any JavaScript testing framework. The official
documentation of Chai is available in the below link:
[Link]
For installation of Chai and making its entry in the [Link] file, run the following
command:
The details on the [Link] file are discussed in detail in the Chapter titled
[Link].
105
WebdriverIO
After installation we have to add the below statement to add expected style Chai in our
code.
require('chai').expect
const c = require('chai').expect
c([Link]()).[Link]('subject')
Let us implement a Chai assertion and verify if the option selected in the below
dropdown is as per the expected result.
106
WebdriverIO
The details on how to handle a dropdown is discussed in detail in the Chapter - Handling
Dropdowns.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
require('chai').expect
//import chai library
const c = require('chai').expect
describe('Tutorialspoint application', function(){
//test case
it('Drodowns with Chai Assertion', function(){
// launch url
[Link]('[Link]
//identify dropdown
const p = $("select[name='selType']")
//select by index
[Link](1)
//get option selected
[Link]([Link]() + ' - option selected by index')
//verify option selected with chai assertion
107
WebdriverIO
c([Link]()).[Link]('name')
//select by visible text
[Link]('By Subject')
//get option selected
[Link]([Link]() + ' - option selected by visible text')
//verify option selected with chai assertion
c([Link]()).[Link]('subject')
//select by value attribute
[Link]('value', 'name')
//get option selected
[Link]([Link]() + ' - option selected by attribute value')
//verify option selected with chai assertion
c([Link]()).[Link]('name')
});
});
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, first the value of the option selected
with the option index - name is printed in the console. Then, the value of the option
108
WebdriverIO
selected with the option visible text - subject is printed in the console. Finally, the value
of the option selected with the option attribute value - name is printed in the console.
Also, we get a PASSED result, pointing to the fact that all the Chai assertions applied on
the dropdown have passed.
Let us implement another Chai assertion and verify if the alert text obtained is as per the
expected result.
The details on how to handle an alert are discussed in detail in the Chapter titled Alerts.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
109
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, at first true is printed in the console
as it is returned by the [Link]() method. Then, the Alert text - I am a JS
prompt is printed in the console.
Also, we get a PASSED result, pointing to the fact that the Chai assertion applied on the
alert text has passed.
110
35. WebdriverIO — Multiple Windows/Tabs WebdriverIO
[Link]()
This method yields the window handle ids of all the currently opened browser windows in
the form of a list. If there are two opened windows, the zero index of the list has the
handle id of the parent window and the first index shall point to the window handle of
the tab.
var x = [Link]()
[Link]()
This method yields the window handle id of the browser which is in focus.
let l = [Link]()
This method is used to switch focus from the browser window in focus to another opened
browser window whose window handle id is passed as a parameter to this method.
[Link](x)
In the below image, on clicking the Click Here link, a new tab opens having the browser
title as New Window. Let us switch to the new tab and access elements in there.
111
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which is as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
112
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the page title of the tab window -
New Window gets printed in the console. Then, the page title of the parent window - The
Internet gets printed in the console.
113
36. WebdriverIO — Scrolling Operations WebdriverIO
We can perform scrolling operations with the WebdriverIO by using the scrollIntoView
method. This method does not accept any parameter and can be applied to the browser
object or on a particular element.
const p = $('#loc')
[Link]()
Or,
[Link]()
In the below image, let us scroll to the footer element link - Helping and click on it.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO.
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
114
WebdriverIO
//identify element
const e = $("=Helping")
//scroll to element
[Link]()
[Link]()
//get page title
[Link]([Link]() + ' - Page time after click')
});
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the page title of the page obtained
on clicking the link after scrolling - Helping Tutorials Point - Tutorialspoint gets printed in
the console.
115
37. WebdriverIO — Alerts WebdriverIO
[Link]()
This method is used to verify if there is an alert in the page. It returns true, if the Alert is
present, else returns false
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
In the below image, on clicking Click for JS Alert, an Alert is displayed. Let us obtain the
text on the Alert.
116
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
117
WebdriverIO
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, the first true is printed in the
console as it is returned by the [Link]() method. Then the Alert text - I am
a JS prompt is printed in the console.
118
38. WebdriverIO — Debugging Code WebdriverIO
To debug the WebdriverIO code in the Visual Studio Code editor, we have to enable the
nightly version of JavaScript Debugger. Debugging is one of the most important steps for
identifying the root cause of an error in code.
Enable Debugging
The steps to enable debugging are listed below:
Step 1: Navigate to the link below if you are using Windows or Linux operating system:
[Link]
Step 2: Click on Install. The following screen will appear on your computer:
Step 3: Create a folder called the .vscode within the project. Then create a file
[Link] within this folder. The following screen will appear on your computer:
119
WebdriverIO
{
"configurations": [
{
"name": "Webdriver IO",
"type": "node",
"request": "launch",
"args": ["[Link]", "--spec", "${file}"],
"cwd": "${workspaceFolder}",
"autoAttachChildProcesses": true,
"program": "${workspaceRoot}/node_modules/@wdio/cli/bin/[Link]",
"console": "integratedTerminal",
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"${workspaceFolder}/lib/**/*.js",
"<node_internals>/**/*.js"
]
120
WebdriverIO
},
]
}
Step 5: Add a breakpoint in the spec file. The following screen will appear on your
computer:
Step 6: Go to the Run menu and select the option Start Debugging. The following screen
will appear on your computer:
Step 7: The execution shall get triggered in Debugger mode, with an orange band at the
bottom. Debugger attached message should be reflected in the Terminal console. Also,
the execution shall halt at the breakpoint. We have to manually resume it again.
121
WebdriverIO
122
39. WebdriverIO — Capturing Screenshots WebdriverIO
Here, the name along with the path where the screenshot is to be saved is passed as a
parameter to the method. In the WebdriverIO, we don't have the option to capture a
screenshot for a particular element.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
123
WebdriverIO
});
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation. The following
screen will appear on your computer:
After the command has been executed successfully, a file named [Link] gets
generated within the project folder. It contains the captured screenshot of the page.
124
40. WebdriverIO — JavaScript Executor WebdriverIO
Inside the WebdriverIO, the JavaScript Executor is bundled and called executeScript. The
JavaScript Executor is capable of performing all the tasks on a page whenever normal
WebdriverIO methods are not working as expected.
[Link]("JavaScript command")
To enter a text - AB into an edit box having id as txt, use the command given below:
[Link]("[Link]('txt').value='AB'")
[Link]("[Link]('.lnk').click()")
[Link]("[Link](0)")
var t = [Link]("return
[Link]('bln').innerHTML").toString()
[Link]("[Link](0,350)")
[Link]("[Link](0, [Link])")
The command given below is used to scroll down upto an element having class as tcl.
[Link]("[Link]('.tcl').scrollIntoView()")
[Link]("[Link]()")
[Link]("[Link]()")
[Link]("return [Link]")
125
41. WebdriverIO — Waits WebdriverIO
The waitUnit method in WebdriverIO is a standard method to wait for an action /element
on the page. It waits for a criterion to be met (a true value).
Here,
In the below image, let us click on the link - Team and wait for the text - Team @
Tutorials Point to appear on the page.
On clicking the link Team, the highlighted message is displayed on the page.
126
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows: Step 1: Install NodeJS. The details on how to perform this
installation are given in detail in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
127
WebdriverIO
The details on how to create a Configuration file are discussed in detail in the Chapter
[Link] file and Chapter titled Configuration File [Link] following
screen will appear on your computer:
After the command has been executed successfully, the text generated on clicking the
Team link - Team @ Tutorials Point gets printed in the console.
128
42. WebdriverIO — Running Tests in Parallel WebdriverIO
We can run WebdriverIO tests in parallel mode. For this we have to create more than
one spec file within the test folder. The numbers of threads in which parallel tests can
run are defined by the parameters in the Configuration file - [Link] file.
The details on how to create a Configuration file are discussed in detail in the Chapter -
[Link] file and Chapter - Configuration File generation to store WebdriverIO
settings.
Let us take a project having three spec files within the test folder. The following screen
will appear on your computer:
129
WebdriverIO
spec files within the test folder would get triggered on running the command given
below:
After the command has been executed successfully, we shall see all the three spec files -
[Link], [Link] and [Link] getting triggered for execution
simultaneously.
Also, the maxInstances field in the [Link] determines the maximum number of
threads possible to trigger the parallel execution. By default, the value is set to 10. Here,
we have three spec files, so the maxInstances = 10, holds true.
130
WebdriverIO
There is another field called capabilities within the [Link] file. Within this, we have
a parameter called the maxInstances. It determines the number of instances that can be
opened simultaneously by the Chrome browser during the parallel run.
Let us set the value 3 for the parameter maxInstances outside the capabilities field and
then set the value 2 for the field maxInstances inside the capabilities field. The value set
for maxInstances within the capabilities overrides the value set for maxInstances outside
the capabilities.
131
WebdriverIO
After the command has been executed successfully, we shall see two spec files -
[Link] and [Link] getting triggered for execution simultaneously in Chrome.
They are initially in RUNNING status.
Once the status of [Link] moved to PASSED, the third spec [Link] moved to
the status of RUNNING. The following screen will appear on your computer:
132
43. WebdriverIO — Data Driven Testing WebdriverIO
We can achieve data driven testing with WebdriverIO. Data driven testing is required
when we need to execute the same test case multiple times with different combinations
of data. Here, we shall see how to use an external JSON file to hold data.
In the WebdriverIO project all the test files are created within the specs folder. The
specs folder resides within the test folder. We shall create another folder, say testData
within the test folder.
The testData folder shall contain the JSON files which hold the different sets of data in
key-value pairs. Also, if we have three test files within the spec folder and we want to
have data driven testing for all these files, we need to create three JSON files.
Each of these JSON files should be used dedicatedly for each test file within the spec
folder. We shall create a JSON file, say [Link] within the testData folder.
[
{
"email":"test@[Link]",
"password":"12"
},
{
"email":"test12@[Link]",
"password":"34"
}
]
133
WebdriverIO
We shall parse this JSON file and convert it in string format. This is done by adding the
below library:
const s =require('fs')
Then to parse the JSON file, we shall use the readFileSync method and pass the relative
path of the JSON file file as a parameter to this method. Finally, store this in an object,
say c. This object shall contain all the data.
let c = [Link]([Link]('test/testData/[Link]'))
Then, we shall iterate the same test case over the two sets of data with the help of the
loop. This loop has to be implemented just before the block and it should pass the data
keys as declared in the JSON file.
With the above set of data, we shall validate the login page of the LinkedIn application.
On clicking on the Sign in button after entering an email and password of less than 6
characters, an error message - The password you provided must have at least 6
characters should be thrown.
134
WebdriverIO
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
135
WebdriverIO
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
136
WebdriverIO
After the command has been executed successfully, the error text - The password you
provided must have at least 6 characters gets printed in the console twice.
Also, it shows the message 2 passing as the same test case defined in one block has
executed two times with two different sets of data.
137
44. WebdriverIO — Running Tests from WebdriverIO
command-line parameters
We can control running tests using the command-line parameters. Let us take a
scenario, where we have four test files within the spec folder in the WebdriverIO project.
Suppose we want to trigger only the files [Link] and [Link] using the
command-line parameters. To do this we have to add a parameter called suites in the
Configuration file [Link] file.
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
138
WebdriverIO
Let us consider that the files [Link] and [Link] belong to a suite called the
group1 and the files [Link] and [Link] belong to a suite called the group2.
We need to add this information to the [Link] file under the suite parameter as
given below.
suites: {
group1: ['test/specs/[Link]', 'test/specs/[Link]'],
group2: ['test/specs/[Link]', 'test/specs/[Link]']
},
To trigger the test files [Link] and [Link] belonging to group1, we have to
run the command given below:
139
WebdriverIO
After the command has been executed successfully, we see only the two test files
[Link] and [Link] under the specs folder have been triggered for execution.
Suppose we want to trigger only the file [Link] using the command-line
parameters. To trigger only the test file [Link], we have to run the following
command:
After the command has been executed successfully, we see only the test file [Link]
under the specs folder has been triggered for execution.
140
WebdriverIO
Besides, if we want to trigger multiple test files [Link] and [Link], the
command should be as follows:
Suppose we want to exclude only the file [Link] from execution. To do this we
have to add a relative path of the file that we want to exclude under the exclude
parameter in the Configuration file [Link] file as given below.
exclude: [
// 'path/to/excluded/files'
'test/specs/[Link]'
],
141
WebdriverIO
After the command has been executed successfully, we see the test file [Link]
under the specs folder has been excluded from execution.
142
45. WebdriverIO — Execute Tests with Mocha WebdriverIO
Options
A test file within the specs folder consists of the describe and it blocks. A describe block
refers to the test suite and the it block refers to the test case. A describe block can have
multiple blocks.
The details on how to create describe and it blocks are discussed in detail in the Chapter
titled Happy path flow with Webdriverio.
To verify if a new build obtained from the development team is a healthy one, we need
not execute all the test cases within a suite. A few test cases are identified for
smoke/sanity testing and they are executed once we have a new build.
We can use the Mocha option called Grep to group test cases and run them together. For
this, we have to add a keyword, say Smoke within the it description. Then at the
runtime, we can instruct the WebdriverIO test to only trigger the it blocks which have
Smoke in its description.
Let us take a test file having four it blocks. Out of the four it blocks, there are two it
blocks having the keyword Smoke in description.
To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO
which are as follows:
Step 1: Install NodeJS. The details on how to perform this installation are given in detail
in the Chapter titled Getting Started with NodeJS.
Step 2: Install NPM. The details on how to perform this installation are given in detail in
the Chapter titled Installation of NPM.
Step 3: Install VS Code. The details on how to perform this installation are given in
detail in the Chapter titled VS Code Installation.
Step 4: Create the Configuration file. The details on how to perform this installation are
given in detail in the Chapter titled Configuration File generation.
Step 5: Create a spec file. The details on how to perform this installation are given in
the Chapter titled Mocha Installation.
Step 6: Add the below code within the Mocha spec file created.
143
WebdriverIO
[Link]('[Link]
//identify element with Class Name then obtain text
[Link]($(".heading").getText() + " - is the text.")
});
});
To trigger only the it blocks connected with Smoke, run the Configuration file -
[Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, we find out of the four it blocks,
only two it blocks (having Smoke tag in description) have been executed.
145
46. WebdriverIO — Generate HTML reports from WebdriverIO
Allure
For installation of Allure and creating it’s entry in the [Link] file, we have to run
the below mentioned command:
The details on [Link] are discussed in the Chapter titled [Link] file.
After installation of the Allure, we have to configure the output directory in the
Configuration file [Link] within the reporter options by adding the below code.
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
reporters: [['allure', {
outputDir: 'allure-results',
146
WebdriverIO
disableWebdriverScreenshotsReporting: false,
}]],
Here, the outputDir has the default directory of /allure-results. After automation is
completed, we shall find this directory generated. It shall contain the .xml files for each
of the test files within the specs folder included in the run along with .txt, .png and other
files.
Also, to attach the screenshot of the failure test, we have set the parameter
disableWebdriverScreenshotsReporting to false.
However, we also need to add an afterStep hook in the [Link] file having the code
as shown below:
147
WebdriverIO
Run the Configuration file - [Link] file with the following command:
The details on how to create a Configuration file are discussed in detail in the Chapter
titled [Link] file and Chapter titled Configuration File generation.
After the command has been executed successfully, a folder called allure-results(as
specified in the [Link]) gets generated within the WebdriverIO project. It contains
the reports in xml format.
Next, we have to convert these reports to the HTML format. For this, we shall first install
the Allure Commandline tool for generating Allure reports from the test results.
After the installation, we can generate the results in HTML format with the below
mentioned command:
After the command has been executed successfully, a browser is opened containing the
test result. The following screen will appear on your computer:
On clicking the failed test(marked with red), we shall get the details of the test along
with the expected, actual output and screenshot of the failure(obtained on expanding
Response).
149