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

 




 

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge