Check-boxes and radio buttons

A free video tutorial from Dmitry Shyshkin
Become a test automation pro with my courses.
Rating: 4.6 out of 5Instructor rating
9 courses
77,552 students
Check-boxes and radio buttons

Learn more from the full course

Advanced Selenium WebDriver with Java and TestNG

Real-world example of Advanced Selenium WebDriver test automation framework with Java and TestNG | Selenium tutorial

04:41:56 of on-demand video • Updated June 2022

Develop Full-Stack Test Automation Framework from scratch
Implement Data Driven Test Framework
Use Page Object Model in Selenium
Automate almost any test case with Selenium
Use advanced Selenium WebDriver techniques (Taking screenshots, capturing JS console errors, etc)
English [Auto]
Hi guys. Today we are starting part four of our course. In this part we will learn different advanced selenium interactions and in the first video we will be checking checkboxes and selecting radio buttons. Let's take a look at our test case here on the welcome page. Let's click on Link CheckBoxes. This opens new checkboxes page, so we will need to add new page object to our project. Now we need to make sure that we can select two checkboxes at the same time. Here in Eclipse, I created new test class checkboxes tests that extends test utilities, and for this class I created new package checkboxes, page tests. And as I said in first part, this project at the end will look like real life project. So we are creating it like one with all the classes and packages. Also, I already started new test selecting two checkboxes test and it starts with opening. Welcome page. When developing new tests. I like to comment all steps of test case first so we have a plan when adding code and this code is already has some comments. So next step is to click on CheckBoxes link. So let's add click checkboxes link method to welcome page object and create checkboxes Page object. Here is the new CheckBoxes page object. So far it doesn't have any methods. Just one checkbox locator. We have two checkboxes on the page, but one locator will be enough for this test here in Welcome page object I have new method click checkboxes link and new locator checkboxes link locator. So basically click on checkboxes Link locator will return new checkboxes page. And here in the test we have this new line of code. Click the link and open new page. Next step in our test is to select all checkboxes. So that's why inside of CheckBoxes page I added new method select all checkboxes. So first step in this method is to get all checkboxes using our checkbox locator. And that's why I created find all method inside of base page object. So right after our web element find method, we have this protected list web element find all. So basically our find element will return only one element with given locator. And if there are more than one elements on the page with this locator, this method will will return just first one and our find all method will return list of web elements using the same locator. So here in the checkboxes page object we are getting list of web elements with this locator. So our locator. Uh, here it is. Input add type equals checkbox will return to web elements to checkboxes that we have on the page. And now we're just going to iterate over all elements inside of this list. So for first checkbox in the CheckBoxes list, if checkbox is not selected and we're using this Webdriver method which will return boolean true or false? So this method we can use on checkboxes and radio buttons. And this exclamation point here means not. So we check in if checkbox selected and then not. So if checkbox is not selected, then we're using just simple click command on the checkbox to select it. And as you remember, we have separate click method like this in our base page object. But this method uses by object as the locator and our checkbox is the web element. So on the web element we can click using this code. So we don't need to use our click method from the base page object. We can just use generic click method from the selenium. So if checkbox is not selected, click If it's already selected, then just skip and do nothing. So this method will select all checkboxes. So first it will find all elements that are checkboxes and then for each element that is not checked, we will click on it to check that element. And then we are doing verification that all checkboxes are checked. And for that we have also new method inside our checkboxes page. Uh, so same we get in the list of all checkboxes, and here we also going to iterate through all web elements inside of this list. And if it's, if at least one is not selected. So we're using the same method is selected. So if it's not selected after we executed all this select all checkboxes method. This method will return false. So basically since our method named are all checkboxes checked, if at least one is not checked, this method will return false and our verification will fail because we're trying to make sure that all checkboxes are checked. So if not, our test will print. Not all checkboxes are checked message. So now let's create test suite and run this test. So here is the new test suite. To run this test, I also created new folder, separate test suites. And in this folder we will keep all separate test suite and separately I created full test suite and smoke test suite. So in the smoke test suite we will have only most important test. So I put just positive login test here in the smoke test suite. In the full test suite we will have all tests that we have. So, so far we have positive log in, test two negative tests and one checkboxes test. So let's run checkbox test suite, right click run as test suite. Let's expand console. And let's bring new created browser window to this monitor. Uh, total test run one failures zero. And as I like to say, never trust the test. That never failed. So let's break this test manually while running in the debug mode and see if test will actually catch the error. So let's go to our checkboxes test. And before let's put a break point here on the verification. And now let's run this test in the debug mode. So right click debug as test and suite. So test is starting again. Here is our browser window. I'll. Let me. Make it back in window mode. Okay, so confirm perspective. Switch? Yes. Remember my decision and switch. So our our eclipse will switch to debug perspective. And now as you see, we are stopped here at assert true method. Now let's open our browser back and uncheck. Let's uncheck second checkbox. So now first checkbox is checked, but second is not. Let's go back to our. Code and continue running. Our test. So resume. To open our test results. We see not all checkboxes are checked. So test is working. It passes when everything is good and it fails when not all checkboxes are checked. So that's expected. So in this test, we worked only with checkboxes, but our lecture 22 title is called CheckBoxes and Radio Buttons. There is no example test case with radio buttons in our app under test, but selenium commands are the same for checkboxes and radio buttons. Click to select and we also can use is select method to verify if it selected. So there is no need for me to create separate test to show it to you. But if you want you can go to this page and here's the URL and on this page there are a lot of different buttons, links, fields, whatever you want, and here are some radio buttons and checkboxes so you can try and automate selecting this male or female or other radio buttons the same way as we did with checkboxes. So basically the only difference is that you can select few checkboxes at the time, but with Radiobuttons, if one is selected, the other must be unselected. So you can create new test case to make sure if you select female then male is unselected or something like that just to practice.