A Word to the Wise for Selenium WebDriver Testers

DZone 's Guide to

A Word to the Wise for Selenium WebDriver Testers

These techniques will help make you a better tester when using the Selenium WebDriver framework.

· DevOps Zone ·
Free Resource

Software testing has drifted towards automation. Testers no longer like to execute manual testing processes to accomplish testing tasks. Automated software testing is touted as one of the best inventions in the software testing world. Through automated testing methods, testers get the power to perform better functions and accomplish tasks more efficiently. When we talk about software testing, one of the most popular and highly-used testing software that comes to mind is Selenium. Selenium has played a major role in enhancing the overall quality of the software development lifecycle.

A Brief About Selenium WebDriver

A collection of topnotch open-source APIs, Selenium WebDriver is used to automate the testing of any web application. Selenium WebDriver tool supports different browsers like Internet Explorer, Firefox, Chrome, and Safari. Through Selenium WebDriver, testers can ensure the perfect working of a web application (You can get more of an idea about it by watching a video on the Download and Install process of Selenium WebDriver by Software Testing Material channel on YouTube).

Improving Your Performance With Selenium

Selenium is an open-source tool, but it is highly efficient. And, the product is evolved to match the needs of the present day web testers. A myriad of practical tips and tricks are to be adopted and used to enhance the quality of automation tests with WebDriver. Plus, a plenty of fantastic valuable topics like automation strategies, benchmarks and researches are to be studied thoroughly to improve the performance.

Various Selenium strategies can be utilized to enhance the quality, optimize code as well as to offer robust automation solutions. The best part about Selenium is that it supports a plenty of programming languages like Java, Python, etc.

Listed below are some top tips to boost the testing quality:

Taking Screenshots of Failures or Issues

Taking a screenshot of the web pages helps to identify the issue and debug test case.

Here is the code snippet to take the screenshot:

File scrFile ;
String pathofscreenshot
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(pathofscreenshot + "\\screenshot.png"));

Verify if an Element Exists

Here is the code snippet to verify the element:

Use <findElements> instead of <findElement>.

<findElements> will throw back an empty list if it doesn’t the matching elements.

The code mentioned below can be used to verify the presence of an element.

Boolean isItemPresent = driver.findElements(By.testLocator).size() > 0

Drag & Drop Comes in Handy in Some Cases

DragAndDropToOffset is the special actions of Selenium WebDriver which is used to perform complex UI interactions. To accomplish this, you would have to set the desired X and Y offsets.

Here is the code snippet to drag and drop:

public void DragAndDrop()
    IWebElement element = driver.FindElement(By.XPath("//*[@id='project']/p[1]/div/div[2]"));
    Actions move = new Actions(driver);
    move.DragAndDropToOffset(element, 30, 0).Perform();

Method to Execute JavaScript (.js)

Custom synchronizations can be performed using the javascript. In order to accomplish this, the tester needs to modify a few values, hide/show elements or enable/disable element

Here is the code snippet to run JavaScript:

JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("alert('hello world');");

Fetch the HTML Source of WebElement

Here is the code snippet to get the HTML source of WebElement:

Webdriver’s <WebElement> class offers <getAttribute()> technique to get the HTML in the element.

The tester will have to pass the <innerHTML> as attribute sort in the method listed Here’s the code:

String html = element.getAttribute("innerHTML");

Scroll the Webpage

Javascriptexecutor enables the tester to scroll the webpages in Selenium WebDriver.

Following is the code snippet to scroll the web pages.

Code to vertically scroll the webpage by 250 pixels:


Code to horizontally scroll the webpage by 250 pixels:


This will scroll webpage horizontally by 250 pixels.

Generic Tips to Enhance the Quality of Testing

  • Create test cases before starting to automate.

It is suggested to develop test cases before commencing the Selenium testing process. This prevents the tester from executing ad-hoc tests. Test cases should include the aim of the test, the needs, the anticipated and possible outcomes as well as the failure conditions.

  • Determine the right tests to automate.

The initial step to kick-start your testing day is by identifying the right tests to perform. Plus, it is also suggested to jot down the automation strategies before deciding on the tests to automate.


The tips and tricks listed above are some codes which can be regularly used by the testers to enhance their testing practice. These simple tricks can enrich the stability and boost the speed of selenium testing process.

Most of the codes listed above can be used in real time situations. Plus, Selenium testers should try to keep abreast with the latest evolvements to ensure that they perform their level best!

selenium ,selenium webdriver ,performance ,tutorial ,performance testing ,testing ,test automation ,web testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}