<<Previous Post << Complete Tutorial>> Next Post>>
WebDriverIO – isSelected
isSelected Command Returns true when <option> or <input> element of type checkbox or radio is currently selected else it Returns False.
Syntax :- $(selector).isSelected()
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 where we will use isSelected() on both the Element i.e., on Checkbox and Radio Element present in WebApplication ‘Omayo.blogspot.com’ and see the result of what isSelected() returns based on the current state of Checkbox and Radio Element present on the page.
Using isSelected() command
First, I will demonstrate the exercise where we will find out the xpath of the checkbox and Radio Element present on the page and will use isSelected() command to see what it returns. Generally, For Selected Elements it should return True and for non selected elements it should return false. Let’s Find it out.
Exercise:-
1) Navigate to http://omayo.blogspot.com/
2) Find Xpath of CheckBox or Radio Element Present on the Page.
3) Find out results provided by isSelected() on different states of checkbox or radio button element present on the page.
Practical Demonstration:
Following are the steps to Perform above exercise (i.e., to find out the result provided by isSelected() on different states of checkbox or radio button element present on the page)
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 ‘isSelected_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 using isSelected() Command of WebDriverIO on CheckBox or Radio Button Element present on ‘omayo.blogspot.com’ application.
You can understand this code, by reading the comments provided in the below code:
beforeEach(function(){ browser.url('http://omayo.blogspot.com'); }); describe('This Example show usage of isSelected() command in webdriverio', () => { it('should give us true if isSelected() has been applied on selected Radio button element',()=> { var isSelected = $("input[value='Bicycle']").isSelected(); //RadioButton is in Selected State console.log(isSelected); }); it('should give us false if isSelected() has been applied on not selected Radio button element', ()=> { var isSelected = $("input[value='Bike']").isSelected();//RadioButton is not in Selected State console.log(isSelected); }); it('should give us false if isSelected() has been applied on not selected Radio button element', ()=> { var isSelected = $("input[value='Laptop']").isSelected();//CheckBox is not in Selected State console.log(isSelected); }); it('should give us true if isSelected() has been applied on selected Radio button element', ()=> { var isSelected = $("input[value='Book']").isSelected();//CheckBox is in Selected State console.log(isSelected); }); });
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) Git Bash Terminal will look like this as below:-
8) Start Selenium Standalone server using the command as given below:-
./node_modules./bin./selenium-standalone start

9) Open Another Terminal like we did in step 6 and run below command.
./node_modules./bin/wdio wdio.conf.js

Observe that the ‘isSelected_command_test.js’ will get executed successfully. The chrome Browser will launch, then it will look out the element based on the XPath 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 the help of isSelected() command of WebDriverIO to check whether the checkbox or radio element present on the page is already selected or not. I will explain other commands to perform different operations on WebPages 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 ?
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>>