'Check Selected Product and product which is added to cart are same or not

I have searched for a mobile product which is of type " Asus Zenfone 5 " in flipkart.com website, search display many products. I selected first product and added it to cart.

Below is my code:

 @Test
 public void flipkartScript() throws InterruptedException {

    driver.get("http://www.flipkart.com/");
    driver.findElement(By.id("fk-top-search-box")).sendKeys("Asus Zenfone 5");
    driver.findElement(By.xpath("//input[@value='Search']")).click();

    // Selecting first product
    driver.findElement(By
        .xpath(".//*[@id='products']/div/div[1]/div[1]/div/div[1]/a[1]"));
    String name = driver.findElement(By.className("title")).getText();
    String price = driver.findElement(By
        .xpath("//span[@class='selling-price omniture-field']")).getText();
    System.out.println("ProductName: "+name + "  ProductPrice: "+price);

    //Adding selected product to cart
    driver.findElement(By.xpath("//input[@value='Add to Cart']")).click();
    Thread.sleep(3000);

    //Checking product is available in cart or not
    driver.findElement(By.xpath("//span[@class='cart-label']")).click();
    Thread.sleep(5000);
}

Everything is working fine in my code.

Now my doubt is, to check if Selected product and added product both are same. Product name in cart is different from the first product Name in the search results. We can only check if the product names are same or not. Other than that, is there any other option to check? So how can we check whether the selected product is same as the product which is in the cart??



Solution 1:[1]

I agree with you that just the Product name cannot be used to distinguish between different products as many products may have same or similar name and that may not give you the expected result.

Try Product Id instead. Use the code to compare the Product Id's.

driver.findElement(By.xpath("//*[@id='products']/div/div[1]/div[1]/div"))
      .getAttribute("data-pid")     //Id from first product in search result

driver.findElement(By.xpath("//*[@class='compare-items']/div[1]"))
      .getAttribute("item_id"))    //Id from product added to cart

Also, there seems to be few dynamic XPath in your code, you may need to make it static so that test do not fail.

Let me know if you have any queries.

Solution 2:[2]

I think the title with mobile model number is displayed in the cart page and where as in the Product Details page, only the title of phone is displayed.

So if you want to compare both the titles:-

//Product Details page.
Title is = Asus Zenfone 5.
String product = driver.findElement(By.xpath("//*[@itemprop='name']")).getText();

Note:- Difference in the page is "Add To Cart" Button is enable and you can click it.

//Cart Page After clicking on mobile image icon the Title is = Asus Zenfone 5. Click on the small Mobile image icon and it will takes to the bigger page. Then you can take get Title.

driver.findElement (By.xpath("//*[@class='carty-image fk-text-center fk-position-relative']/a/img")).click();
String cart = driver.findElement(By.xpath("//*[@class='title   Hover']")).getText();

Note:- Difference in the page is "Add To Cart" Button is disable, As we have already added the Item to the cart.

Now you can compare by.

Assert.assertEquals(product, cart); 

I hope this helps you.

Solution 3:[3]

After selecting the product in product detail page get the page title and store it in a String

String product_title=driver.getTitle();

After adding the product to the cart in the cart page click the product image in the cart(You can also do this in another tab)

driver.findElement(By.xpath("//div[contains(@class,'carty-image')]/a/img")).click();

Assert.assertEquals(product_title, driver.getTitle());

We are comparing both the title's.In your case it should be "Asus Zenfone 5 Price in India - Buy Asus Zenfone 5 Black 8 Online - Asus : Flipkart.com" By this way you can be 100% sure that the product is the same product you selected.You can also check price in the details page and compare it with the cart page

You can also get the product attributes "Black, with 8 GB, with 1.2 GHz Processor" in detail page and compare it with the cart page

//product detail page
String detail_page=driver.findElement(By.xpath("//span[@class='subtitle']")).getText();
//cart page     
String cart_page=driver.findElement(By.xpath("//p[contains(@class,'fk-font-11')]")).getText();
Assert.assertEquals(detail_page, cart_page); 

Edit :

You cannot compare by using name in both product page and cart page bcoz if u look at the page source of cart page the name is "Asus A501CG-2A508WWE/ A501CG-2A584WWE Zenfone..." even if we write a regex and get the name ex : Asus zenfone we won't be able to get the full name Asus Zenfone 5 so this will not be the correct procedure to check.Only in the order summary section which appears after placing the order(clicking place order button in view cart page) you will be able to verify using the name "Asus Zenfone 5".

Explanation about my solution :

Step 1 : In the search page you click the first product(asus zenfone 5)

Step 2 : You will be redirected to the detail page of the product you selected here after selecting the features like color or storage the page will reload and after that get the page title it will be like ex : Asus Zenfone 5 Price in India - Buy Asus Zenfone 5 Red 8 Online - Asus : Flipkart.com and other features (like color, storage and processor selected),seller name

Step 3 : click add to cart button and go to the cart page

Step 4 : In the cart page verify whether all the features are present for checking the name the only thing you can do is click on the image of the product and open it in a new tab and then again get the title and compare it with the previous title which we got before adding the product to the cart.Thus you can verify that you have added the correct product.

You can also keep the another check to verify whether the product added is same like get the product id of the product before adding it to the cart and in the cart page get the pid attribute from the url and compare both of them

Hope this helps you...Kindly get back if you have any doubts

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manu
Solution 2
Solution 3