DWQA Ask QuestionCategory: SeleniumHandle Compare two products in a E commerce Web Site-Ingram Micro
admin Staff asked 5 years ago
  1. Navigate to URL: https://ca.ingrammicro.com
  2. Search for product: Apple
  3. Assert that search result is displayed.
  4. Create a method which takes arrays of SKU’s
  5. This method should find all the number of SKU’s in the list.
  6. If any SKU is not found than print error message and come out.
  7. if all the sku is found than check corresponding check box.
  8. Click compare
  9. validate Compare screen is displayed.
  10. Assert that correct products are compared by validating the product titles.

Solution at below link:

Handling Scenario: Select and Compare products in an E-commerce Site

3 Answers
Sonali Adik Staff answered 5 years ago
Start your code here
public class Ingrammicro_Search_Popup {
 WebDriver driver;
 @BeforeMethod
 public void before_method()
 {
 System.setProperty("webdriver.chrome.driver","C:\\sonali\\chromedriver.exe");
 driver=new ChromeDriver();
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
 driver.manage().timeouts().pageLoadTimeout(3000, TimeUnit.SECONDS);
 driver.get("https://ca.ingrammicro.com");
 }
 @Test
 public void search_popup() throws Exception 
 {
 WebElement search_box1 =driver.findElement(By.xpath("//input[@id='searchBox_Global']"));
 search_box1.sendKeys("Apple");
 //***Search Apple Keyword**************
 List<WebElement> apple_prod_list=driver.findElements(By.xpath("//div[@id='live-search']//tr[@class='selectable']"));
 //*********Apply Assertions************************
 /* String s_title_expected= "apple_prod_lists.getText()";
 String s_title_actual="Apple_product_list displayed";
 Assert.assertEquals(s_title_expected,s_title_actual);*/
 //By Using Enhanced For Loop
 for(WebElement apple_prod_lists: apple_prod_list)
 {
 System.out.println(apple_prod_lists.getText());
 }
 WebElement search_box =driver.findElement(By.linkText("Search:"));
 search_box.click();
 //************List Of Apple_Product********************
 List<WebElement> apple_prod_list1=driver.findElements(By.xpath("//a[@class='ellipsis-multiline tooltip-clickable']"));
 List<WebElement> Arr_list= new ArrayList<WebElement>();
 System.out.println(apple_prod_list1.size());
 for(int j=0;j<apple_prod_list1.size();j++)
 {
 Arr_list.add(apple_prod_list1.get(j));
 System.out.println(apple_prod_list1.get(j).getText());
 //String s1_Actual=apple_prod_list1.get(j).getText();
 if(j==3)
 break;
 }
 // By Using Enhanced For Loop
 /* for(WebElement apple_prod_lists1: apple_prod_list1)
 {
 System.out.println(apple_prod_lists1.getText());
 }*/
 //**************List Of SKU No*************
 List<WebElement> Sku_no=driver.findElements(By.xpath("//div[@class='product-information']/div/span[2]"));
 List<WebElement> Arr= new ArrayList<WebElement>();
 System.out.println(Sku_no.size());
 // By Using Enhanced For Loop
 /*for(WebElement SKU_list:Sku_no )
 {
 System.out.println(SKU_list.getText());
 }*/
 // By using for loop
 for(int i=0;i<Sku_no.size();i++)
 {
 Arr.add(Sku_no.get(i));
 // System.out.println(Arr.get(i).getText());
 if(Arr.get(i).getText()!=null)
 {
 System.out.println("SKU_No Found:");
 }
 else
 {
 System.out.println("SKU_No Not found:");
 }
 }
 /* List<WebElement> apple_prod_list1=driver.findElements(By.xpath("//a[@class='ellipsis-multiline tooltip-clickable']"));
 List<WebElement> Arr_list= new ArrayList<WebElement>();
 System.out.println(apple_prod_list1.size());
 for(int j=0;j<apple_prod_list1.size();j++)
 {
 Arr_list.add(apple_prod_list1.get(j));
 System.out.println(apple_prod_list1.get(j).getText());
 // String s1_Actual=apple_prod_list1.get(j).getText();
 if(j==3)
 break;
 */
 List<WebElement> checkbox =driver.findElements(By.xpath("//div[@class='prod-compare-checkbox']"));
 // By Using Enhanced For Loop
 /* for( WebElement checkbox1:checkbox)
 {
 checkbox1.click();
 }*/
 //List<WebElement> Arr1= new ArrayList<WebElement>();
 System.out.println(checkbox.size());
 for(int l=0;l<checkbox.size();l++)
 {
 //Arr1.add(checkbox.get(l)); 
 checkbox.get(l).click();
 if(l==3)
 break;
 }
 WebElement compare=driver.findElement(By.xpath("//div[@id='top-paging-container']//div[@class='pager-container']//div[@class='compare']"));
 compare.click();
 List<WebElement> compare_product_list =driver.findElements(By.xpath("//a[@class='tooltip-clickable']"));
 System.out.println(compare_product_list.size());
 List<WebElement>prod_list =new ArrayList<WebElement>();
 for(int k=0;k<compare_product_list.size();k++)
 {
 prod_list.add(compare_product_list.get(k));
 System.out.println(compare_product_list.get(k).getText());
 //String s2_Expected=compare_product_list.get(k).getText();
 // Assert.assertEquals(s1_Actual, s2_Expected);
 //} 
 }
 }
}
Sarang Staff answered 5 years ago

With do respect,I have Some doubts about the code in code

  1. Where is assertion to validate the code only if conditions were applied
  2. What if the site load slow, WebDriverWait is missing
  3. What if user choose any of the 4,3 or only two products, there is no mechanism found
  4. There is no reusable method inside code
  5. What if after searching of Philips only 3 or 2 product available in list, how then the script works as it is making comparison of 4 products
  6. Why there is unnecessary code is commented is present in code
  7. There is not a single line of description how the program flow works. 
Bindiya Patil Staff answered 5 years ago

WebDriver driver;
@BeforeMethod
public void beforeMethod1()
{
System.setProperty(“webdriver.chrome.driver”, “E:\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(6500, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.SECONDS);
driver.get(“https://ca.ingrammicro.com/&#8221;);
}
@Test
public void search_product()
{
WebElement Search_box=driver.findElement(By.xpath(“//input[@id=’searchBox_Global’]”));
Search_box.sendKeys(“Apple”);
List<WebElement> apple_product_list=driver.findElements(By.xpath(“//div[@class=’global’]//table[@class=’results’]//tr”));
for(WebElement eltement:apple_product_list)
{
System.out.println(eltement.getText());
}
WebElement Search_btn=driver.findElement(By.linkText(“Search:”));
Search_btn.click();

List<WebElement>Search_product=driver.findElements(By.xpath(“//a[@class=’ellipsis-multiline tooltip-clickable’]”));
for(WebElement element:Search_product)
{
System.out.println(element.getText());
}

List<WebElement>Skn_Values=driver.findElements(By.xpath(“//div[@class=’product-information’]/div/span[2]”));
System.out.println(Skn_Values.size());
List<WebElement>skn_value_list=new ArrayList<WebElement>();

for( int i=0;i<Skn_Values.size();i++)
{
skn_value_list.add(Skn_Values.get(i));
String Actual_skn_value=Skn_Values.get(2).getText();
String Expected_Skn_values=”387DDH”;
if(skn_value_list.get(i).getText()!=null)
{
System.out.println(“sku number is present”);
}
else
{
System.out.println(“sku number is not present “);
}
Assert.assertEquals(Actual_skn_value, Expected_Skn_values);

List<WebElement>Check_boxes=driver.findElements(By.xpath(“//div[@class=’prod-compare-checkbox’]”));
System.out.println(Check_boxes.size());
for(int j=0;j<Check_boxes.size();j++)
{
Check_boxes.get(j).click();
if(j==3)
break;
}
WebElement compare_btn=driver.findElement(By.xpath(“//div[@id=’top-paging-container’]//div[@class=’pager-container’] //div[@class=’compare’]”));
compare_btn.click();

List<WebElement> compare_product=driver.findElements(By.xpath(“//a[@class=’tooltip-clickable’]”));
System.out.println(compare_product.size());

//List<WebElement>list_of_compare_product=new ArrayList<WebElement>();
for(int k=0;k<compare_product.size();k++)
{
//list_of_compare_product.add(compare_product.get(k));
String Actual_product_name=compare_product.get(2).getText();
String Expected_product_name=” FR 27IN IMAC W/ RETINA 5K DISPLAY 3.0G 6C 8THGEN IC I5 1TB”;
//System.out.println(list_of_compare_product.get(k).getText());
Assert.assertEquals(Actual_product_name, Expected_product_name);
}

}
}
}