Saturday, 12 July 2014
Tuesday, 27 May 2014
Select Date Using Selenium Webdriver
How To Select Date From DatePicker Using Selenium WebDriver.......
import org.junit.Test;
public class demoDatePicker {
A lot of application has datepicker which shows calenders upon clicking on it. The reason is to test, Does it shows calender after clicking on it and get the user selected date. So here currently working on Selenium WebDriver. I want to select values in date range from the drop down.. I want to know how can i select the values as Date, Month and year in the date picker drop down.
Following code will open the url in firefox once it come up, click on text box which shows calender and then naviage to February and select the defined date in the code below. It will show name of month in console window.
Here we go.........
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class demoDatePicker {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/datepicker/");
driver.manage().window().maximize();
WebElement ele = driver.findElement(By.className("demo-frame"));
driver.switchTo().frame(ele);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Click on text box so that date picked will come
driver.findElement(By.id("datepicker")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
for (int i = 0; i < 12; i++) {
// Get month string
String month = driver.findElement(By.className("ui-datepicker-month")).getText();
if(month.equalsIgnoreCase("February"))
break;
driver.findElement(By.xpath("/html/body/div/div/a[2]/span")).click();
System.out.println(month);
}
// Date picker is a table so that navigate to each cell, If particular
// cell matches value 20 then select it
WebElement dateWidget = driver.findElement(By.className("ui-datepicker-calendar"));
List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));
List<WebElement> columns = dateWidget.findElements(By.tagName("td"));
// select 20th date
for (WebElement row : rows) {
for (WebElement col : columns) {
if (col.getText().equals("20")) {
col.click();
break;
}
}
}
}
}
Monday, 26 May 2014
How to write data to Excel Sheet in Selenium WebDriver
import org.apache.poi.hssf.usermodel.HSSFCell;
Assert.assertEquals(firefoxDriver.getTitle(), "Welcome: Mercury Tours");
@AfterClass
Before start writting data to excel sheet make sure Apache POI library is downloaded and add into your IDE you are going to use. The same library is being used for reading data from excel file. So this is Microsoft API which used for reading and writting data from/to Microsoft stuff.
Below code is performed using TestNg. Now writting data to excel sheet in a sequential order or maintain insertion order of keys, Order in which keys are inserted in to LinkedHashMap.
Following is the code to write to excel workbook:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Assert;
import org.junit.Before;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Set;
/**
* Created by Gaurav on 5/26/2014.
*/
public class demoWriteToExcel {
LinkedHashMap<String,Object[]> table;
HSSFWorkbook hssfWorkbook;
HSSFSheet hssfSheet;
FirefoxDriver firefoxDriver;
@BeforeClass
public void setUp(){
hssfWorkbook = new HSSFWorkbook();
hssfSheet = hssfWorkbook.createSheet("TestData");
//Used linkedHashMap to maintain insertion order of keys
table = new LinkedHashMap<String, Object[]>();
table.put("1",new Object[]{"SrNo", "TestCase", "Result"});
firefoxDriver = new FirefoxDriver();
firefoxDriver.get("http://newtours.demoaut.com/");
}
@Test
public void login() {
// It will insert data into table when it can reach to the login page.
Assert.assertEquals(firefoxDriver.getTitle(), "Welcome: Mercury Tours");
table.put("2", new Object[]{"1", "I can reach login page", "Pass"});
}
// Initialization of rows and cells to get data into the excel sheet.
@AfterClass
public void filldata(){
Set<String> strings = table.keySet();
//rows loop
int rowCount = 0;
for(String key: strings){
HSSFRow row = hssfSheet.createRow(rowCount);
Object[] objects = table.get(key);
//columns loop
int cellcount =0;
for(Object obj: objects) {
HSSFCell cell = row.createCell(cellcount);
cell.setCellValue(obj.toString());
cellcount++;
}
rowCount++;
}
try {
// Following path is given where the workbook will get created
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\Gaurav\\Desktop\\Selenium_TC\\Selenium_practicals\\gaurav.xls"));
hssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Subscribe to:
Posts (Atom)