In our former guide, we delved into the various kinds of assert statements in the Java-dedicated unit testing framework, along with their practical applications through examples. It’s crucial to recognize that in the role of an Automation Test Engineer, assertions serve as fundamental aspects in creating test scripts.
Taking the Selenium tutorials further, the subsequent guides will concentrate on operating different sorts of web elements on websites. Specifically, in this tutorial, we’ll discuss dropdowns and their management techniques.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Ahead of progressing to the problem statement and its resolution, let’s briefly introduce and comprehend the application being tested. For this, we’ve constructed a mock HTML page encompassing a variety of web elements.
The basic elements on the webpage include:
- Hyperlink
- Button
- Dropdown
Consult the webpage mentioned above:
What You’ll Learn:
Understanding the Application under Test
The webpage has been crafted to include several basic kinds of web elements.
- Hyperlink: Provided are two hyperlinks dubbed “Google” and “abodeQA” which divert the user to “https://www.google.co.in/” and “http://www.abodeqa.com/” respectively upon selection.
- Dropdown: Three dropdowns have been established for selecting colors, fruits, and animals, with a default value already inputted.
- Button: A “try it” button was crafted to reveal a pop-up box with OK and Cancel options upon selection.
Below is the HTML code utilized to create the above-mentioned webpage:
<!DOCTYPE html> <html> <head><title> Assessment of Select Class </title> <body> <div id="header"> <ul id="linkTabs"> <li> <a href="https://www.google.co.in/">Google</a> </li> <li> <a href="http://abodeqa.wordpress.com/">abodeQA</a> </li> </ul> </div> <div class="header_spacer"></div> <div id="container"> <div id="content" style="padding-left: 185px;"> <table id="selectTable"> <tbody> <tr> <td> <div> <select id="SelectID_One"> <option value="redvalue">Red</option> <option value="greenvalue">Green</option> <option value="yellowvalue">Yellow</option> <option value="greyvalue">Grey</option> </select> </div> </td> <td> <div> <select id="SelectID_Two"> <option value="applevalue">Apple</option> <option value="orangevalue">Orange</option> <option value="mangovalue">Mango</option> <option value="limevalue">Lime</option> </select> </div> </td> <td> <div> <select id="SelectID_Three"> <option value="selectValue">Select</option> <option value="elephantvalue">Elephant</option> <option value="mousevalue">Mouse</option> <option value="dogvalue">Dog</option> </select> </div> </td> </tr> <tr> <td> <!DOCTYPE html> <html> <body> <p>Select the button to display a confirmation box.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { confirm("Press a button!"); } </script> </body> </html> </td> </tr> </tbody> </table> </div> </div> </body> </html>
Automating the Scenario
- Launch the web browser and navigate to the webpage
- Click on the hyperlink labeled “Google”
- Return to the initial webpage
- Choose “Green” in the color dropdown
- Choose “Orange” in the fruit dropdown
- Select “Elephant” in the animal dropdown
Incorporating Selenium Select Class with WebDriver Code
Bear in mind that in the creation of scripts, we’ll be leveraging the “Learning_Selenium” project developed in the previous tutorial.
Step 1: Under the “Learning_Selenium” project, form a new java class named “HandlingDropDown”.
Step 2: Into the “HandlingDropDown.java” class, copy and paste the code that follows.
The subsequent test script relates to the aforementioned scenario:
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class HandlingDropDown { WebDriver driver; // Preparing the browser settings and launching the application @Before public void setUp() { driver=new FirefoxDriver(); driver.get("file:///F:/Work/Blogs/testingstuff/DemoWebAlert.html"); driver.manage().window().maximize(); } // Test to ascertain the selection of dropdown values @Test public void testSelectFunctionality() throws InterruptedException { driver.findElement(By.linkText("Google")).click(); driver.navigate().back(); Thread.sleep(5000); Select selectByValue = new Select(driver.findElement(By.id("SelectID_One"))); selectByValue.selectByValue("greenvalue"); Thread.sleep(5000); Select selectByVisibleText = new Select (driver.findElement(By.id("SelectID_Two"))); selectByVisibleText.selectByVisibleText("Lime"); Thread.sleep(5000); Select selectByIndex = new Select(driver.findElement(By.id("SelectID_Three"))); selectByIndex.selectByIndex(2); Thread.sleep(5000); } // Dismantle the setup once the test is complete @After public void tearDown() { driver.quit(); } }
Guide to the Code
Import Statements
- import org.openqa.selenium.support.ui.Select – This package import is necessary in order to fashion a WebDriver script that handles a dropdown element employing the Select class.
Object Construction for Select class
Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
We design an instance of the Select class and instantiate it via the Select class and the identifier specific to the dropdown.
The locator value or identifier for the dropdown element may be located leveraging methods explored in preceding tutorials such as Selenium IDE and Firebug.
Assigning the value in the dropdown with the method selectByValue()
selectByValue.selectByValue(“greenvalue”);
In the aforementioned Java command, we designate the value “green” in the dropdown with the selectByValue() method, which has the value attribute as a parameter.
Assigning the value in the dropdown with the method selectByVisibleText()
selectByValue.selectByVisibleText(“Lime”);
In the aforementioned Java command, we employ the selectByVisibleText() method to select the value “Lime” in the dropdown. The visible text is provided as the parameter.
Assigning the value in the dropdown with the selectByIndex() method
selectByValue.selectByIndex(2);
In the aforementioned Java command, we select the third value in the dropdown using the selectByIndex() method with the index value passed as the parameter.
Note that the index value beginnings from “0”.
Conclusion
With this tutorial, we’ve introduced the Select class of WebDriver for handling dropdown elements on websites and reviewed the methods used to fill in dropdown values.
A recap of the guide:
- The Select class of WebDriver is leveraged to manage dropdown elements on a webpage.
- Before writing scripts, a package to make the Select class accessible needs to be imported.
- import org.openqa.selenium.support.ui.Select;
- We design an instance of the Select class and instantiate it utilizing the Select class and the identifier for the dropdown.
- Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
- The locator value or identifier for the dropdown can be pinpointed utilizing Selenium IDE and Firebug.
- We have three methods to pick a value from a dropdown:
- selectByValue()
- selectByVisibleText()
- selectByIndex()
- The Java command below is used to set the color “green” in the dropdown by utilizing selectByValue():
- selectByValue(“greenvalue”);
- The Java command below is used to set the fruit “Lime” in the dropdown by utilizing selectByVisibleText():
- selectByVisibleText(“Lime”);
- The Java command below is used to set the third value in the dropdown by utilizing selectByIndex():
- selectByIndex(2);
Next Tutorial #14: In our subsequent guide, we’ll be exploring diverse WebDriver commands such as isSelected(), isEnabled(), and isDisplayed(), which return a Boolean value indicating whether or not a particular web element is present.
Stay tuned as we continue to automate dropdowns using the WebDriver utility – “Select class”.