Useful Functions for Automation Framework using Selenium Web Driver

28 / May / 2015 by Sumit Gambhir 1 comments

While doing automation using selenium, there are certain repetetive tasks that we need to perform in order to handle page elements. So, the aim of this blog is to highlight some common operations that we need to handle every now and then.

Some operations are:
1. Open URL in different browsers (Chrome, Firefox, IE)
2. Do mouse-hovers
3. Taking snapshots for analysing  the failures
4. Getting current time-stamp
5. Handling a drop-down

1. Open URL in different browsers (Chrome, Firefox, IE)

Do the following steps first, to use this method:
1. Download ‘chromedriver.exe’ and ‘IEDriverServer.exe’ from
2. Then save the downloaded file in ‘Drivers’ folder at path where ‘src’ folder exists

call ‘OpenApp’ function with passing two parameter: ‘BrowserName(ie. CH/FF/IE) & URL’
e.g. OpenApp(“CH”,””) it will open this url in ChromeBrowser

public static WebDriver OpenApp(String BrowserName, String url){
return driver;
public static void fn_OpenURL(String url){

public static WebDriver fn_LaunchBrowser(String browsername){
System.setProperty("", "Drivers\\chromedriver.exe");
driver= new ChromeDriver();
}else if(browsername=="FF"){
driver= new FirefoxDriver();
}else if(browsername=="IE"){
System.setProperty("", "Drivers\\IEDriverServer.exe");
driver= new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
return driver;


2. Do mouse-hovers

Incase, we need to click on a sub-menu that is visible only when users do mouse-hover on the main-menu, then we can do it using this function. Just pass web element position to this function.
e.g MouseOver(driver.findElement(“Main-Menu”)))

public static void MouseOver(WebElement we){
Actions actObj=new Actions(driver);


3. Take Snapshot for analysing the failures

Scenario – As an automation tester, we must need to take snapshots of error & exceptions while running the script as a proof, so we can use this method.
e.g. fn_TakeSnapshot(driver, “where you want to save the SnapShot”)

public static String fn_TakeSnapshot(WebDriver driver, String DestFilePath) throws IOException{
String TS=fn_GetTimeStamp();
TakesScreenshot tss=(TakesScreenshot) driver;
File srcfileObj= tss.getScreenshotAs(OutputType.FILE);
File DestFileObj=new File(DestFilePath);
FileUtils.copyFile(srcfileObj, DestFileObj);
return DestFilePath;


4. Get current Time-stamp

Scenario – While reporting your script status, you need to pass time & date that when your test-script has finished. Another scenario: If you need unique username every time, you can append the timestamp with any constant string (e.g SumitMay28201513_15_10 PM), so here you are:

public static String fn_GetTimeStamp(){
DateFormat DF=DateFormat.getDateTimeInstance();
Date dte=new Date();
String DateValue=DF.format(dte);
DateValue=DateValue.replaceAll(":", "_");
DateValue=DateValue.replaceAll(",", "");
return DateValue;


5. Handle drop-down (select an option from list)

Scenario: If you need to select an option from any drop-down, you can use following methods
NOTE: ‘Select’ class in selenium works only those drop-downs, which lies in <select > tag, like in the following html.

<div><select id="SelectID_Three"><option value="selectValue">Select</option>
<option value="firstcolour">Yellow</option>
<option value="secondcolour">Lime</option>
<option value="thirdcolour">Red</option>

The following code will help you solve the problem:

//select the dropdown using "select by visible text", so pass VisibleText as 'Yellow' to funtion
public static void fn_Select(WebElement WE, String VisibleText){
Select selObj=new Select(WE);

//select the dropdown using "select by index", so pass IndexValue as '2'
public static void fn_Select(WebElement WE, int IndexValue){
Select selObj=new Select(WE);

//select the dropdown using "select by value", so pass Value as 'thirdcolor'
public static void fn_Select(WebElement WE, String Value){
Select selObj=new Select(WE);

Hope it will ease your tasks, Happy Learning !! :)


comments (1 “Useful Functions for Automation Framework using Selenium Web Driver”)

  1. Jesmy

    Could you help me to know How to connect Page Factory, what all kind of functions should come in each class, How to write main class, and how to connect to Suite , all in formal way using Java and selenium webDriver and TestNG?


Leave a comment -