<<Previous Post << Complete Tutorial>> Next Post>>
WebDriverIO – $$ Command
The $$ command is a way to call findElements command in order to fetch multiple elements on the page.
On passing a locator/selector it returns an array with element results and it will look for that element first and call the action on that element.
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 fetch multiple elements of Ordered List present on the page and print third element on console , using $$ command of WebDriverIO.
Using $$ Command
First, I will demonstrate the exercise where we will fetch Xpath of ordered list then after getting an array with element results will print 3rd element text on console using $$ command, followed by practically demonstrating the WebDriverIO Automation code for achieving it.
Exercise:-
1) Navigate to http://omayo.blogspot.com/
2) Finding Xpath of ordered list present on omayo.blogspot.com web application.
Practical Demonstration:
Following are the Steps to Perform above Exercise (i.e. to fetch Xpath of ordered list then after getting an array with element results will print 3rd element text on console) 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 “Double_Dollar_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 for fetching Xpath of ordered list and printing 3rd element text on console present on webpage by using $$ command of WebDriverIO 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 $$ command in webdriverio', () => { it('should fetch multiple element based on the locator passed and perform actions accordingly', ()=> { browser.url('http://omayo.blogspot.com'); const text = $$('//*[@id="HTML25"]/div[1]/ol/li'); console.log(text[2].getText()); }); });
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 ‘Double_Dollar_Command_test.js’ will get executed successfully, the chrome Browser will launch, then xpath of ordered list will be fetched 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:
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 ?
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>>
describe(“desc block”,function(){
it(“it block”,function(){
browser.get(“some url”);
browser.sleep(2000);
});
}) ;
instead of ‘function’ we used () =>, is it just another way of writing js code ?