Selenium has emerged as the numero uno among the test automation tools and hence the demand for selenium experts too has increased. Various Selenium tutorials also have come up to help professionals to learn selenium and to take up a testing career in the software industry.
Selenium Scripting and Troubleshoot Scenarios Guide
In this part of Selenium tutorial, we are talking about efficient selenium scripting and troubleshooting scenarios. Here we are going to discuss about some ideas that would help to troubleshoot some recurrent problems which may not come on a daily basis but would be helpful in future.
There are certain action which are an integral part of the test scripts while automating a test scenario such as
- Clicking a hyperlink, button etc
- Typing in a text box
- Scrolling horizontally and vertically to bring an desired object into view
Another unusual problem we may face is about accessing multiple elements in a list. At some times, there will be multiple elements of same types such as multiple images or multiple hyperlinks in a ordered or unordered list. Such elements should be dealt with a single piece of code and WebElement List will come for help here. For example refer the image given below.
Image source: http://www.softwaretestinghelp.com/efficient-selenium-scripting-selenium-tutorial-27/
Here is an unordered list of various service providers. Verification of click ability and visibility can be checked here with a single piece of code using a list of elements. Following syntax can be used to import the package for using WebElement List in the test scripts.
Import java. util.List;
// Storing the list
List <WebElement> serviceProviderLinks = driver.findElements(By.xpath(“//div[@id=’ServiceProvider’]//ul//li”));
// Fetching the size of the list
int listSize = serviceProviderLinks.size();
for (int i=0; i<listSize; i++)
// Clicking on each service provider link
// Navigating back to the previous page that stores link to service providers
Handling keyboard and mouse events is another critical area where we may come across several issues. There are occasion where we may not be able to deal with alerts and popups etc, with Selenium commands. In such conditions, we will have to depend on some other Java utilities and Robot class is one such Java utility which can be used for performing keyboard events and mouse events.
Handling Keyboard Events
Imagine a situation where an unwanted popup has appeared on the screen and the same cannot be accepted or dismissed using alert interface. Now there is only one intelligent option is left out and that is to close the window shortcut keys, “Alt+Spacebar C”. Now let us look into the process to do this with the help of Robot Class.
Before initiating the action, we should import the necessary packages that supports Robot Class with in the test scripts and the same can be done using the following syntax.
// Instantiating Robot class
Robot rb =new Robot();
// Calling KeyPress event
// Calling KeyRelease event
Handling Mouse Events
Robot class can handle mouse events also but in this part of the article we are looking at the ability of Selenium to handle mouse events.
Selenium webdriver has a vast range of interactive utilities that can be used for the automation of keyboard and mouse. Action Interface is one among many such activities which stimulates the single user interactions.
Import the packages and use the following syntax for it.
// Instantiating Action Interface
Actions actions=<strong>new</strong> Actions(driver);
// howering on the dropdown
actions.moveToElement(driver.findElement(By.<em>id</em>(“id of the dropdown”))).perform();
// Clicking on one of the items in the list options
WebElement subLinkOption=driver.findElement(By.id(“id of the sub link”));