Showing posts with label Selenium webdriver with Java. Show all posts
Showing posts with label Selenium webdriver with Java. Show all posts

Thursday, 19 June 2014

Sample Java WebDriver usage

The following test case was discussed at Cigniti company technical interview question:
  1. 1. open flipkart.com 
  2. 2.select electronics in that click on laptops.
  3. 3.In Laptops brands select HP
  4. .4.select checkbox 30000-40000 
  5. 5. list of Laptops copy to excel sheet( without using Data table).
  6. 6.select first Laptop and buy it.
As per my understanding I've prepared Java code to automate with Selenium WebDriver


solution:------------

import java.io.FileOutputStream;
import java.io.WriteAbortedException;
import java.util.ArrayList;

import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
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.support.ui.Select;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import com.thoughtworks.selenium.Selenium;
public class flipkart_lappy {
 public WebDriver driver;
 public Selenium selenium;
 String str1;
 
 @Testpublic 
 void lappy() throws Exception{
  FileOutputStream fo =new FileOutputStream("E:\\Saritha\\output\\result.xls");
  WritableWorkbook wb = Workbook.createWorkbook(fo);
  WritableSheet ws = wb.createSheet("Sheet1", 0);
  driver.findElement(By.xpath(".//*[@id='fk-mainhead-id']/div[2]/div/div[1]/ul/li[1]/a/span")).click();
  Thread.sleep(1000);
  driver.findElement(By.linkText("Laptops")).click();
  Thread.sleep(1000);driver.findElement(By.linkText("HP")).click();
  Thread.sleep(1000);driver.findElement(By.xpath("//*[@id='price_range']/li[4]/a/input")).click();
  Thread.sleep(1000);//adding input from excel sheet.
  String str1= driver.findElement(By.xpath(".//*[@id='browse-results-area']/div[6]")).getText();
  System.out.println("str1");String s[] = str1.split("\n");
  System.out.println(s.length);
  for (int i = 0; i < s.length; i++) 
  {
   if (s[i].contains("HP")) {
    System.out.println(s[i]);
    }
    Label res =new Label(0, i, s[i]);
    
    ws.addCell(res);
   }
   wb.write();
   wb.close();
   driver.findElement(By.xpath("//*[@id='products']/div[1]/div[1]/div/div[1]/a")).click();
   ArrayListwindow =new ArrayList(driver.getWindowHandles());
   driver.switchTo().window(window.get(0));
   Thread.sleep(1000);
   driver.findElement(By.xpath("//*[@id='mprod-buy-btn']/form/input[3]")).click();
  }
  
  @BeforeTest
  public void beforeTest() {
   driver =new FirefoxDriver();
   driver.get("http://www.flipkart.com/");
   driver.manage().window().maximize();}
@AfterTestpublic void afterTest() {

driver.quit();}

}

try to excute it