Google+

Using Selenium Test Automation - Search for any specific Laptop in Flipkart website and add it to Bag

The below is the problem and solution code for Searching for any specific Laptop model in Flipkart website and adding it to Bag

Problem:




Solution:

import java.util.Iterator; 
import java.util.Set; 
import java.util.concurrent.TimeUnit; 
 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class Demo { 
 
 public static void main(String args[]) { 
  
  WebDriver driver = new FirefoxDriver(); 
   
  driver.get("http://www.flipkart.com"); 
   
  driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 
   
  try { 
   
  
   if(driver.findElement(By.xpath("//input[@type='password']")).isDisplayed()) { 
    
   driver.findElement(By.xpath("(//button)[2]")).click(); 
   
           } 
   
  }catch(Throwable t) { 
   
   System.out.println("Lightbox not displayed"); 
   
  } 
   
   
  driver.manage().window().maximize(); 
   
  driver.findElement(By.name("q")).sendKeys("Acer Aspire 3 Pentium Quad Core"); 
   
  driver.findElement(By.xpath("//button[@type='submit']")).click(); 
   
  driver.findElement(By.xpath("(//div[contains(text(),'Laptop')])[1]")).click(); 
   
  Set<String> windows = driver.getWindowHandles(); 
   
  Iterator<String> itr = windows.iterator(); 
   
  String mainWindow = itr.next(); 
   
  String childWindow = itr.next(); 
   
  driver.switchTo().window(childWindow); 
  
  try { 
    
   
   if(driver.findElement(By.xpath("//input[@type='password']")).isDisplayed()) { 
    
       driver.findElement(By.xpath("(//button)[2]")).click(); 
   
      } 
   
  }catch(Throwable t) { 
   
   System.out.println("Lightbox not displayed"); 
   
  }
   
  if(driver.getTitle().contains("Acer Aspire")) { 
   
   driver.findElement(By.xpath("(//button)[2]")).click(); 
  
  } 
  
  System.out.println("Item added to bag sucessfully"); 
  
 } 
 
 
} 

2 comments:

Pravin Patel said...

This is great. Anyhow Flipkart will not punish by testing on live site?

Arun Motoori said...

Yes, its not a good practice to automate sites like Flipkart.

I believe that I am doing any harm to the Flipkart, rather I feel I am promoting/marketing flipkart to reach more audience.

Some people may end up in purchasing from Flipkart after trying this scenario.