HomeWebDriverIO

WebDriverIO – Keys Command

WebDriverIO – Keys Command

<<Previous Post                          << Complete Tutorial>>                             Next Post>>

WebDriverIO – Keys Command

Keys Command send a sequence of key strokes to the active element.

In Simple Terms: – Keys Command is used to send sequence of keyboard key strokes where key strokes send to WebDriverIO are translated to Unicode characters and based on that respective action takes place. You can also use characters like “Left arrow” or “Back space”.

Usage: – browser.keys(value)

Where value is parameter which should be sequence of keys to type or An array or a string

Pre-requisite: Have ‘Visual Studio Code’ IDE installed in your machine (Click here to install if not installed in your machine)

Here is the small Exercise to understand Keys Command by Entering Random Value in Search Bar and press Enter so that search button gets pressed and search results should come up on screen, using Keys command of WebDriverIO.

Using Keys Command

First, I will demonstrate the exercise where we will open URL in browser window, then Enter Random value in Search Bar and Press Enter from Keyboard using Keys command, followed by practically demonstrating the WebDriverIO Automation code for achieving it.

Exercise:-

1)  Navigate to http://omayo.blogspot.com/ 

2)  Entering “Random” Value to Search Bar present on Web Application.

3) Now Pressing Enter Key from Keyboard and observe the search results.

Practical Demonstration:

Following are the Steps to Perform above Exercise (i.e. To Enter Random Value in Search Bar and press Enter so that search button gets pressed and search results should come up on screen) using WebDriverIO Automation code in ‘Visual Studio Code’ IDE:

Pre-requisite: Download the Project from here (Click here to Download) and Unzip it.

1)  Open Visual Studio Code on your system.

2)  Click on File and Select Open Folder. Select the project folder on which your workspace is present.

3) Expand ‘test’ from the Explorer, right click on the displayed ‘specs’ and select ‘New File’ option as shown below:

4) Give any name of the newly created file say “Keys_Command_test.js” and copy the below given code into the newly created file as shown below and save the file:

The following is the WebDriverIO sample code to To Enter Random Value in Search Bar and press Enter so that search button gets pressed on ‘Omayo.blogspot.com’ application.

You can understand this code, by reading the comments provided in the below code:

Copy the code from here:-

describe('This Example show usage of Keys command in webdriverio', () => {
    it('should use Enter Key of Keyboard rather than clicking on search button', ()=> {
    browser.url('http://omayo.blogspot.com');
    $('td.gsc-input > input').setValue('Random'); //Writing in Search Bar 
    browser.keys('Enter')//Hitting Enter Key from Keyboard for Searching Random Word on web Application
    browser.pause(3000);
    });
});

5) Click on ‘wdio.conf.js’ to open in the IDE, and make sure the Test file name is updated properly and browser name should be changed from firefox to chrome in this file as shown below and save it:

6) Click on Terminal and Open New Terminal.

7) By Default, Powershell Terminal will open, change it to git bash as shown below:

8) Open Another Terminal in the same way we did in the step 6 and run below command in terminal

.\node_modules\.bin\wdio .\wdio.conf.js

Observe that the ‘Keys_Command_test.js’ will get executed successfully, the chrome Browser will launch, and open URL given in code and respective action will be taken by WebDriverIO Automation code.

And also, the result will be displayed as ‘Passed’ in the terminal as shown below:

This is how, we can take help of Keys Command of WebDriverIO to use key press from Keyboard. Likewise, I will explain other commands to perform different operations on Web Pages using WebDriverIO in the upcoming Articles.­­

Next Steps:

> To learn more about WebDriverIO, continue to the next post (Click on Next Post link below)

> Check complete WebDriverIO Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning ?

About Me > Ajay Lunia 

Connect to me on LinkedIn (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

<<Previous Post                          << Complete Tutorial>>                             Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *