Automate E-Commerce Website using Selenium Webdriver

In this post you will learn to automate different functionalities of e-commerce website. This assignment is rated as 'Pro' level. We will cover multiple test scenarios of e-commerce website in this assignment like., User Registration, Buy Product, Search Product and other website filters using Selenium Webdriver. Along with test assignments, url of the demo e-commerce website is also provided. You can use this demo e-com website for your automation practice.

Assignment 1 - Automate 'User Registration' of an e-commerce website with Selenium

This section is focused on covering all the scenarios for User Registration for an e-commerce website. In this assignments you will learn the different Selenium commands which are used to handle the web form. A web form is generally a collection of web elements like, text boxes, radio buttons, selection box etc. And in web testing, we mostly face web forms and our 90% of testing revolves around web forms. We are going to cover both positive and negative scenarios for User Registration.

Assignment Level - Intermediate

Positive Scenario

Test Case 1 - Automate User Registration Process

Steps to Automate:
2. Click on sign in link.
3. Enter your email address in 'Create and account' section.
4. Click on Create an Account button.
5. Enter your Personal Information, Address and Contact info.
6. Click on Register button.
7. Validate that user is created.

If you find it difficult to automate above steps, then follow the reference link or take a look at the code shared below.

Reference Links for Solution :

Code for User RegistrationFollowing 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.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import io.github.bonigarcia.wdm.WebDriverManager;

public class EcomSignUp {

 public static void main(String[] args) {
 
  WebDriverManager.chromedriver().setup();
  WebDriver driver=new ChromeDriver();
  String URL="http://automationpractice.com/index.php";

  driver.get(URL);
  driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
  driver.manage().window().maximize();
  
  //Click on Sign in
  driver.findElement(By.linkText("Sign in")).click();
  
  //Enter email address
  driver.findElement(By.cssSelector("[name='email_create']")).sendKeys("test1249@test.com");
  
  //Click on "Create an account"
  driver.findElement(By.xpath("//button[@name=\"SubmitCreate\"]")).click();
  
  //Select Title
  driver.findElement(By.xpath("//input[@id=\"id_gender1\"]")).click();
  driver.findElement(By.name("customer_firstname")).sendKeys("Test User");
  driver.findElement(By.name("customer_lastname")).sendKeys("Vsoft");
  driver.findElement(By.id("passwd")).sendKeys("PKR@PKR");
  
  // Enter your address
  driver.findElement(By.id("firstname")).sendKeys("Test User");
  driver.findElement(By.id("lastname")).sendKeys("Vsoft");
  driver.findElement(By.id("company")).sendKeys("Vsoft");
  driver.findElement(By.id("address1")).sendKeys("Test 81/1,2nd cross");
  driver.findElement(By.id("city")).sendKeys("XYZ");
  
  // Select State
  WebElement statedropdown=driver.findElement(By.name("id_state"));
  Select oSelect=new Select(statedropdown);
  oSelect.selectByValue("4");

  driver.findElement(By.name("postcode")).sendKeys("51838");
  
  // Select Country
  WebElement countrydropDown=driver.findElement(By.name("id_country"));
  Select oSelectC=new Select(countrydropDown);
  oSelectC.selectByVisibleText("United States");
  
  //Enter Mobile Number
  driver.findElement(By.id("phone_mobile")).sendKeys("234567890");
  driver.findElement(By.xpath("//input[@name=\"alias\"]")).clear();
  driver.findElement(By.xpath("//input[@name=\"alias\"]")).sendKeys("Office");
  driver.findElement(By.name("submitAccount")).click();
  String userText=driver.findElement(By.xpath("//*[@id=\"header\"]/div[2]/div/div/nav/div[1]/a")).getText();

  // Validate that user has created
  if(userText.contains("Vsoft")) {
   System.out.println("User Verified,Test case Passed");
  }
  else {
   System.out.println("User Verification Failed,Test case Failed");
  }
 }
}


Negative Scenarios
Test Case 2 - Verify invalid email address error.

Steps to Automate:
2. Click on sign in link.
3. Enter invalid email address in the email box and click enter.
4. Validate that an error message is displaying saying "Invalid email address."


Test Case 3 - Verify error messages for mandatory fields.

Steps to Automate:
2. Click on sign in link.
3. Enter email address and click Register button.
4. Leave the mandatory fields (marked with *) blank and click Register button.
5. Verify that error has been displayed for the mandatory fields.


Test Case 4 - Verify error messages for entering incorrect values in fields.

Steps to Automate:
2. Click on sign in link.
3. Enter email address and click Register button.
4. Enter incorrect values in fields like., enter numbers in first and last name, city field etc and enter alphabets in Mobile no, Zip postal code etc., and click on 'Register' button.
5. Verify that error messages fpr respective fields are displaying.
Try automating the above scenarios using Selenium commands, if you face any difficulty please refer the Selenium Tutorial series. Happy Learning!

Assignment 2 - Automate 'Buy Product' functionality of an e-commerce website with Selenium

Most important functionality of an e-commerce website is buying a product, which includes various steps like select product, select size/color, add to cart, checkout etc. You will find every test scenario along with automation code in following section.
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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import io.github.bonigarcia.wdm.WebDriverManager;

public class EcomExpert {

 public static void main(String[] args){

  WebDriverManager.chromedriver().setup();
  WebDriver driver=new ChromeDriver();
  String URL="http://automationpractice.com/index.php";
  
  // Open URL and Maximize browser window
  driver.get(URL);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);

  //Click on Sign in
  driver.findElement(By.linkText("Sign in")).click();
  //Login
  driver.findElement(By.id("email")).sendKeys("test1249@test.com");
  driver.findElement(By.id("passwd")).sendKeys("PKR@PKR");
  driver.findElement(By.id("SubmitLogin")).click();
  //Click on Women
  driver.findElement(By.linkText("WOMEN")).click();

  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);
  actions.moveToElement(SecondImg).moveToElement(MoreBtn).click().perform();

  //Change quantity by 2
  driver.findElement(By.id("quantity_wanted")).clear();
  driver.findElement(By.id("quantity_wanted")).sendKeys("2");

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

  //Select Color
  driver.findElement(By.id("color_11")).click();

  //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
  driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div/p[2]/a[1]/span")).click();
  driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div/form/p/button/span")).click();
  //Agree terms&Conditions
  driver.findElement(By.xpath("//*[@id=\"cgv\"]")).click();
  driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div/div/form/p/button/span")).click();

  //Click on Payby Check
  driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div/div/div[3]/div[2]/div/p/a")).click();
  //Confirm the order
  driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]/div/form/p/button/span")).click();

  //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.

Assignment 3 - Automate 'Search Product' functionality of an e-commerce website

Test Case 1- Automate 'Search Product' Functionality of an e-commerce website with Selenium
Steps to Automate:
2. Move your cursor over Women's link.
3. Click on sub menu 'T-shirts'
4. Get Name/Text of the first product displayed on the page.
5. Now enter the same product name in the search bar present on top of page and click search button.
6. Validate that same product is displayed on searched page with same details which were displayed on T-Shirt's page.

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

Reference Links for Solution :

Automation Code for Product Search: This 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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class EcomPractice2 {
 
 public static void main(String[] args) throws InterruptedException{

  WebDriverManager.chromedriver().setup();
  WebDriver driver=new ChromeDriver();
  String URL="http://automationpractice.com/index.php";

  driver.get(URL);
  driver.manage().window().maximize();
  
  // Initialise Actions class object
  Actions actions=new Actions(driver);
  driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
  WebElement womenTab=driver.findElement(By.linkText("WOMEN"));
  WebElement TshirtTab=driver.findElement(By.xpath("//div[@id='block_top_menu']/ul/li[1]/ul/li[1]/ul//a[@title='T-shirts']"));
  actions.moveToElement(womenTab).moveToElement(TshirtTab).click().perform();
  Thread.sleep(2000);
  
  // Get Product Name
  String ProductName=driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[3]/div[2]/ul[1]/li[1]/div[1]/div[2]/h5[1]/a[1]")).getText();
  System.out.println(ProductName);
  driver.findElement(By.id("search_query_top")).sendKeys(ProductName);
  driver.findElement(By.name("submit_search")).click();
  
  // Get Name of Searched Product
  String SearchResultProductname=driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[3]/div[2]/ul[1]/li[1]/div[1]/div[2]/h5[1]/a[1]")).getText();

  // Verify that correct Product is displaying after search
  if(ProductName.equalsIgnoreCase(SearchResultProductname)) {
   System.out.println("Results Matched;Test Case Passed");
  }else{
   System.out.println("Results NotMatched;Test Case Failed");
  }
  
  // Close the browser
  driver.close();
 }

}

Happy Learning!

Assignment 6 - Automate Google Search << Previous


    Follow Us

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

    Comments

    Popular posts from this blog

    10 Best Demo Websites for Automation Testing Practice

    Automate Browser Actions on Godaddy.com using Selenium

    Automate Amazon Website 'Menu links' using Selenium