This Is My Self-Mini Project of Web Automation Testing Using Java, Selenium, and Cucumber

Project Information

For this project, i use:

Java Programming Language

Java is the #1 programming language and development platform. It reduces costs, shortens development timeframes, drives innovation, and improves application services. With millions of developers running more than 51 billion Java Virtual Machines worldwide, Java continues to be the development platform of choice for enterprises and developers.

Selenium Framework

Selenium is a portable framework for testing web applications. Selenium provides a playback tool for authoring functional tests without the need to learn a test scripting language.

Cucumber Software Tool

Cucumber is a software tool that supports behavior-driven development. Central to the Cucumber BDD approach is its ordinary language parser called Gherkin. It allows expected software behaviors to be specified in a logical language that customers can understand.

JUnit

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

Eclipse IDE

The Eclipse IDE is famous for our Java Integrated Development Environment (IDE), but we have a number of pretty cool IDEs, including our C/C++ IDE, JavaScript/TypeScript IDE, PHP IDE, and more. You can easily combine multiple languages support and other features into any of our default packages, and the Eclipse Marketplace allows for virtually unlimited customization and extension.

Preparation

Clone Repo

Test Structure

  • Use Gherkin syntax: a set of special keywords to give structure and meaning to executable specifications. Each keyword is translated to many spoken languages; in this reference we’ll use English. Either spaces or tabs may be used for indentation. The recommended indentation level is two spaces. Here is an example:

    Feature: Login page feature admin-demo.nopcommerce.com
    
    Scenario: Successful Login with Valid Credentials
      Given User opens URL "https://admin-demo.nopcommerce.com/login"
      When User enters Email as "admin@yourstore.com" and Password as "admin"
      And Clicks on Login button
      Then Page title should be "Dashboard / nopCommerce administration"
      When User clicks on Logout link
      Then Page title should be "Your store. Login"
    
  • Use Step Definition is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute. To illustrate how this works, look at the following Gherkin Scenario step definition:

    package stepdefinitions;
    
    import org.junit.Assert;
    
    import factory.DriverFactory;
    import io.cucumber.java.en.Given;
    import io.cucumber.java.en.Then;
    import io.cucumber.java.en.When;
    import pages.LoginPage;
    
    public class LoginSteps {	
      private LoginPage loginPage = new LoginPage(DriverFactory.getDriver());
    
      @Given("User opens URL {string}")
      public void user_opens_url(String url) {
        DriverFactory.getDriver().get(url);	    
      }
    
      @When("User enters Email as {string} and Password as {string}")
      public void user_enters_email_as_and_password_as(String email, String password) {
          loginPage.enterEmail(email);
          loginPage.enterPassword(password);
      }
    
      @When("Clicks on Login button")
      public void click_on_login_button() {
        loginPage.clickLoginButton();
      }
    
      @Then("Page title should be {string}")
      public void page_title_should_be(String expectedTitle) {	    
        if(DriverFactory.getDriver().getPageSource().contains("Login was unsuccessful.")) {
          DriverFactory.getDriver().close();
          Assert.assertTrue(false);
        } else {
          Assert.assertEquals(expectedTitle, DriverFactory.getDriver().getTitle());
        }		
      }
    
      @When("User clicks on Logout link")
      public void user_clicks_on_logout_link() {
        loginPage.clickLogoutLink();
      }
    }
    
  • Use TestRunner class. To run your BDD test, you must use one of the available runners. In this tutorial, we will use JUnit, but you can use any other.

    • Create a new JUnit class And Add the following annotations to the class:
    package TestRunners;
    
    import org.junit.runner.RunWith;
    
    import io.cucumber.junit.Cucumber;
    import io.cucumber.junit.CucumberOptions;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
        features = {"src/test/resources/features/"},
        glue = {"hooks", "stepdefinitions"},
        plugin = {"pretty"},
        dryRun = false,
        monochrome = true
        )
    public class TestRunner {
    
    }
    

Run Test

In Eclipse IDE, run your test and generating Cucumber Report at once, you can hover your cursor to TestRunner.java file, then right-click, click Run As 1 JUnit Test. Screenshot_1

The Test will run

Screenshot_3

Test Report by Cucumber Report

After run the test, you can also see the result of the test by copy-paste the link in the console. Screenshot_4

Test Report Sample

Screenshot_7 The result will be like this.

All is Done!