Introduction
In this article, I will review a tool called Webdriver IO REPL interface.
The tool can be used for debugging tests, finding elements, getting to know selenium and Web automation.
In addition, we will install Selenium Grid server locally - the server is required to be up and running as a prerequisite for using this tool.
The tool is built on a framework called WebdriverIO.
Tool advantages
- The tool provides the user with the ability to run selenium commands through the command line (CLI), which gives us the ability to run our commands line by line instead of running as a whole test suite or using a breakpoint for debugging our testing scripts.
- Super simple and fast installation (detailed in the next section).
- There is excellent documentation of the Framework on which the tool is based - which includes code examples to execute each command. You can read about the tool API of here.
- You can use the tool to locate elements instead of writing long scripts to verify if an element exists/ selected, etc.
- You can navigate manually to the desired places in the browser and then run the code lines that we want. In this way, we save a lot of navigation and access that requires navigation code to reach the desired state that we want to test or inspect.
- When we receive an error from the server (for example, No such element exception / Timeout, etc) as opposed to running in a test environment where the error would cause the program to crash - using this tool the error is printed and we can continue to run commands (which greatly optimizes the work of locating elements).
Tool installation
- Install Node.js via the following link. An alternative way for Windows users to install is through Chocolatey.
- Then you need to run a Selenium Grid server, the fastest way to do this is through a node package called selenium-standalone.
- Navigate to the command line by opening the user menu, inserting “cmd” on the search menu and pressing the Enter key.
Run the following command on our command line:
npm install selenium-standalone@latest –g
selenium-standaloneinstall
The use of “-g” indicates that we want the installation to be globally available on a computer rather than a specific project.
- After the server installation has successfully completed, run the following command to install the Webdriver IO framework with which our tool communicates with the Selenium API.
npm install -g webdriverio
- The next step is to run the server - to do this, run the following command:
Selenium-standalone start
We can see that the server is running successfully at port 4444.
The server will be available to us at:
http://localhost:4444/wd/hub/static/resource/hub.html
This is what the server looks like:
The tool starts by running the following command:
wdio repl chrome
Note that Chrome is used as a browser parameter. If you type the command without this parameter and if Firefox is installed on your computer, Firefox should open by default.
This is how it looks after the tool has started:
Test case #1
The first test case is attached in the following screenshot:
As we can see, we started navigating to the Google site, storing the page title as a variable and then running an assert that checks that the page title is equal to the text I expect - in this case there is no match and therefore an error is thrown.
Test case #2
We will use the “getAttribute” command to extract the “src” of the Google image. The function accepts two parameters, the Selector and the Attribute that we want to extract.
The Console.log command is invaluable for us when we want to print to the terminal, as in the following case:
In conclusion
In this article, we reviewed the REPL tool (Read-Eval-Print-Loop for more reading on the concept here), its advantages, installation and using the tool for writing 2 simple test cases. In order to experience WebdriverIO in writing your JavaScript tests, I recommend starting with the next video to install a full workspace. Then, continue to their excellent Developer Guide here.
Happy testing!