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)…

    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 ‘BaseScenariosSteps’ class, it should end up looking like below…
  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…
  6. Copy and paste the undefined ‘Then’ step definition in to the ‘ValidationSteps’ class, it should end up looking like below…

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)…
  2. Also add another method for asserting the page title contains the expected string…

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

Additionally, make sure the class contains the following imports…

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 ‘BaseScenariosSteps’ 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…
  2. Open up the ‘ValidationSteps’ 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…

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…

    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’ –> ‘ValidationSteps (steps)’
    • An undefined step definition should be automatically added to ‘ValidationSteps’ like below…

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…

    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() …

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps’ 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…

Validate a PageSource String on the Website

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…

    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 lightbulb icon appears next to it
  2. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘ValidationSteps (steps)’
    • An undefined step definition should be automatically added to ‘ValidationSteps’ like below…

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…

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

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps’ 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…

Validate Existence of Multiple Text in PageSource

Creating the Cucumber Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…

    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 lightbulb icon appears next to it
  2. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘ValidationSteps (steps)’
    • An undefined step definition should be automatically added to ‘ValidationSteps’ like below…
  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…

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

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…

Ensure the correct imports are in the BasePage class…

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps’ 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…

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)…

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!

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.