Module 17: BigBasket - Comparing Basket Data with Excel Info | Selenium Forum
M
Posted on 31/07/2016
Hi,

I have added products to basket and able to verify the subtotal. But unable to verify the product and quantity in the basket with the excel.
Could you please help me with the logic to cross verify.

package exercises;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

public class BigBasket {

static WebDriver driver;
static int count=0;
public static void main(String[] args) throws Exception {

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("https://bigbasket.com");

Xls_Reader xls = new Xls_Reader("D:\\Selenium\\WebDriver Practise\\BigBasket.xlsx");

driver.findElement(By.xpath("//*[@id='skip_explore']")).click();
Thread.sleep(1000);

int rows = xls.getRowCount("Sheet1");
int cols = xls.getColumnCount("Sheet1");

for(int rNum=2;rNum<=rows;rNum++){

String prod = xls.getCellData("Sheet1",0,rNum);
String quant = xls.getCellData("Sheet1",1,rNum);

addToBasket(prod,quant);

count++;

String cartItems = driver.findElement(By.xpath("//span[@class='uiv2-num-basket-items']")).getText().split(" ")[0].trim();
//System.out.println("Cart Items -- "+cartItems);
//System.out.println("Count of Items added: "+count);

if(count==Integer.parseInt(cartItems))
System.out.println("Basket Updated Correctly for "+count+" item(s)");
else
System.out.println("Basket is NOT Updated Correctly");

}


//View Cart
WebElement cartHover = driver.findElement(By.xpath("//div[@class='uiv2-basket-items']"));

Actions act = new Actions(driver);
act.moveToElement(cartHover).build().perform();
Thread.sleep(5000);

driver.findElement(By.xpath("//a[@class='uiv2-checkout-button-small fltr view_basket_checkout']")).click();
Thread.sleep(3000);

// U Must login :

driver.findElement(By.xpath("//*[@id='uiv2-loginform']/div[2]/span/input")).sendKeys("**********@***mail.com");
driver.findElement(By.xpath("//*[@id='password']")).sendKeys("********");
driver.findElement(By.xpath("//*[@id='password']")).sendKeys(Keys.ENTER);

Thread.sleep(3000);

//Verify SubTotal equals to addition of total prices in basket

List<WebElement> Sub_Amount_List = driver.findElements(By.xpath("//span[@class='uiv2-subtotal-rate']/span[2]"));

Double Final_SubTotal = Double.parseDouble(driver.findElement(By.xpath("//*[@id='finalSubTotal']")).getText().replace("Rs. ", "").trim());

Double Expected_SubTotal = 0.00;
for(int i=0;i<Sub_Amount_List.size();i++){
Expected_SubTotal = Expected_SubTotal + Double.parseDouble(Sub_Amount_List.get(i).getText());
}
System.out.println("Expected Sub Total : "+Expected_SubTotal);

System.out.println("Actual Sub Total : "+ Final_SubTotal);

Assert.assertEquals(Final_SubTotal,Expected_SubTotal);
System.out.println("Assert -- Final SubTotal is Equal To Expected SubTotal");

//To verify Basket against Excel ----*****Unable to Verify********

List<WebElement> part1_list = driver.findElements(By.xpath("//div[@class='uiv2-yourbasketitems-gridlist']/div/ul/li[2]/a[1]"));
List<WebElement> part2_list = driver.findElements(By.xpath("//div[@class='uiv2-yourbasketitems-gridlist']/div/ul/li[2]/a[2]"));

List<WebElement> quantity_list = driver.findElements(By.xpath("//input[@class='text-change-qty-search-popup'and @type='text']"));

for(int i=0;i<quantity_list.size();i++){
String full_name = part1_list.get(i).getText().trim() + " " + part2_list.get(i).getText().trim();
System.out.print(full_name);
String quant_indiv = quantity_list.get(i).getAttribute("value");
System.out.println(" ------- "+quant_indiv);


}

} //Function to add products to Baskets
public static void addToBasket(String productName, String productQuantity) throws Exception{

driver.findElement(By.xpath("//*[@id='id_q']")).clear();
driver.findElement(By.xpath("//*[@id='id_q']")).sendKeys(productName);
driver.findElement(By.xpath("//*[@id='auto_search']/div/form/input[2]")).click();

Thread.sleep(1000);

List<WebElement> searchResults = driver.findElements(By.xpath("//div[@id='facet-products-wrapper']/div[@id='products-container']/div/ul/li/div[3]/span/a"));

List<WebElement> quantityBoxes =driver.findElements(By.xpath("//div[@id='facet-products-wrapper']/div[@id='products-container']/div/ul/li/div[5]/div[2]/div/div[3]/input[@type='text']"));

//System.out.println("Quanity Boxes --- " + quantityBoxes.size());

for(int i=0;i<searchResults.size();i++){
String str = searchResults.get(i).getText();
//System.out.println(str);
//System.out.println(str.replace("\n", " ").trim());
//System.out.println(searchResults.get(i).getAttribute("href"));
//System.out.println(searchResults.get(i).isDisplayed());

}

List<WebElement> allButtons = driver.findElements(By.linkText("ADD"));
//System.out.println("Add Buttons -- "+allButtons.size());


int j=-1;
for(int i=0;i<searchResults.size();i++){
if(searchResults.get(i).isDisplayed())
j++;
if(productName.equalsIgnoreCase(searchResults.get(i).getText().replace("\n", " ").trim())){
quantityBoxes.get(i).clear();
quantityBoxes.get(i).sendKeys(productQuantity);
allButtons.get(j).click();
Thread.sleep(1000);
System.out.println("Successfully added --- "+searchResults.get(i).getText().replace("\n", " ").trim());

}
}

}

}

M
Replied on 01/08/2016

[quote:1wllbjgb]But unable to verify the product and quantity in the basket with the excel.
[/quote:1wllbjgb]

can you get data from the website?


M
Replied on 01/08/2016

Yes, I am able to get the product names and the quantity from the site. The below part (before the function definition addToBasket()) prints the quantity and products in the Cart:

for(int i=0;i<quantity_list.size();i++){
String full_name = part1_list.get(i).getText().trim() + " " + part2_list.get(i).getText().trim();
System.out.print(full_name);
String quant_indiv = quantity_list.get(i).getAttribute("value");
System.out.println(" ------- "+quant_indiv);


M
Replied on 02/08/2016

what are you trying to do here.

String full_name = [color=#FF0000:aa8mmnrq]part1_list.get(i).getText().trim() + " " + part2_list.get(i).getText().trim();
[/color:aa8mmnrq]


M
Replied on 02/08/2016

The product Name added to the Cart consists of two <a> tags
FRESHO
Potato
So, i have separately derived the two <a> tags into part1 and part2. I am then combining them to get the full name similar to the excel.
[attachment=0:1r11cubb]Screenshot of Basket.jpg[/attachment:1r11cubb]

Responsive image

M
Replied on 02/08/2016

i'm getting this

Responsive image

M
Replied on 03/08/2016

There is a new "Offer" div which couldn't recognize the products on the page. I have changed the xpath of the searchResults to be more precise. Please update ONLY this line in the code (in addToBasket function). I am able to add items to the cart now.

List<WebElement> searchResults = driver.findElements(By.xpath("//div[@class='uiv2-list-box-img-title']/span/a"));


M
Replied on 04/08/2016

i'm getting this now.

Responsive image

M
Replied on 04/08/2016

I am not getting any error. I tried again Now. I was able to successfully add the products to basket and also view them in cart.
The line highlighted is just clicking on the Search button after entering the product name (from excel). There is no change in the xpath of the button.

Please try again and let me know in case if you are still facing issues.


M
Replied on 04/08/2016

didn't work.

please ping me on gtalk.


M
Replied on 04/08/2016

I have pinged on gmail and waiting for reply. In the meantime, I am attaching the output console that I got today morning.
If you are still facing issues, could you please help with the logic on how we can compare excel data (2 columns) with the Basket page (after getting the item names).
I will try the code and send for your review.

[attachment=0:1zbto4w7]BigBasket Console Output.jpg[/attachment:1zbto4w7]

Responsive image

M
Replied on 08/08/2016

is your query solved karthik?


M
Replied on 08/08/2016

The query is solved after talking with Ashish. I was successfully able to compare the excel values with the Cart.
Thanks Ashish for guiding on the logic.
(Might not be an effective one, but still got the task done. Look forward to optimize after going through Framework videos)

Below code after the for loop of //Printing Cart Products and their Quantity
//Verifying against Excel
for(int rNum=2;rNum<=rows;rNum++){

String prod = xls.getCellData("Sheet1",0,rNum);
String quant = xls.getCellData("Sheet1",1,rNum);
for(int i=0;i<quantity_list.size();i++){
String full_name = part1_list.get(i).getText().trim() + " " + part2_list.get(i).getText().trim();


if(full_name.toLowerCase().contains(prod.toLowerCase())){
if(quantity_list.get(i).getAttribute("value").equals(quant)){
System.out.println("Product "+prod+" is present in Cart (displayed as ["+full_name+"]) with Quantity "+quant+" which is expected");
break;
}
}
}
}