DWQA Ask QuestionCategory: QuestionsFlipkart Assignment: Search Product and find the highest and lowest priced product
admin Staff asked 5 years ago
  • Open Url: https://www.flipkart.com (Do not Login) and validate the page is opened by checking the title and a page locator.
  • Search for TV and check search results is displayed.
  • Fetch all the products name and their price, save it in a Hash Map collection with key as Product name and value as price. Price should be integer. Example:  HashMap<String, Integer>
  • Print the Hash Map.
  • Print the Product which has the highest price.
  • Print the Product which has the lowest price

Note:

  • Use find element, find element to locate the objects in the page.
  • Use Maven and test ng to run the test.
  • Use reporter.log to log the steps.
  • Use test ng assert for validations.
  • No need to login in the application.
admin Staff replied 5 years ago

Variable names should be camel cases or use under score. For example: highPrice or high_price. Also, it is a good practice to prefix the variable name with their types. so for example, if its string then s_high_price(s represents string) or i_high_price(i represents integer)

5 Answers
admin Staff answered 5 years ago

Answer to this question posted here:
http://www.automationfraternity.com/selenium/selenium-real-time-scenario-flipcart-search-product-and-find-lowest-and-highest-priced-product/

Tejal Gadewar Staff answered 5 years ago

public class SmokeTest {

WebDriver driver;

@Test
public void flipkart()
{
// invoke browser and url
System.setProperty(“webdriver.chrome.driver”,”F:\\automation\\chromedriver.exe”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://www.flipkart.com/&#8221;);
Reporter.log(“flipkart url launched”);
driver.findElement(By.xpath(“//button[@class=’_2AkmmA _29YdH8′]”)).click();

//validate page is title
String actual=driver.getTitle();
String expected=”Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!”;
Assert.assertEquals(actual, expected);
Reporter.log(“title validate successfully”);

//validate page object
WebElement searchbox=driver.findElement(By.xpath(“//input[@title=’Search for products, brands and more’]”));
//String actual1=searchbox.getText();
//String expected1=”Search for products, brands and more”;
// Assert.assertEquals(actual1, expected1);
Reporter.log(“validate search object successfully”);

//search for TV and check results
searchbox.sendKeys(“tv”);
searchbox.sendKeys(Keys.ENTER);
boolean chktvresult=driver.findElement(By.xpath(“//span[@class=’_2yAnYN’]”)).isDisplayed();
System.out.println(chktvresult);
Assert.assertEquals(chktvresult, true);
Reporter.log(“search and validate tv successfully”);

//fetch all products

List<WebElement> fetchprod=driver.findElements(By.xpath(“//div[@class=’_3wU53n’]”));
List<WebElement> fetchprodprice=driver.findElements(By.xpath(“//div[@class=’_1vC4OE _2rQ-NK’]”));
HashMap<String,Integer> list=new HashMap<>();
for(int i=0;i<fetchprod.size(); i++)
{
String pn=null;
int pp=0;

pn=fetchprod.get(i).getText();
pp=Integer.parseInt(fetchprodprice.get(i).getText().replaceAll(“[^0-9]”,””));
list.put(pn,pp);
}
Set<Entry<String,Integer>> hashSet=list.entrySet();
for(Entry entry:hashSet)
{
System.out.println(“key=”+entry.getKey()+”,value=”+entry.getValue());
}

}
}

Supriya Kitukale Staff answered 5 years ago

 

Start your code here
package flipkart;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;

public class SmokeTest
{
WebDriver driver;
@BeforeMethod
public void beforeMethod()
{
System.setProperty("webdriver.chrome.driver","E:\\Vision IT\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
}
@Test()
public void title_validation()
{
//validate page title
String expected="Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!";
String actual=driver.getTitle();
Assert.assertEquals(actual, expected,"title is correct");
Reporter.log("Browser chrome invoked with url");
WebElement cross_link=driver.findElement(By.xpath("//button[text()='✕']"));
cross_link.click();

//fetch all product
WebElement txtbx_searchbox=driver.findElement(By.xpath("//input[@placeholder='Search for products, brands and more' ]"));
txtbx_searchbox.sendKeys("tv");
WebElement button_search=driver.findElement(By.xpath("//button[@type='submit']"));
button_search.click();
List<WebElement> tv_List=driver.findElements(By.xpath("//div[@class='col col-7-12']/div[1]"));
List<WebElement> tv_price=driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
//Hashmap function for fetch data
String p_name=null;
int p_price=0;

HashMap<String, Integer> obj=new HashMap<String,Integer>();

for(int i=0;i<tv_List.size();i++)
{
p_name=(tv_List.get(i).getText());
p_price=Integer.parseInt(tv_price.get(i).getText().replaceAll("[^0-9]",""));
//System.out.println(p_name);
//System.out.println(p_price);
obj.put(p_name, p_price);
Set<Entry<String,Integer>>hashset=obj.entrySet();
for(Entry entry:hashset)
{
System.out.println("Product="+entry.getKey()+",Values="+entry.getValue());
}
}
/*for(int i=0;i<tv_price.size();i++)
{
String s=(tv_price.get(i).getText().replaceAll("[^0-9]",""));
}*/


}
}

 

Nivedita Gorde Staff answered 5 years ago

public class SmokeTest
{
WebDriver driver;
@BeforeMethod
public void beforeMethod()
{
//invoked the browser
System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium\\chromedriver_win32 74new\\chromedriver.exe”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);
//navigate on flipkart side
driver.get(“https://www.flipkart.com/&#8221;);
Reporter.log(“Browser is invoked and page is navigate”,true);
}
@Test
public void test01()
{
WebElement closelogin= driver.findElement(By.xpath(“//button[text()=’✕’]”));
closelogin.click();
//validate title
String Excepted_titile=”Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!”;
String Actual_title= driver.getTitle();
Assert.assertEquals(Excepted_titile, Actual_title);
Reporter.log(“Validate the page title successfully”);
//search for TV
WebElement search=driver.findElement(By.xpath(“//input[@name=’q’]”));
search.sendKeys(“TV”);
WebElement search_enter=driver.findElement(By.xpath(“//button[@type=’submit’]”));
search_enter.click();
//validate search result
WebElement check_result=driver.findElement(By.xpath(“//span[@class=’_2yAnYN’]”));
Assert.assertEquals(check_result.isDisplayed(),true);
Reporter.log(“Search for TV and then validate searching result”,true);
//Fetch Product name and their values
List<WebElement>name=driver.findElements(By.xpath(“//div[@class=’col col-7-12′]/div[1]”));
List<WebElement>price=driver.findElements(By.xpath(“//div[@class=’_1vC4OE _2rQ-NK’]”));
HashMap<Integer,String>obj=new HashMap<Integer,String>();
String Tvname=null;
int tv_price=0;
for(int i=0;i<price.size();i++)
{
Tvname=name.get(i).getText();
tv_price=Integer.parseInt(price.get(i).getText().replaceAll(“[^0-9 a-z A-Z]”,””));
obj.put(tv_price,Tvname);
}
// Print the content of the hashMap
for (Map.Entry<Integer,String> entry : obj.entrySet())
{
System.out.println(entry.getKey()+” : “+entry.getValue());
}
Reporter.log(“Fatch all the Product name ane their price and print in console”);

//Get all the keys from Hash Map
Set<Integer> allkeys = obj.keySet();
ArrayList<Integer> product_prices = new ArrayList<Integer>(allkeys);
//Sort the Prices in Array List using Collections class
Collections.sort(product_prices);
//Highest Product is
int high_price = product_prices.get(product_prices.size()-1);
//Low price is
int low_price = product_prices.get(0);

Reporter.log(“The Heightest Price for TV is “+high_price, true);
Reporter.log(“The Lowest Price for TV is “+low_price, true);
}
@AfterMethod
public void afterMethod()
{
driver.close();
}
}

admin Staff replied 5 years ago

Also use Code when u submit the response.

admin Staff replied 5 years ago

Variable names should be camel cases or use under score. For example: highPrice or high_price. Also, it is a good practice to prefix the variable name with their types. so for example, if its string then s_high_price(s represents string) or i_high_price(i represents integer)

Sneha Funde Staff answered 5 years ago
Start your code here 
//close login pop up
driver.findElement(By.xpath("//button[@class='_2AkmmA _29YdH8']")).click();
String expected_page_title="Online Shopping Site for Mobiles, Electronics, Furniture, Grocery, Lifestyle, Books & More. Best Offers!";
String actual_page_title=driver.getTitle();
Assert.assertEquals(expected_page_title, actual_page_title);

WebElement search_bx =driver.findElement(By.xpath("//input[@type='text']"));
search_bx.sendKeys("tv");
//search_bx.sendKeys(Keys.ENTER);
WebElement click_on_search_box =driver.findElement(By.xpath("//button[@type='submit']"));
click_on_search_box.click();

//for product name
String total_list_of_tv_prod = null;
List<WebElement> list_of_tv_prod=driver.findElements(By.xpath("//div[@class='_3wU53n']"));

List<WebElement> list_price_of_tv_prod=driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));

HashMap<String, Integer> list=new HashMap<String, Integer>();
for(int i=0;i<list_of_tv_prod.size();i++)
{
String total_list=null;
int prod_price=0;
total_list=list_of_tv_prod.get(i).getText();
prod_price=Integer.parseInt(list_price_of_tv_prod.get(i).getText().replaceAll("[^0-9-]",""));
list.put(total_list, prod_price);
}
Set<Entry<String,Integer>> hashSet=list.entrySet();
for(Entry entry:hashSet)
{
System.out.println("Product="+entry.getKey()+" , Values = "+entry.getValue());
}
}
catch (NumberFormatException e)
{
System.out.println(e.toString());
}
}