Where can I find a full test automation project with Selenium WebDriver?

You have been learning test automation with Selenium.

You went through lots of concepts and would like to see how they all work together.

Where can you find a project that uses everything you learned and more?

Here 🙂

What follows is a small project that I built a while ago for a job interview.

It uses many test automation concepts such as:

  • page factory
  • base classes
  • html classes
  • test listeners
  • test ng assertions and fixtures
  • annotations
  • custom locators (javascript and jquery)
  • screenshots
  • saving errors in text files

The exercise consisted in automating the following test case with Java and Selenium WebDriver:

  • Launch bestbuy url (www.bestbuy.ca)
  • Search a product and add it to cart
  • Go all the way through checkout process and place the order with invalid credit card
  • Capture the error message due to invalid credit card

Before downloading the project and checking the source code, a few details about the project.

Project details

Maven project
– all dependencies are managed through the pom.xml file

 Test NG

– unit testing library

Java JDK 8

– used for lambda expressions and streams

Page Factory

– pattern for creating page object and page fragment classes

– the elements of page object/fragment classes have names and locators

– names and locators are implemented using annotations

– available locator types are id, xpath, css, name and javascript

  
   @Name("SEARCH_HEADER")
   @FindBy(className = "main-navigation-container") 
   public class SearchHeader extends HtmlElement{ 
 
   @Name("SEARCH_FIELD")
   @FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_TxtSearchKeyword") 
   private TextInput searchKeywordTxt;
 
   @Name("SEARCH_BUTTON")
   @FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_BtnSubmitSearch")
   private Button searchBtn; 
     
   public void search(String keyword) {
     searchKeywordTxt.click();  
     searchKeywordTxt.clear();
     searchKeywordTxt.sendKeys(keyword);
     searchBtn.click();  
   }
}

 

The project has the automation framework classes in the main folder and all test items in the test folder.

Main folder (framework classes)

annotations classes
– FindBy
– FindByJQUERY
– FindByJS
– Name
– Timeout
 
 package com.bestbuy.demo.annotations;

 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;

 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE, ElementType.FIELD})
 public @interface Name {
   String value();
 }

 

html element classes
 
    package com.bestbuy.demo.element;

    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebElement;

    public class CheckBox extends TypifiedElement {
   
      public CheckBox(WebElement wrappedElement) {
        super(wrappedElement);
      }

      public WebElement getLabel() {
        try {
            return getWrappedElement().findElement(By.xpath("following-sibling::label"));
        } catch (NoSuchElementException e) {
            return null;
        }
      }

      public String getLabelText() {
        WebElement label = getLabel();
        return label == null ? null : label.getText();
      }

      public String getText() {
        return getLabelText();
      }

      public void select() {
        if (!isSelected()) 
            getWrappedElement().click();        
      }

      public void deselect() {
        if (isSelected()) 
            getWrappedElement().click();
      }

      public void set(boolean value) {
        if (value) 
           select();
        else 
           deselect();
      }
   }

exceptions classes

 decorator and proxy classes used by the page factory

page class

– used as base class for the page object classes

 page factory classes

miscellaneous classes such as

– custom driver class, screenshot class
– enumerations (used to avoid hardcoding data in the page objects)
– simple logger class
– property class
– TextFile class

 

Test folder (test items)

    • base test class (used as base by the test classes)
    • page objects classes (all page object and page fragment classes)
    • test listeners (class for taking a screenshot and logging exceptions in case of failures)
 
 package com.bestbuy.demotests.testlisteners;

 import java.lang.reflect.Field;
 import org.testng.ITestContext;
 import org.testng.ITestListener;
 import org.testng.ITestResult;
 import com.bestbuy.demo.exceptions.HtmlElementsException;
 import com.bestbuy.demo.utils.Driver.BrowserDriver;
 import com.bestbuy.demo.utils.Driver.Screenshot;
 import org.openqa.selenium.WebDriver;
 import static com.bestbuy.demotests.BaseTest.BaseTestClass.*;

 public class TestListener implements ITestListener {   
   
    @Override
    public void onTestFailure(ITestResult result) {       
       try {
         Screenshot screenshot = 
         new Screenshot(getDriverFromBaseTest(result));
   
         screenshot.capture(result.getName());
       } 
       catch (Exception ex) {
         throw new HtmlElementsException(ex.getMessage());
       }                 
   }  

   @SuppressWarnings("unchecked")
   private WebDriver getDriverFromBaseTest(ITestResult result) 
       throws IllegalAccessException {
   
      WebDriver driver = null;
   
      try { 
         Class< ? extends ITestResult> testClass = 
         (Class< ? extends ITestResult>) result.getInstance().getClass();
  
         Class< ?extends ITestResult> baseTestClass = 
         (Class< ? extends ITestResult>) testClass.getSuperclass();
  
         Field driverField = baseTestClass.getDeclaredField("driver");
    
         driver = (BrowserDriver)driverField.get(result.getInstance()); 
    
         return driver;
     } 
     catch (SecurityException | NoSuchFieldException | IllegalArgumentException ex) {     
         throw new HtmlElementsException("error getting the driver from base test");    
     }         
   
   }
    
   @Override
   public void onTestSuccess(ITestResult result) 
   {}  
  
   @Override
   public void onTestSkipped(ITestResult result) 
   {} 
  
   @Override
   public void onTestFailedButWithinSuccessPercentage(ITestResult result) 
   {} 
  
   @Override
   public void onStart(ITestContext context)     
   {} 
  
   @Override 
   public void onFinish(ITestContext context)    
   {}
  
   @Override
   public void onTestStart(ITestResult arg0)
   {}
    
 }
  • test class

Most interactions with web elements are done through the page factory classes.

Occasionally, Javascript code is used when buttons cannot be clicked through the page factory.

Some of the pages have random popups that are closed if displayed.

The test script does the following:

  1. Open the home page
  2. Execute a keyword search
  3. Select the Online Only filter
  4. Select a product with online availability
  5. Add the product to the cart
  6. Continue checkout on the basket page
  7. Select New Member
  8. Fill in the Ship To info
  9. Select credit card as payment method
  10. Fill in the payment info
  11. Submits transaction
  12. Verify that there is at least an error message
  13. Saves the error messages and the search keyword to a text file

The test script uses a keyword parameter which can take multiple values.
Download source code

Interested in learning how to build a project like this one?

Get started here

Advertisements

5 Comments Add yours

  1. P says:

    could you please fix the link for your project. actually it would be better if you could upload your code on github and share link here

    Like

    1. Alex Siminiuc says:

      please try again.
      the link should work now.

      Like

  2. Sam says:

    Hello sir,
    Thanks for these great series of blogs on Selenium wedriver with Java. I trid to download the source code by clicking the provided link, however, it didn’t work.
    Could you please fix the link so I can download the source code of the project.
    Thanks
    Sam

    Like

    1. Alex Siminiuc says:

      please try again.
      the link should work now.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s