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 *

Test Automation
3 Easy steps for Running selenium scripts in Sauce Labs

Hello Readers, Its been a long time publishing any post, I have done a small execution of selenium script in a remotely placed device on saucelabs. Below piece of code will help you in doing the same. There are 3 simple steps to connect and run your automation script in …

automation testing
Test Automation
Test Automation-Easy steps to create test-output folder Backup in TestNG automatically

Overview– TestNG is widely used and most preferred in test automation, It contains several features that are beneficial for Unit testing, regression, and Integration testing. In this article we will see out report generated by Test-NG can be copied in every run. Test-NG generate its execution report in test-output folder, …

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

HTTP Watch and selenium integration- Process automation reduces the time and increases accuracy as well gives ample time to invest energy in other areas of the project. In this blog post, I will be talking about IT process automation, I will be doing an integration of sniffing tool and test …