Automate 'Buy Product' from E-commerce Website using Selenium

In this assignment, you will learn to automate end-to-end Buy Product functionality of an e-commerce website. While automating this assignment, you will encounter many advanced Selenium Webdriver commands. That's why this assignment is rated as 'Expert' Level. We will cover multiple  scenarios in this assignment.

Automation Level - Expert

Test Case 1 - Automate End to End Buy Order functionality.

Steps to Automate:
2. Login to the website.
3. Move your cursor over Women's link.
4. Click on sub menu 'T-shirts'.
5. Mouse hover on the second product displayed.
6. 'More' button will be displayed, click on 'More' button.
7. Increase quantity to 2.
8. Select size 'L'
9. Select color.
10. Click 'Add to Cart' button.
11. Click 'Proceed to checkout' button.
12. Complete the buy order process till payment.
13. Make sure that Product is ordered.

If you find it difficult to automate above steps, then follow the reference link or you can also view the code for ordering product below in this post.

Reference Links for Solution:

Automation Code for Buying Product: Following code is contributed by Uday.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class EcomExpert {

 public static void main(String[] args){

  WebDriver driver=new ChromeDriver();
  String URL="";
  // Open URL and Maximize browser window
  driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);

  //Click on Sign in
  driver.findElement(By.linkText("Sign in")).click();
  //Click on Women

  WebElement SecondImg=driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div[2]/ul/li[2]/div/div[1]/div/a[1]/img"));
  WebElement MoreBtn=driver.findElement(By.xpath("/html/body[1]/div[1]/div[2]/div[1]/div[3]/div[2]/ul/li[2]/div[1]/div[2]/div[2]/a[2]"));
  Actions actions=new Actions(driver);

  //Change quantity by 2

  //Select size as L
  WebElement Sizedrpdwn=driver.findElement(By.xpath("//*[@id='group_1']"));
  Select oSelect=new Select(Sizedrpdwn);

  //Select Color

  //Click on add to cart
  driver.findElement(By.xpath("//p[@id='add_to_cart']//span[.='Add to cart']")).click();

  //Click on proceed
  driver.findElement(By.xpath("/html//div[@id='layer_cart']//a[@title='Proceed to checkout']/span")).click();
  //Checkout page Proceed
  //Agree terms&Conditions

  //Click on Payby Check
  //Confirm the order

  //Get Text
  String ConfirmationText=driver.findElement(By.xpath("//div[@id='center_column']/p[@class='alert alert-success']")).getText();
  // Verify that Product is ordered
  if(ConfirmationText.contains("complete")) {
   System.out.println("Order Completed: Test Case Passed");
  else {
   System.out.println("Order Not Successfull: Test Case Failed");


Test Case 2 - Verify that 'Add to Wishlist' only works after login.
Steps to Automate:
2. Move your cursor over Women's link.
3. Click on sub menu 'T-shirts'.
4. Mouse hover on the second product displayed.
5. 'Add to Wishlist' will appear on the bottom of that product, click on it.
6. Verify that error message is displayed 'You must be logged in to manage your wishlist.'

Test Case 3 - Verify that Total Price is reflecting correctly if user changes quantity on 'Shopping Cart Summary' Page.
Steps to Automate:
2. Login to the website.
3. Move your cursor over Women's link.
4. Click on sub menu 'T-shirts'.
5. Mouse hover on the second product displayed.
6. 'More' button will be displayed, click on 'More' button.
7. Make sure quantity is set to 1.
8. Select size 'M'
9. Select color of your choice.
10. Click 'Add to Cart' button.
11. Click 'Proceed to checkout' button.
12. Change the quantity to 2.
13. Verify that Total price is changing and reflecting correct price.

Similar way you can add few more test cases. Happy Learning!

Assignment 5 - Automate User Registration << Previous  |  Next  >> Assignment 7 - Automate Search Product

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.


  1. Please send me Selenium Script of all Assignments.
    At my mail ID


  2. Hi, Could you please tell me how to switch to alerts or windows in this assignment. After "Add To Cart", there is a window pop-up/alert/frame in which we have to click on "Proceed to Checkout". Unable to switch windows here. Please help.


Post a Comment

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on using Selenium

Automate E-Commerce Website using Selenium Webdriver