DWQA Ask QuestionCategory: SeleniumHandle Ajax Control on E commerce Search box-Site:IngramMicro
admin Staff asked 5 years ago
  1. URL: https://ca.ingrammicro.com/
  2. Search Apple in the Box
  3. List of Items is displayed in the pop up

Validate:

  1. Wait for the Pop up to appear
  2. Assert that Pop up is displayed
  3. Assert that all the search rows displayed have text searched as ‘Apple’

Create a reusable method which should work fine for any product.

5 Answers
Sarang Staff answered 5 years ago

For Automating this Test Case I used TestNG and 3 methods(resuable) as follows

  1. initializeDriver – for Webdriver Initialization
  2. webDriverWait – for waiting the List of WebElements to be Displayed
  3. FetchProductList- for Fetching product list and validation with the Actual product Names is Contained by the product Name Specifed By User
  4. getListOfProduct – It is the Actual @Test to be executed
    Start your code here

    import java.util.List;
    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.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    public class ProductListValidation {
    static WebDriver driver;
    static WebDriverWait wait;

    // Test Case to fetch the Product by using Search functionality
    String ProductName = “Cisco”;
    @Test
    public void getListOfProduct()
    {
    initializeDriver();
    driver.get(“https://ca.ingrammicro.com”);
    FetchProductList(driver, ProductName.toLowerCase());
    }

    // WebDriver Initilization Method
    public static void initializeDriver()
    {
    System.setProperty(“webdriver.chrome.driver”, “chromedriver.exe”);
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    // Webdriver Wait Reusable Utility
    public static List<WebElement> webDriverWait(List<WebElement> WebElementList)
    {
    wait = new WebDriverWait(driver, 10);
    return wait.until(ExpectedConditions.visibilityOfAllElements(WebElementList));
    }

    // Product fetching and Validation Method
    public static void FetchProductList(WebDriver driver,String ProductName)
    {
    WebElement searchBox = driver.findElement(By.xpath(“//input[@id=’searchBox_Global’]”));
    searchBox.sendKeys(ProductName);
    List <WebElement> searchBoxContent = driver.findElements(By.id(“live-search”));

    // Validation Point 1 – Waiting for the Pop up to appear
    List<WebElement> AfterWaitsearchBoxContent = webDriverWait(searchBoxContent);
    boolean list = AfterWaitsearchBoxContent.isEmpty();

    // Assertion for list of Prodution is getting displayed
    Assert.assertFalse(list);

    // Printing the List size and Product to be Searched
    System.out.println(“Entered Product for Search is : ” + ProductName + “\n” );

    for(int i=0;i<searchBoxContent.size();i++)
    {
    System.out.println(searchBoxContent.get(i).getText());
    String textWithInList = searchBoxContent.get(i).getText().toLowerCase();
    boolean expectedTextInList = textWithInList.contains(ProductName.toLowerCase());
    Assert.assertTrue(expectedTextInList);
    System.out.println(“Searched list is Displaying Successfully “+ProductName+” Products”);
    }
    }
    }

Chintan answered 5 years ago
Start your code here

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class SearchBox
{

WebDriver driver;

@Test
public void SearchBox() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chintan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://ca.ingrammicro.com/");
driver.manage().window().maximize();

WebElement search_box=driver.findElement(By.id("searchBox_Global"));
search_box.sendKeys("apple");

List<WebElement>apple_product_list=driver.findElements(By.id("live-search"));

for(WebElement apple_product_list1:apple_product_list)
{
System.out.println(apple_product_list1.getText());
}

WebElement click_on_search=driver.findElement(By.linkText("Search:"));
click_on_search.click();

List<WebElement>all_product_name_list=driver.findElements(By.xpath("//a[@class='ellipsis-multiline tooltip-clickable']"));

for(WebElement all_product_name_list1:all_product_name_list)
{
System.out.println(all_product_name_list1.getText());
}

List<WebElement>all_product_SKU_list=driver.findElements(By.xpath("//div[@class='product-information']/div/span[2]"));

List<WebElement>SKUlist=new ArrayList<WebElement>();

int i;
for(i=0;i<all_product_SKU_list.size();i++)
{
SKUlist.add(all_product_SKU_list.get(i));
System.out.println(SKUlist.get(i).getText());
}
}
}

Sarang Staff replied 5 years ago

Where is Assertion or any check point?

Nikita dumbhare Staff answered 5 years ago

 

Start your code here

Page Object File

public class PO_IngramSearch {
WebDriver driver;

public PO_IngramSearch(WebDriver driver) {
this.driver = driver;
}


@FindBy(xpath="//input[@id='searchBox_Global']")
WebElement searchProduct;

@FindBy(xpath="//tr[@class='selectable' and @rel='0' ]")
WebElement clickOnProduct;

@FindBy(xpath ="//div[@id='live-search']//table[@class='results']//tr")
List<WebElement> allPopUpElements;

@FindBy(xpath ="//div[@class='prod-number-container vpn_breakword']//span[2]")
List<WebElement> skuID;

@FindBy(xpath ="//div[@class='search-results']//div[@class='toggle-positioning-container']//input[@type='checkbox']")
List<WebElement> checkboxClick;

@FindBy(xpath ="//div[@id='top-paging-container']//div[@class='compare']")
WebElement clickOncompare;

@FindBy(xpath ="//td//p[@class='product-name']//a[@class='tooltip-clickable']")
List<WebElement> comparePageProductList;



public void validateIngramPageTitle(){
String title =driver.getTitle();
Assert.assertEquals(title,"Computer and Technology Products - Services for Business to Business Needs - Ingram Micro");

}
public void searchParticularProduct(){
searchProduct.click();
searchProduct.sendKeys("Apple");
}
public void validateSearchPopUpData(){
int count1 = allPopUpElements.size();
String[] actualData = {"Apple", "Applecare", "Apple Case", "Apple Tablet", "Apple Notebook", "Apple Carrying Case", "Apple Smart Watch", "Apple Smartwatch Band", "Apple Smartphone", "Apple Headset", "Apple Cover Case", "Apple Earset"};
for (int i = 0; i < count1; i++) {

//Assert that Pop up is displayed
Assert.assertTrue(allPopUpElements.get(i).getText().toLowerCase().equals(actualData[i].toLowerCase()));

//Assert that all the search rows displayed have text searched as ‘Apple’
Assert.assertTrue(allPopUpElements.get(i).getText().contains("Apple"));
System.out.println(allPopUpElements.get(i).getText());
}
}

public void clickOnProduct(){
clickOnProduct.click();
}

public void validateskuID(){
String a="nikita";
System.out.println(a.length());
String[] skuData ={"395DDH","7499DB","387DDH","6661XD","9497JD","9493JD","9499JD","9501JD","14887Z","14888Z"};
int count2=skuID.size();
for(int j=0;j<count2;j++){
Assert.assertTrue(skuID.get(j).getText().equals(skuData[j]));
System.out.println(skuID.get(j).getText());
}
}
public void clickOnCheckbox() throws InterruptedException{

for ( int k=0;k<1;k++ ) {
if ( !checkboxClick.get(k).isSelected()) {
checkboxClick.get(0).click();
checkboxClick.get(1).click();
}
clickOncompare.click();
}
}


public void validateComparePageTitle(){
String productComparison ="Product Comparison";
Assert.assertEquals(productComparison, "Product Comparison");

}
public void validatingComparePageProductTitles(){


String [] comparedProduct = {"FR 21.5IN IMAC W/ RETINA 4K DISPLAY 3.0G 6C 8THGEN IC I5 1TB","CTO MACBOOK 12IN 1.3G M7 8GB 512GB SILVER"};
for(int l=0;l<comparePageProductList.size();l++)

Assert.assertTrue(comparePageProductList.get(0).getText().equals(comparedProduct[0]));
Assert.assertTrue(comparePageProductList.get(1).getText().equals(comparedProduct[1]));
System.out.println("Task Completed");


}


TC File

public class TC_IngramSearch {

WebDriver driver;

@BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

}

@BeforeMethod
public void beforeMethod() {
driver = new ChromeDriver();

driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.get("https://ca.ingrammicro.com/");


}

@Test(priority = 0)
public void TC_Positive_Search_Product() {
try {
PO_IngramSearch search = PageFactory.initElements(driver, PO_IngramSearch.class);
search.validateIngramPageTitle();
search.searchParticularProduct();
Thread.sleep(2000);
search.validateSearchPopUpData();
search.clickOnProduct();
search.validateskuID();
search.clickOnCheckbox();
search.validateComparePageTitle();
search.validatingComparePageProductTitles();
} catch (InterruptedException e) {

e.printStackTrace();
}

}

@AfterMethod
public void tearDown() {
driver.close();
;
}
}

 

admin Staff replied 5 years ago

@Nikita: please rectify the mistakes sarang pointed out.

Sarang Staff replied 5 years ago

With do respect if have some doubts in the code as following

1. Why this Element showing 16 Values if only 10 Products were displyed on Page
element -checkboxClick – with xpath
“//div[@class=’search-results’]//div[@class=’toggle-positioning-container’]//input[@type=’checkbox’]”

2. Why thread.sleep used in TestCase as it is mentioned in testcase to use webdriverWait

3.Why assertions is applied in PO file

4. Why this variable used in PO file – String a=”nikita”;

5. Why in PO file method “validatingComparePageProductTitles” the value of variable “String [] comparedProduct”
is Hardcoded – (data should be not hardcoded mentioned in testcase) ?

6. Why in Po file WebElement showing 3 values as there are only two products were getting selected 2 for comparision.
WebElement –
comparePageProductList – “//td//p[@class=’product-name’]//a[@class=’tooltip-clickable’]”

7. Why only First two products of the list were getting selected? if someone needed to verify data for any other product from the list then ?

Sarang Staff replied 5 years ago

With do respect if have some doubts in the code as following

1. Why this Element showing 16 Values if only 10 Products were displyed on Page
element -checkboxClick – with xpath
“//div[@class=’search-results’]//div[@class=’toggle-positioning-container’]//input[@type=’checkbox’]”

2. Why thread.sleep used in TestCase as it is mentioned in testcase to use webdriverWait

3.Why assertions is applied in PO file

4. Why this variable used in PO file – String a=”nikita”;

5. Why in PO file method “validatingComparePageProductTitles” the value of variable “String [] comparedProduct”
is Hardcoded – (data should be not hardcoded mentioned in testcase) ?

6. Why in Po file WebElement showing 3 values as there are only two products were getting selected 2 for comparision.
WebElement –
comparePageProductList – “//td//p[@class=’product-name’]//a[@class=’tooltip-clickable’]”

7. Why only First two products of the list were getting selected? if someone needed to verify data for any other product from the list then ?

admin Staff answered 5 years ago
Start your code here
public void HandleAjaxSearch() {
String PRODUCT = "HP";

//Browser Set Up and navigate
System.setProperty("webdriver.chrome.driver", "E:\\_AkashStuff\\Automation\\dependencies\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://ca.ingrammicro.com/");//difference btw and naviogate/get
Reporter.log("Browser opned and navigated",true);

//Checkpoint
String expected_title = "Computer and Technology Products - Services for Business to Business Needs - Ingram Micro";
String actual_title = driver.getTitle();
Assert.assertEquals(actual_title, expected_title, "Title is correct");

//Soft Assert-Used only in cases where checkpointy failure has an
//impact on the subsequent steps execution
SoftAssert obj = new SoftAssert();
obj.assertEquals(actual_title, expected_title, "Title is correct");

//Search for item
WebElement searchbox = driver.findElement(By.id("searchBox_Global"));;
searchbox.sendKeys(PRODUCT);

//Sync-wait for pop up to appear
WebElement search_ajax_list = driver.findElement(By.id("live-search"));

WebDriverWait wait = new WebDriverWait(driver, 60) ;
wait.until(ExpectedConditions.visibilityOf(search_ajax_list));

//Validate ajax pop with search list items is displayed
boolean expected = search_ajax_list.isDisplayed();
boolean actual = true;
Assert.assertEquals(expected, actual,"List did not appear");

//Fetch all the items in the list and check their text
List<WebElement> collection = driver.findElements(By.xpath("//div[@id = 'live-search']//tr"));
for(int i=0;i<collection.size();i++) {
SoftAssert softassert = new SoftAssert();
softassert.assertEquals(collection.get(i).getText(), PRODUCT);
}





}
Sarang Staff replied 5 years ago

@Test annotation is misssing

Nitu A Staff answered 5 years ago

Start your code here

import java.util.List;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class AppleSearch {
WebDriver driver;
@BeforeMethod
public void BaseClass() {

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\HP\\Downloads\\selenium folder\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}

@Test
public void Test() {
// *****************Navigate to URL: https://ca.ingrammicro.com*****************
driver.get(“https://ca.ingrammicro.com&#8221;);
// *****************Search for product: Apple*******************************
WebElement search_Product = driver.findElement(By.xpath(“//input[@id=’searchBox_Global’]”));
search_Product.sendKeys(“Apple”);
// ************* List of Items is displayed in the pop
// up*****************************
WebElement result = driver.findElement(By.xpath(“//div[@id=’live-search’]”));
// ***********************Wait for the Pop up to
// appear*****************************
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath(“//div[@id=’live-search’]//tr[@class=’selectable’]”)));
// *************Assert that search result is displayed.***********************
boolean expected = result.isDisplayed();
boolean actual = true;
Assert.assertEquals(expected, actual);
// ****************fetch all the item from list and displayed it
// ***********************************************
List<WebElement> Apple_List = driver.findElements(By.xpath(“//div[@id=’live-search’]//tr]”));
System.out.println(“list of apple” + Apple_List.size());
for (int i = 0; i <= Apple_List.size(); i++) {
SoftAssert soft = new SoftAssert();
soft.assertEquals(Apple_List.get(i).getText(), “Apple” + “list is displayed”);
System.out.println(Apple_List.get(i).getText());
}
}
@AfterMethod
public void AfterTest() {

}
}

 

Sarang Staff replied 5 years ago

Issues –
1. driver.get(“https://ca.ingrammicro.com”);
why the url having extra characters and inside “” url should be there but its not.