How To Select Date From DatePicker Using Selenium WebDriver.......
import org.junit.Test;
public class demoDatePicker {
Tuesday, 27 May 2014
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();
}
}
}
Sunday, 18 May 2014
Actions In Selenium using Selenium WebDriver
There are different kind of actions can be perform in selenium webdriver. So every action needs to build and then it will be ready to perform. The actions will be responsible for every action performed in browser. In most of the cases we will use .Build() and .perform() after initialization of every new action.
So following are the actions that can be perform:
1. To build multiple actions in a order and then return the result of that particular action:
new Actions(driver).Build();
2. To execute currently built action and no return:
new Actions(driver).Perform();
3. To perform particular action on the browser to be perform:
new Actions(driver).Build().Perform();
It is better to specify by locator using findelement.
4. To click the mouse on last know mouse co-ordinates and return the actions:
new Actions(driver).Click();
5. To click the mouse on specified element and return the actions:
new Actions(driver).Click(draggable);
6. To click and hold the mouse button on last known mouse co-ordinates and return the actions:
new Actions(driver).ClickAndHold();
7. To drag-and -drop from one element to another and that return the actions:
new Actions().DragAndDrop(draggable, droppable);
8. To drag-and-drop to specified offset on one element and that returns the actions:
new Actions(driver).DragAndDropToOffset(draggable, 25, 35);
9. To move the mouse over specified element and that return the actions:
new Actions(driver).MoveToElement(draggable);
10. To move the mouse to the specified offset of the top left corner of the specified element and that returns the actions:
new Actions(driver).MovetoElement(draggable, 25, 35);
11. To release the mouse button at the last known mouse co-oridinates and that return the actions:
new Actions(driver).Release();
12 To release the mouse button on specified element and that returns the actions:
new Actions(driver).Release(draggable);
13. To send the multiple keystrokes in specific order to the browser and that return the actions:
new Actions(driver). SendKeys(Keys.Alt)
In this above syntax key parameter is string.
14. To send sequence of key strokes to the specified element in the browser and that return the actions:
new Actions(driver) .SendKeys(draggable, Keys.Alt);
In this above syntax key parameter as string.
15. To send a modifier key down message to the browser and that return the actions:
new Actions(driver).KeyDown(Keys.Alt)
16. To send a modifier key down message to the specified element in the browser and that return the actions:
new Actions(driver).KeyDown(draggable, Key.Alt);
17. To send a modifier key up message to the browser and that return the actions:
new Actions(driver).KeyUp(Keys.Alt)
18. To send a modifier key up message to the specified element in the browser and that return the actions:
new Actions(driver).KeyUp(draggable, Key.Alt);
Now we will see how different actions we can use in selenium.
public class demoActions {
WebDriver driver;
@Before
public void setup() {
driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();
}
/* @After
public void tearDown(){
wd.quit();
}*/
@Test
public void shouldDoUnitTesting() {
WebElement uName = driver.findElement(By.name("userName"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("login"));
Actions actions = new Actions(driver);
Action build = actions.moveToElement(uName)
.click(uName)
.keyDown(Keys.SHIFT)
.sendKeys("s")
.keyUp(Keys.SHIFT)
.sendKeys("oftedge100")
.keyDown(Keys.CONTROL)
.sendKeys("a" +"c")
.keyUp(Keys.CONTROL)
.click(pwd)
.keyDown(Keys.CONTROL)
.sendKeys("v")
.keyUp(Keys.CONTROL)
.click(login)
.build();
build.perform();
}
}
Thanks to Savita Shinde....
Subscribe to:
Posts (Atom)