Creating the Base Scenarios

Validate the Title of a Website

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario. We can also add a Cucumber tag either above the ‘Scenario’ (to apply to the specific scenario) or above the ‘Feature’ (to apply to all scenarios within the feature)…
    @Chrome 
    Feature: BaseScenarios 
    
    These scenarios can be used in any project 
    
    Scenario: 01. Validate the title of a website 
        Given a DuckDuckGo user is on the base page 
        Then they see the page title contains "DuckDuckGo"

    You should notice that the ‘Given’ and ‘Then’ lines are highlighted.  This is because they do not yet have any step definitions attached to them.

Creating the Undefined Step Definitions

  1. Open up CMD / Terminal and ‘cd’ (change directory) in to the root of your Maven or Gradle project
  2. Enter in
    $ mvn install

    (without the $) or

    ./gradlew build
    • The build might give an error but that’s fine for now
  3. Scroll up in the CMD / Terminal and you will be able to see the undefined step definitions
  4. Copy and paste the undefined ‘Given’ step definition in to the ‘BaseSteps’ class, and tweak it slightly with RegEx so it accepts variations, like below…
    package steps;
    
    import cucumber.api.PendingException;
    import io.cucumber.java.en.Given
    public class BaseSteps extends Page {
    
        @Given("^(?:a DuckDuckGo user|an internet user) is on the base page")
        public void userIsOnTheBasePage() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }
    }
  5. Because our base page is also the search page, let’s slightly tweak our undefined ‘Given’ step definition so that we can use RegEx to either use the word ‘base’ or ‘search’ when referring to the step and being on the page, without capturing the match (see https://agileforall.com/just-enough-regular-expressions-for-cucumber/ for more info…
    package steps;
    
    import cucumber.api.PendingException;
    import io.cucumber.java.en.Given;
    import pages.Page;
    
    public class BaseSteps extends Page {
    
        @Given("^(?:a DuckDuckGo user|an internet user) is on the (?:base|search) page$")
        public void userIsOnTheBasePage() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        }
    }
  6. Copy and paste the undefined ‘Then’ step definition in to the ‘BaseSteps’ class, it should end up looking like below…
    package steps;
    
    import cucumber.api.PendingException;
    import io.cucumber.java.en.Then;
    import pages.Page;
    
    public class BaseSteps extends Page {
    
        @Then("^they see the page title contains \"([^\"]*)\"$")
        public void they_see_the_page_title_contains(String expectedTitle) throws Throwable {
            // Write code here that turns the phrase above into concrete actions 
            throw new PendingException();
        }
    }

Creating the Test Methods

We will now create test methods in Java for each of our steps 🙂

  1. Open up the ‘BasePage’ class in the ‘pages’ package and add the following method to navigate to the Base URL (as defined in your ‘Settings’ class file. Also make sure the import to ‘utils.selenium.Settings is added’ above the class)…
    public void navigateToBaseUrl() {
       String baseUrl = Settings.baseUrl;
       browser().navigate().to(baseUrl);
       System.out.println("Welcome to Product - Selenium Automation Framework");
    }
  2. Also add another method for asserting the page title contains the expected string…
    public void validatePageTitle(String expectedTitle) {
        Assert.assertTrue(getTitle().contains(expectedTitle));
        System.out.println(":: The title of the site is: " + getTitle());
    }

    For the above to work, you will also need to add a getTitle() method near the top of the class, which returns driver.getTitle() …

    private String getTitle() { return driver.getTitle(); }

Additionally, make sure the class contains the following imports…

import static utils.selenium.Driver.browser;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import utils.selenium.Settings;

Connecting the Step Definitions to the Test Methods

We will now call the test methods from their appropriate step definitions. This whole POM approach is nice as we have suitable layers of abstraction, which helps the code be easier to read.  It also allows the step definitions to simply contain one line of code calling the test methods.

Due to the method we created earlier in the ‘Page’ class, where we made a generic method called instanceOf<T>() that takes the same generic class and initialises a new object from PageFactory with the correct elements for the page, we can now use that when calling our test methods 🙂

  1. Open up the ‘BaseSteps’ class again, and refactor the ‘Given’ step definition so it initialises the page (in this case ‘BasePage’) with the correct elements and then calls the navigateBaseUrl() method, like below…
    @Given("^(?:a DuckDuckGo user|an internet user) is on the (?:base|search) page$")
    public void userIsOnTheBasePage() {
        instanceOf(BasePage.class).navigateToBaseUrl();
    }
  2. Open up the ‘BaseSteps’ class again, and refactor the ‘Then’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the validatePageTitle() method, like below…
    @Then("^they see the page title contains \"([^\"]*)\"$")
    public void they_see_the_page_title_contains(String expectedTitle) {
        instanceOf(BasePage.class).validatePageTitle(expectedTitle);
    }

Again, make sure the step definition classes have the correct imports for the Cucumber step and the BasePage.

Validate the URL of a Website

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…
    Scenario: 02. Validate the Url of the website
        Given a DuckDuckGo user is on the base page
        Then they see the page Url contains "https://start.duckduckgo.com/"

    You should notice that the  ‘Then’ line is highlighted.  This is because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Highlight some of the undefined ‘Then’ line in IntelliJ until a yellow lightbulb icon appears next to it
  2. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘BaseSteps (steps)’
    • An undefined step definition should be automatically added to ‘BaseSteps’ like below…
      @Then("^they see the page Url contains \"([^\"]*)\"$")
      public void theySeeThePageUrlContains(String expectedUrl) throws Throwable {
          // Write code here that turns the phrase above into concrete actions 
          throw new PendingException();
      }

Creating the Test Method

We will now create the test method in Java for our undefined ‘Then’ step 🙂

  1. Open up the ‘BasePage’ class in the ‘pages’ package and add the following method to assert the page Url contains the expected string…
    public void validatePageUrl(String expectedUrl) {
        Assert.assertTrue(getUrl().contains(expectedUrl));
        System.out.println(":: The page Url is: " + getUrl());
    }

    For the above to work, you will also need to add a public getUrl() method near the top of the class, which returns driver.getUrl() …

    private String getUrl() {return driver.getCurrentUrl(); }

Connecting the Step Definition to the Test Method

  1. Open up the ‘BaseSteps’ class again, and refactor the ‘Then’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the validatePageUrl() method, like below…
    @Then("^they see the page Url contains \"([^\"]*)\"$")
    public void theySeeThePageUrlContains(String expectedUrl) {
        instanceOf(BasePage.class).validatePageUrl(expectedUrl);
    }

Validate a PageSource String on the Website

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…
    Scenario: 03. Validate the PageSource string on the website
        Given a DuckDuckGo user is on the base page
        Then they see "DuckDuckGo" in the PageSource

    You should again notice that the  ‘Then’ line is highlighted.  This is again because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Highlight some of the undefined ‘Then’ line in IntelliJ until a yellow light bulb icon appears next to it
  2. Click the yellow light bulb icon and select ‘Create step definition’ –> ‘BaseSteps (steps)’
    • An undefined step definition should be automatically added to ‘BaseSteps’ like below…
      @Then("^they see "([^"]*)" in the PageSource$")
      public void theySeeInThePageSource(String expectedPageSource) {
          // Write code here that turns the phrase above into concrete actions 
          throw new PendingException();
      }

Creating the Test Method

We will now create the test method in Java for our undefined ‘Then’ step ?

  1. Open up the ‘BasePage’ class in the ‘pages’ package and add the following method to assert the PageSource contains the expected string…
    public void validatePageSource(String expectedPageSource) {
        Assert.assertTrue(getPageSource().contains(expectedPageSource));
        System.out.println(":: The page source is: " + getPageSource());
    }

    For the above to work, you will also need to add a getPageSource() method near the top of the class, which returns driver.getPageSource() …

    private String getPageSource() {return driver.getPageSource(); }

Connecting the Step Definition to the Test Method

  1. Open up the ‘BaseSteps’ class again, and refactor the ‘Then’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the validatePageSource() method, like below…
    @Then("^I see \"([^\"]*)\" in the PageSource$")
    public void iSeeInThePageSource(String expectedText) {
        instanceOf(BasePage.class).validatePageSource(expectedPageSource);
    }

Validate Existence of Multiple Text in PageSource

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…
    Scenario: 04. Validate existence of multiple texts in PageSource
        Given a DuckDuckGo user is on the base page
        Then they see
          | DuckDuckGo    |
          | search engine |
          | track you     |

    You should again notice that only the ‘Then’ line is highlighted.  This is again because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Highlight some of the undefined ‘Then’ line in IntelliJ until a yellow light bulb icon appears next to it
  2. Click the yellow light bulb icon and select ‘Create step definition’ –> ‘BaseSteps (steps)’
    • An undefined step definition should be automatically added to ‘BaseSteps’ like below…
      @Then("^they see$")
      public void theySee() throws Throwable {
          // Write code here that turns the phrase above into concrete actions
          throw new PendingException();
      }
  3. Edit the undefined step definition so it has a List<String> argument that is passed in to it called ‘existsInPageSource’, it should look similar to below…
    @Then("^they see$")
    public void theySee(List<String> existsInPageSource) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

Ensure the correct imports are in the step definition class file again (ensure the java.util.List exists)…

import cucumber.api.PendingException;
import cucumber.api.java.en.Then;
import pages.BasePage;
import java.util.List;

Creating the Test Method

We will now create the test method in Java for our undefined ‘Then’ step

  1. Open up the ‘BasePage’ class in the ‘pages’ package and add the following method to assert the PageSource contains the multiple expected strings…
    public void validateMultipleInPageSource(List<String> table) {
        for (String row : table) {
            Assert.assertTrue(getPageSource().contains(row));
            System.out.println("The text " + row + " is in the PageSource");
        }
    }

Ensure the correct imports are in the BasePage class…

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import utils.selenium.Settings;
import java.util.List;
import static utils.selenium.Driver.browser;

Connecting the Step Definition to the Test Method

  1. Open up the ‘BaseSteps’ class again, and refactor the ‘Then’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the validateMultipleInPageSource() method, like below…
    @Then("^they see$")
    public void theySee(List<String> existsInPageSource) {
        instanceOf(BasePage.class).validateMultipleInPageSource(existsInPageSource);
    }

Cucumber Background Step

  1. If you look at the four base scenarios in your ‘BaseScenarios.feature’ file, you will see that every scenario shares the step of ‘Given I navigate to the base URL’.  Because of this, we can clean up the Feature file a bit by declaring the ‘Given’ step once as a Background step, which will be applied at the beginning of every scenario in the Feature file (see https://github.com/cucumber/cucumber/wiki/Background for more information)…
    @Chrome
    Feature: BaseScenarios
      These scenarios can be used in any project
    
      Background:
        Given a DuckDuckGo user is on the base page
    
      Scenario: 01. Validate the title of a website
        Then they see the page title contains "DuckDuckGo"
    
      Scenario: 02. Validate the Url of the website
        Then they see the page Url contains "https://start.duckduckgo.com"
    
      Scenario: 03. Validate the PageSource string on the website
        Then they see "DuckDuckGo" in the PageSource
    
      Scenario: 04. Validate existence of multiple texts in PageSource
        Then they see
          | DuckDuckGo    |
          | search engine |
          | track you     |

We have now successfully added our BaseScenarios.  In the next part, we will actually add our first proper test of searching for something on Google and then clicking through on a search result 🙂

You can run one of the Base scenarios by right clicking on them in the .feature file and selecting ‘Run Scenario’.

Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Previous Article

Next Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.