Useful Functions for Automation Framework using Selenium Web Driver

28 / May / 2015 by Sumit Gambhir 2 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
“http://www.seleniumhq.org/download/”
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”,”http://www.seleniumhq.org/”) it will open this url in ChromeBrowser

[java]
public static WebDriver OpenApp(String BrowserName, String url){
fn_LaunchBrowser(BrowserName);
fn_OpenURL(url);
return driver;
}
public static void fn_OpenURL(String url){
driver.get(url);
driver.manage().window().maximize();
}

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

[/java]

 

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(By.name(“Main-Menu”)))

[java]
public static void MouseOver(WebElement we){
Actions actObj=new Actions(driver);
actObj.moveToElement(we).build().perform();
}
[/java]

 

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”)

[java]
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);
DestFilePath=DestFilePath+TS+".png";
File DestFileObj=new File(DestFilePath);
FileUtils.copyFile(srcfileObj, DestFileObj);
return DestFilePath;
}
[/java]

 

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:

[java]
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;
}
[/java]

 

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.

[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>
</select></div>
[/html]

The following code will help you solve the problem:

[java]
//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);
selObj.selectByVisibleText(VisibleText);
}

//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);
selObj.selectByIndex(IndexValue);
}

//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);
selObj.selectByValue(Value);
}
[/java]

Hope it will ease your tasks, Happy Learning !! 🙂

FOUND THIS USEFUL? SHARE IT

comments (2)

  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?

    Reply

Leave a Reply to srinivasulu Cancel reply

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