Test Automation some useful selenium hacks

Test Automation

Selenium Hacks- In this post I will be sharing some quick tips on selenium,Below are some of the code snippets that I mostly used when I am creating java test cases using selenium webdriver. I hope the same will help you save some time in your test script creation.

These tricky hacks are useful and make automation scripting effective, Tricks mentioned can be further optimized/updtaed as per requirement.

1-Capturing full page screenshot, with vertical scrollbar-

Using shutterbug jar full page screen capture can be done,It is capable of handling vertical, horizontal scrollbar effectively and captures a full screenshot.

String timeStamp = new SimpleDateFormat("HH_mm_ss").format(new Date());
String screenshot_timestamp = nullĀ  ;
screen_timestamp = screenshotname+"_"+timeStamp;
Shutterbug.shootPage(driver,ScrollStrategy.VERTICALLY).withName(screen_timestamp).save("Locl drive location");

2-Linking screenshot in TestNG reports-

To link captured a screenshot to testNg report below line of code can be called, this code link screenshot with click to open become hyperlink which opens captured screenshot.

Reporter.log("<a href="+"E:\\screenshot\\"+screenshot_timestamp+".png"+">click to open

3-Method to handle Sync Issues in web automation-

This is the best way to handle sync issues, by using a for loop with an upper limit of time

public WebElement Element_Finder(WebDriver driver, By locator, int T) throws InterruptedException
WebElement myElement = null;
for (int i = 1; i < T; i++)
System.out.println("Iteration -->"+ i);
myElement = driver.findElement(locator);
//This method is written in #4
WebElementHighlight(myElement, driver);
catch (Exception ex)
System.out.println("Did not found element with locator"+locator);

4-Highlighting a web object with some colour-

Selenium driver will highlight the web object, we only need to call below method and same will highlight the web object in return

public void WebElementHighlight(WebElement element, WebDriver driver)
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='6px groove green'", element);
// return element;

5-Whether a webelement exist or not-

To identify whether a web element exists or not, below code is an effective way to identify the same.
In below code size of the webobject is being checked, where not equal to zero condition is being checked.


6-Working on frame-

While working on frames, first of all, identify the count of a frame and then use switch to method to switch over respective frame.

int i;

7-How to declare webdriver and use the same in another class-

Best way to declare a web driver in a class and then to get that driver.

Find below sample code

public class Superclass
public WebDriver driver;
public Superclass(){
driver = new FirefoxDriver();
public WebDriver getdriver(){
if (driver == null){
driver = new FirefoxDriver();
return driver;
return driver;

//In other class, call like below syntax

8- Integrating network sniffing with selenium scripts

Performance parameters for a single user can be determined by integrating, we can integrate sniffing tool called hTTPWatch with selenium. Refer this link

IT Automation-Integration of Selenium and Sniffing tool (HttpWatch)

for the same.



No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Triggering SoapUI from selenium

The software industry has adopted Service Oriented Architecture-SOA, which consist of Microservices consist of SOAP and REST. Selenium is an API which helps in Web automation, Selenium does not help in API testing although SoapUI scripts can be invoked from selenium/Java classes. Using below 3 steps SOAPUI project can be …

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …