Datadriven testing using selenium webdriver


Datadriven or Parameterization in Selenium Webdriver


package selenium_examples;


import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {
           
                 WebDriver driver = new FirefoxDriver();

public void search() throws BiffException, IOException, InterruptedException
{


driver.get("http://www.google.com");
FileInputStream fi=new FileInputStream("F:\\Sel.xls");
Workbook WB = Workbook.getWorkbook(fi);


Sheet sheet = WB.getSheet("Sheet1");
System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());

for (int i=0;i {

String s=sheet.getCell(0,i).getContents();
System.out.println(s);
driver.findElement(By.id("gbqfq")).sendKeys(s);
driver.findElement(By.id("gbqfb")).click();
Thread.sleep(3000);
driver.findElement(By.id("gbqfq")).clear();
}
fi.close();
}

public static void main(String[] args) throws BiffException, IOException, InterruptedException
{
Sample s1=new Sample();
s1.search();

}

}

Output:-
Number of Rows in Excel Sheet =4
Selenium
Selenium Ide
Selenium RC
Selenium Webdriver


The above script gets values from excel sheet and uses in google search.


Important things to note in this script is Excel Sheet format should be in .Xls.If we use .xlsx format excel sheet,we get error : "Exception in thread "main" jxl.read.biff.BiffException: Unable to recognize OLE stream".
So save the excel sheet as "Excel 97-2003 Workbook".


 

Selenium Webdriver Scripts : Working with Calendar using Selenium webdriver

Selenium webdriver script to select current month date and next month date through calendar.

package selenium_examples;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://hotels.spicejet.com/index.html");

driver.findElement(By.xpath("//html/body/div[3]/div/table/tbody/tr/td[1]/div[1]/form[1]/fieldset/div[2]/div[2]/div[1]/a/img")).click();
driver.findElement(By.linkText("8")).click();
driver.findElement(By.xpath("//html/body/div[3]/div/table/tbody/tr/td[1]/div[1]/form[1]/fieldset/div[2]/div[2]/div[2]/a/img")).click();
driver.findElement(By.xpath("//html/body/div[5]/div[2]/a[2]/span")).click();
driver.findElement(By.linkText("12")).click();

}

  }


Above scripts selects ‘Check in date’  as 8th of the current month i.e. 8/11/2013   and ‘Check out date’ as 12th of the next month i.e. 12/12/2013 from calendar.

Working with calendar - selenium webdriver

 



 

Data Driven Testing for Login Functionality using Selenium Webdriver


Below is the code for Mercury Tours Login functionality using Selenium WebDriver.
We are Providing Login Details from Excel Sheet.



package selenium_examples;


import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

WebDriver driver = new FirefoxDriver();

public void login() throws BiffException, IOException, InterruptedException
{


FileInputStream fi=new FileInputStream("F:\\Sel1.xls");
Workbook WB = Workbook.getWorkbook(fi);


Sheet sheet = WB.getSheet("Sheet1");
System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());

for (int i=1;i {

String s=sheet.getCell(0,i).getContents();
String t=sheet.getCell(1,i).getContents();
System.out.println("Username=" +s);
System.out.println("Password= " +t);
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys(s);
driver.findElement(By.name("password")).sendKeys(t);
driver.findElement(By.name("login")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("SIGN-OFF")).click();
}
fi.close();
}

public static void main(String[] args) throws BiffException, IOException, InterruptedException
{
Sample s1=new Sample();
s1.login();

}

}


Output:-

Number of Rows in Excel Sheet =4
Username=mercury
Password= mercury
Username=mercury1
Password= mercury1
Username=mercury2
Password= mercury2



 

How to get auto populated Search results using selenium webdriver

How to get auto populated Google search results?

package selenium_examples;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Testing");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
WebElement table = driver.findElement(By.className("gssb_m"));    
List rows = table.findElements(By.tagName("span")); 
System.out.println("Total no. of rows: "+ rows.size());
Iterator i = rows.iterator();  
while(i.hasNext()) {
 WebElement row = i.next();
 System.out.println(row.getText());
}

     
}

       }


Output:-

Total no. of rows: 4
testing
testing tools
testingken
testing interview questions

webdriver autocomplete script

 




 

How to upload files using selenium webdriver


package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.name("file_0"));
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("E:\\index.jpg");

 
}
}


 
upload file in selenium webdriver


How to scroll down the page through Selenium Web Driver

Page Scroll down script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 300)");

 

}
}


Page Scroll up script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(300, 0)");
 

}
}



 



 

How to right click on a link and open link in new tab using selenium webdriver

Script1:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 
The above script clicks on 'About' link in google page and opens 'About google' page in new tab.

right click on link in webdriver


contextClick()-Performs a context-click at the current mouse location.

contextClick(WebElement onElement)-Performs a context-click at middle of the given element

Script2:-

Below script clicks on 'About' link in google page and opens 'About google' page in new window.

package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 

How to highlight an element using selenium webdriver

Script to highlight element border

Script1:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("http://www.seleniumhq.org/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("q"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid blue'", element);
       }
}

Above script highlights search textbox border in www.seleniumhq.org website.

highlight element in selenium webdriver



Script2:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("https://www.google.co.in/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("btnK"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid red'", element);
       }
}



 


How to get x and y coordinates of an element using Selenium Webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println("X Position= "+ driver.findElement(By.name("btnK")).getLocation().getX());
System.out.println("Y Position= "+ driver.findElement(By.name("btnK")).getLocation().getY());

}
}


Output:-

X Position= 373
Y Position= 386


How to get Height and Width of an image or element using Selenium Webdriver


Selenium webdriver script to get Width and Height of an Image

Script1:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println("Height of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getHeight());
System.out.println("Width of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getWidth());

}
}


Output:-

Height of Mercury Tours image= 110
Width of Mercury Tours image= 100


element height and width in webdriver


Script2:-

Script to get Height and Width of google search button

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println("Height= "+ driver.findElement(By.name("btnK")).getSize().getHeight());
System.out.println("Width= "+ driver.findElement(By.name("btnK")).getSize().getWidth());

}
}


Output:-

Height= 29
Width= 102


How to find total number of checkboxes in a page using selenium webdriver

package selenium_examples;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();

       Listcheckboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));
System.out.println("Total Checkboxes ="+checkboxes.size());

}
}




Output :-

Total Checkboxes =46


checkboxes count webdriver selenium


How to extract number of links and text of links of a page using Selenium WebDriver

package selenium_examples;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
   public static void main(String[] args)
   {
   
    
            WebDriver driver = new FirefoxDriver();
    
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    
            driver.get("http://newtours.demoaut.com/");
    
            List linkElements = driver.findElements(By.tagName("a"));
    
            System.out.println("Total number of Links ="+linkElements.size());
     
            for(int i=0; i< linkElements.size(); i++)

            {
            System.out.println(linkElements.get(i).getText());
            }
    
       }       
                             }

Output:-             
Total number of Links =16

Home
Flights
Hotels
Car Rentals
Cruises
Destinations
Vacations
SIGN-ON
REGISTER
SUPPORT
CONTACT
your destination
featured vacation destinations
Register here
Business Travel @ About.com
Salon Travel
 


  
 

Handling Alerts or Popups using Selenium webdriver

package selenium_examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
   public static void main(String[] args)
  
                        {
   
  WebDriver driver = new FirefoxDriver();
                driver.get("http://www.rediff.com");
                driver.findElement(By.linkText("Sign in")).click();
                driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                driver.findElement(By.id("btn_login")).click();
                Alert alt=driver.switchTo().alert();
               System.out.println(alt.getText());
               alt.accept();
              
     
    }
  }

Output:-
Please enter your email ID

Handling alerts or popups in webdriver


Alert alt=driver.switchTo().alert();->Get a handle to the open alert, prompt or confirmation
alt.getText()->Gets the text of alert
alt.accept()->To Accept any alert, i.e Clicking OK button
alt.dismiss()->To dismiss any alert, i.e Clicking Cancel button



  

How to open a new tab using selenium webdriver


package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://www.google.com");

     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
   
     System.out.println("New Tab Opened");
   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"w");
   
     System.out.println("New Tab Closed");

}
  }


Output:-

New Tab Opened
New Tab Closed



Script to go back to first tab i,e google.com page

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://www.google.com");

     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
   
     System.out.println("New Tab Opened");
   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL , Keys.TAB);
   
 
}
  }

How to select multiple options from a list box using selenium webdriver


package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

       droplist.selectByVisibleText("Accounting / Finance");
       droplist.selectByVisibleText("Agriculture / Dairy");
 
}
  }


Above script selects Accounting / Finance and Agriculture / Dairy from Industry list box.

selenium scripts,webdriver scripts



How to deselect selected options from a list box using selenium webdriver

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

       droplist.selectByVisibleText("Accounting / Finance");
       droplist.selectByVisibleText("Agriculture / Dairy");
       droplist.deselectAll();
       System.out.println("Deselected selected options");
}
  }


 deselectAll() method - Clears all selected options.


Webdriver Scripts : How to check whether button is enabled or disabled in Selenium Web driver


package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");

if(driver.findElement(By.id("gbqfba")).isEnabled())
{
System.out.println("Button is Enabled");
}
else
{
System.out.println("Button is Disabled");
}


}
  }


Output:-

Button is Enabled


Selenium webdriver script to check whether textbox is enabled or disabled

package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");

if(driver.findElement(By.id("gbqfq")).isEnabled())
{
System.out.println("Textbox is Enabled");
}
else
{
System.out.println("Textbox is Disabled");
}


}
  }


Output:-

Textbox is Enabled



Selenium Scripts:How to check whether a list box or dropdown box allows multiple selection or not in selenium webdriver


Script 1 :-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

      if (droplist.isMultiple())
      {
      System.out.println("Multiple selection is allowed");
      }
   
      else
      {
      System.out.println("Multiple selection is not allowed");
      }
     
      }
  }


Output:-

Multiple selection is allowed

isMultiple() method returns TRUE if the drop-down element allows multiple selection at a time else returns FALSE.


Script 2:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://book.spicejet.com/");

 Select droplist = new Select(driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1")));

      if (droplist.isMultiple())
      {
      System.out.println("Multiple selection is allowed");
      }
            else
      {
      System.out.println("Multiple selection is not allowed");
      }
     
      }
  }

Output:-

Multiple selection is not allowed


Selenium Scripts : How to resize current browser window in Selenium WebDriver

Resize the browser window in Selenium Webdriver

Script 1:-

package selenium_examples;



import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;




public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://funandknowledge.blogspot.com");
Dimension D = new Dimension(800,500);
driver.manage().window().setSize(D);
      }
  }
Selenium Scripts,Resize Browser


Script 2:-

package selenium_examples;



import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;




public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
Dimension D = new Dimension(500,800);
driver.manage().window().setSize(D);
      }
  }




How to check selected value in dropdown list using selenium webdriver

Script To check selected value in dropdown list using selenium webdriver

package selenium_examples;



import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;




public class spicejet {

public static void main(String[] args) {


 

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
    Select droplist = new Select(driver.findElement(By.name("fromPort")));

        droplist.selectByIndex(3);
        System.out.println(droplist.getFirstSelectedOption().getText());
        System.out.println("Done!");
      }
  }

Output:-

New York
Done!

Selenium webdriver script to check selected value in dropdown list


Selenium Webdriver Scripts - WebDriver.Navigation



package selenium_examples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class spicejet {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.findElement(By.id("gbqfbb")).click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.navigate().refresh();
System.out.println("done!");

}
}

navigate.back() - Performs browser back button action
navigate().forward() - Performs browser forward button action
navigate.refresh() - It refreshes the current page
navigate.refresh() - Navigates to particular URL



Mouse hover action in selenium Webdriver


package selenium_examples;

import java.util.concurrent.TimeUnit;

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.interactions.Actions;

public class spicejet {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://spicejet.com/");

Actions builder = new Actions(driver);
WebElement Element = driver.findElement(By.xpath("html/body/div[2]/div[1]/div/div[2]/div[2]/div/ul/li[4]/a"));
builder.moveToElement(Element).build().perform();

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

driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div/ul/li[4]/ul/li/a")).click();

System.out.println("Done!");

}
}

Above script opens http://spicejet.com/ website and hovers over menu item "Travel Tips" and clicks "FAQ's".

selenium scripts




How to count the number of drop down values present in a dropdown list using selenium webdriver

package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);

WebElement we = driver.findElement(By.name("fromPort"));

java.util.List options =  we.findElements(By.tagName("option"));

System.out.println("Dropdown values count = " + options.size());

driver.close();


}
}

Output :-

Dropdown values count = 10

selenium webdriver scripts

Above script logins into Mercury tours website and gets count of number of drop down values present in "Departing From" dropdown list.






Selenium Webdriver Scripts:Selenium Webdriver getTagName()

script 1

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String tagname =  driver.findElement(By.id("gbqfba")).getTagName();
System.out.println(tagname);

}
}

Output:-

button

getTagName() method Gets the tag name of the element.

Above script gets tag name of google search button.


script2

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.co.in");
String tagname =  driver.findElement(By.name("p")).getTagName();
System.out.println(tagname);

}
}

Output:-

input

Above script gets tag name of search textbox from yahoo.co.in website.

Selenium Webdriver Scripts : how to get browser name and version using selenium webdriver


package selenium_examples;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.funandknowledge.blogspot.com");
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();

String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);

String version = cap.getVersion().toString();
System.out.println(version);


}
}



Output :-

firefox
19.0

Selenium Tutorials : Record and Playback in selenium ide

Record a test script

In this tutorial we are going to record a test script with Selenium IDE.

1.Open mozilla firefox browser and in tools menu click on selenium ide.

2.Click on record button in selenium ide.

3.Open google.com in firefox browser.

4.Type funandknowledge.blogspot.com  in google search box and click on search button.

5.Stop recording in selenium by clicking on red button.

6.Click on play current testcase icon which playbacks the recording actions.

screenshot for above recorded scenario in Selenium Ide



How to download and install selenium IDE?




Steps to download and install Selenium IDE (Integrated Development Environment).

1.In Mozilla Firefox browser open http://docs.seleniumhq.org/download/

2.Under Selenium IDE section click on version number here its 2.3.0 as shown in screenshot below


3.Clicking on selenium ide link a security pop up box as shown in screenshot will display.Click on "Allow".



4.When you click on allow button, Firefox will install Selenium IDE.Click on restart button in pop up.



5.After firefox has restarted you can see Selenium IDE listed under the Firefox Tools menu.Click on Selenium IDE as shown in screenshot or press Ctrl+Alt+S it will launch Selenium IDE.













How to download and install UFT 11.5



Steps to download and install UFT 11.5 :-

1.open url http://www8.hp.com/us/en/software-solutions/software.html?compURI=1172122#tab=TAB3

2.Click on 'Trial software'

3.Click on HP Unified Functional Testing 11.50 CC English SW E-Media Evaluation



4.Fill in your personal details and click Next



5.Agree Software Download Terms of Use

 
6.Click on download link


7.It will take 2 to 3 hrs to download

8.Extract the zip file

9.Choose the default options and click on cancel when it asks for the license key

10.UFT 11.5 trial version will be successfully installed and can be used for 30 days

Selenium getAttribute(value) and getAttribute(maxlength) script examples



package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://book.spicejet.com/");
System.out.println(driver.findElement(By.name("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1_CTXT")).getAttribute("value"));




}
}

Output:-

Leaving from...


getAttribute() method :- Gets the value of a given attribute of the element.

getAttribute("value") retrieves the value inside the fields.In above script from http://book.spicejet.com,it retrieves text from "Leaving from" Dropdown box .


Script 2

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://ebay.in");
System.out.println(driver.findElement(By.id("gh-ac")).getAttribute("maxlength"));




}
}

Output:-

300

getAttribute("maxlength") retrieves the maxlength of the field.In above script from http://ebay.in ,it retrieves maxlength of search textbox .



How to capture label text in Selenium webdriver


Script to get label text or capture label text in Selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println(driver.findElement(By.name("btnK")).getText());
System.out.println(driver.findElement(By.name("btnI")).getText());

 
}
}


Output:-

Google Search
I'm Feeling Lucky

Above scripts gets the text of buttons in google webpage.

getText() method gets text of the element.


How to get names of links in selenium webdriver?

Below script gets the links names of Signon and Register in Mercury Tours webpage.


package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td/a")).getText());
System.out.println(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).getText());



}
}

Output:-

SIGN-ON
REGISTER

How to Get the Tooltip text through Selenium Webdriver


Webdriver script to get Tooltip text in selenium webdriver.

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
   driver.get("http://docs.seleniumhq.org/");
   WebElement toolTipObject = driver.findElement(By.xpath("/html/body/div/div/h1/a"));
   String GoogleTooltip = toolTipObject.getAttribute("title");
        System.out.println(GoogleTooltip);
             }

           }

Output of the script:-

Return to Selenium home page


Above script opens http://docs.seleniumhq.org/ webpage and gets the tooltip of below highlighted part. imag



getAttribute():-Gets the value of an element attribute specified.


How to capture screenshot of webpage in selenium webdriver


package selenium_examples;


import java.io.File;
import java.io.IOException;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://funandknowledge.blogspot.com");

  File capturescreenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


 try {
FileUtils.copyFile(capturescreenshotFile,new File("E:\\fnkblogscreenshot.png"));
} catch (IOException e) {

e.printStackTrace();
}

 
}
}

Above script takes screenshot of funandknowledge.blogspot.com webpage.Screenshot is stored in E drive with name fnkblogscreenshot.

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge