Thursday, 13 September 2012

How do you make Selenium wait for the page to load in Selenium 2.0?

// Sleep until the div we want is visible or 5 seconds is over

long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
// Browsers which render content (such as Firefox and IE) return "RenderedWebElements"
RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className("gac_m"));

// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {



driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


How to Take a screenshot using Selenium WebDriver?

WebDriver driver = new FirefoxDriver();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));


How to stop the selenium server until a popup opens? 

selenium.WaitForPopUp("id=popup_container", "30000");


How to fetch data from table using Selenium WebDriver ?

<table> <tbody> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr> </tbody> </table>

// Grab the table 

WebElement table = driver.findElement(; 

 // Now get all the TR elements from the table

 List<WebElement> allRows = table.findElements(By.tagName("tr"));

 // And iterate over them, getting the cells 

for (WebElement row : allRows) { 

 List<WebElement> cells = row.findElements(By.tagName("td")); 

 for (WebElement cell : cells) { 

 //  so on 




// List<WebElement> cells = row.findElements(By.xpath("./*"));


Q) how to find out a text is available in current webpage using WebDriver ?

import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.*; 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;

public class SearchingText {

    static WebDriver driver = new FirefoxDriver();
    static Selenium selenium = new WebDriverBackedSelenium(driver, "http://");

    public static  void find(){

    boolean b =driver.getPageSource().contains("Telugu");


      public static void main(String[] args) {



How to automate onblur through selenium RC 

How to automate onblur through selenium RC .
How to automate lost focus through selenium.

I faced the problem while automating the form submit. All of the form fields are having some action on their lost focus.
The onblur event occurs when an object loses focus.
For example : I have two fields First Name and Last Name. When i enter first name and press tab then it loses its focus and onblur function gets called.
and its calls upperCase and it changes the first name in Upper case.
the same case with Last Name. When i enter last name and press tab it calls blur function lowerCase and changes the letters in lower case.

But the problem in automation is i cant automate lost focus. when we type in first name and last name it simply types in first name and last name text box.
It does not call onblur function upper case and lower case and does not change the letter respectively.

so, fireEvent is a special command in selenium which helps in automating onblur function.

this is html of the element and blur javascript functions 

<script type="text/javascript">
function upperCase()
var x=document.getElementById("fname").value;
function lowerCase()
var x=document.getElementById("lname").value;
Enter your First Name: <input type="text" id="fname" onblur="upperCase()" />
<br />
Enter your Last Nast: <input type="text" id="lname" onblur="lowerCase()" />


 selenium.type("fname", "ramesh");
 selenium.fireEvent("fname", "blur");
 selenium.type("lname", "reddy");
 selenium.fireEvent("lname", "blur");

How this works is :

First this will type "ramesh" in First Name and then selenium.fireEvent("fname", "blur"); this will call the onblur fuction "upperCase()" which changes the First Name in upper case "ramesh".
then it types in Last Name and then selenium.fireEvent("lname", "blur"); which means it will press tab and lost the function and on the lost focus it calls
blur function lowerCase which changes the Last Name in lower case.


Running test case in multiple browsers parallely in WebDriver

 we can run test script in different browsers parallely using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox and chrome parallely.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelRunning {

 private  WebDriver driver=null;
 @Parameters ({"BROWSER"})
 public void setup(String BROWSER){
  System.out.println("Browser: " + BROWSER);

  if (BROWSER.equals("FF")) {
   System.out.println("FF is selected");
   driver = new FirefoxDriver();
  } else if (BROWSER.equals("IE")) {
   System.out.println("IE is selected");
   driver = new InternetExplorerDriver();
  } else if (BROWSER.equals("HU")) {
   System.out.println("HU is selected");
   driver = new HtmlUnitDriver();
  } else if (BROWSER.equals("CH")){
   System.out.println("Google chrome is selected");
   driver = new ChromeDriver();

 public  void testParallel()throws Exception{

  WebElement search = driver.findElement("q"));
  search.sendKeys("automation  by ramesh");
  Assert.assertTrue(driver.getPageSource().contains("working as Associate Manager @freelancher"));

In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.


<?xml version="1.0" encoding="UTF-8"?>
<suite name="webDriver" parallel="tests">
  <test name="WebDriverDemo Witn FF" preserve-order="true">
  <parameter name="BROWSER" value="FF" />
   <class name="com.freelancher.ParallelRunning" />
  <test name="WebDriverDemo with IE" preserve-order="ture">
  <parameter name="BROWSER" value="IE"></parameter>
   <class name="com.freelancher.ParallelRunning"></class>

 <test name="WebDriverDemo with HTML unit" preserve-order="true">
  <parameter name="BROWSER" value="HU"></parameter>
   <class name="com.freelancher.ParallelRunning"></class>

  <test name="WebDriverDemo with HTML unit" preserve-order="true">
  <parameter name="BROWSER" value="CH"></parameter>
   <class name="com.freelancher.ParallelRunning"></class>



How to verify the text ignoring cases sensitivity in selenium ?

Some times we need to verify the text on the webpage no matter is upper cases or lower cases or proper.
We just need to verify the text on the web page ignoring their cases sensitivity.
REGEXPI is used to avoid cases sensitive to verify the text on the page.

Lets say i want to verify the text "seleniumresource is good blog for selenium" No matter its in upper cases or lower case or proper case.

Then use REGEXPI in selenium command selenium.isTextPresent or verifyTextPres

verifyTrue(selenium.isTextPresent("regexpi:SELENIUMRESOURCE IS GOOD BLOG FOR SELENIUM"));


SELENIUMRESOURCE is good blog for selenium"));

verifyTrue(selenium.isTextPresent("regexpi:SELENIUMRESOURCE Is GoOd Blog for SELENIUM"));

verifyTrue(selenium.isTextPresent("regexpi:SELENIUMRESOURCE is good blog for selenium"));





1 comment: