Creating your First Proper Test

Search for “Reddit” on DuckDuckGo and Select a Search Result

Creating the Cucumber Scenario

When creating our Cucumber scenarios, we want to keep them simple and write them in a declarative style, sticking simply to the business logic, rather than writing them in an imperative style.  A great article on this can be found at http://itsadeliverything.com/declarative-vs-imperative-gherkin-scenarios-for-cucumber.

  1. Right-click on the src/test/resources/features directory, select ‘New’ –> ‘File’ and create a file called ‘SearchScenarios.feature’
  2. Open up the ‘SearchScenarios.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)

    If you’ve been following all the blog posts in this series, you will notice that we have reused some existing steps in this scenario, such as;
    “Given I am on the search page”

    The other undefined step definitions will be highlighted as being undefined still.

Creating the Undefined Step Definitions

  1. Highlight some of the ‘When I search for “<string>”‘ step in IntelliJ until a yellow lightbulb icon appears next to it
  2. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘SendKeysSteps (steps)’
    • An undefined step definition should be automatically added to ‘SendKeysSteps’ like below…
      • If ‘SendKeysSteps (steps)’ is not listed, choose one that is listed then cut and paste the step definition in to ‘SendKeySteps’ class
      • Also rename the String argument to something suitable (e.g. ‘searchTerm’)

  3. Highlight some of the ‘And I view the first result’ step in IntelliJ until a yellow lightbulb icon appears next to it
  4. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘ReturnClickSteps (steps)’
    • An undefined step definition should be automatically added to ‘ReturnClickSteps’ like below…
      • If ‘ReturnClickSteps (steps)’ is not listed, choose one that is listed then cut and paste the step definition in to ‘ReturnClickSteps’ class

  5. Highlight some of the ‘Then I see the Reddit homepage’ step in IntelliJ until a yellow lightbulb icon appears next to it
  6. Click the yellow lightbulb icon and select ‘Create step definition’ –> ‘ValidationSteps (steps)’
    • An undefined step definition should be automatically added to ‘ValidationSteps’ like below…
      • If ‘ValidationSteps (steps)’ is not listed, choose one that is listed then cut and paste the step definition in to the ‘ValidationSteps’ class

Creating the Test Methods

We will now create test methods in Java for each of our steps to use.

Because we are taken to a new ‘search results’ page when we submit a search on Google, we should create a new page class for the search results page.

  1. Right-click on the ‘pages’ package, select ‘New’ –> ‘Java Class’ and create a class file called ‘SearchResultsPage’
  2. Open up the ‘SearchResultsPage’ class and edit it so it looks like below (we want it to inherit from BasePage, as that is where we navigate to the page from when we conduct our search on the ‘BasePage’)…
  3. Open up the ‘BasePage’ class in the ‘pages’ package and add the following WebElement at the top of the class…

    We will use this element in our test method for sending keys to the ‘searchField’. If you right-click on the search field in Chrome web browser and select ‘Inspect Element’, you can see that it has the value of ‘q’ given for its name, this is what we use to find the element in our test framework.

    Also make sure that IntelliJ has added the correct imports…

  4. Add the following test method within the ‘BasePage’ class…

    The above method clears the ‘searchField’ of any text, and then sends the passed string to the ‘searchField’, presses the Return key to submit the search, and then returns an instance of the ‘SearchResultsPage’ class after submission.
    Let’s now add a method to assert that the search results have been returned.

  5. Open up the ‘SearchResultsPage’ class in the ‘pages’ package and add the following WebElement at the top of the class…

    We will use this element in our test method for checking that the search results content area is displayed, which is an element specific to the SearchResultsPage (e.g. it does not appear on the BasePage). If you right-click on the search field in Chrome web browser and select ‘Inspect Element’, you can see that it has the value of ‘search’ given for its ID, this is what we use to find the element in our test framework.

    Also make sure that IntelliJ has added the correct imports…

  6.  Add the following method within the ‘SearchResultsPage’ class…

    Because we are taken to a new ‘Reddit’ page after selecting the appropriate search result, we should also create another new page class for the ‘Reddit’ page. Because when we click on a search result, we are taken to another page, this would count as a ‘ReturnClick’ method, due to a new page object being returned.

  7. Right-click on the ‘pages’ package, select ‘New’ –> ‘Java Class’ and create a class file called ‘RedditPage’
  8. Open up the ‘RedditPage’ class and edit it so it looks like below (we want it to inherit from ‘SearchResultsPage’ class, as that is where we navigate to the page from when we click through on our search result)…

  9. Open up the ‘SearchResultsPage’ class in the ‘pages’ package and add the following WebElement at the top of the class…

    If we inspect the element of the first listed result, we can see that every search result link returned by DuckDuckGo falls within an <a> anchor tag with a class name of “result__a”.  In our @FindBy annotation, we can choose to find a List of all WebElements that meet our criteria of having a className of “result__a”.  Also make sure that has IntelliJ added the correct import of  import java.util.List

    We will use this element in our test method for clicking the link in the searchResult at Index 0 within this list of search results.

  10. Add the following test method within the ‘SearchResultsPage’ class…

  11. Because we will want to use both the ‘selectFirstListedSearchResult()’ and ‘assertSearchResultsDisplayed()’ methods in our ‘And I view the first result’ step, we can make both these methods private to the class and then make another public method that calls them, like below…

  12. Next, we can assert that we see the Reddit homepage by making a method which calls all of the base validation methods we made in the previous section.  To help with timeouts, we will also need to add a WebElement for the redditContentArea and assert that is displayed before asserting the rest. Open up the ‘RedditPage’ class again and add the following…

Connecting the Step Definitions to the Test methods

We will now call the test methods from their appropriate step definitions.

  1. Open up the ‘SendKeysSteps’ class again, and refactor the ‘When I search for “<string>”‘ step so it initialises the page (in this case ‘BasePage’) with the correct web elements rendered and then calls the searchFor() method, like below…
  2. Open up the ‘ReturnClickSteps’ class again, and refactor the ‘And I view the first result’ step so it initialises the page (in this case ‘SearchResultsPage’) with the correct web elements and then calls the viewFirstResult() method, like below…
  3. Open up the ‘ValidationSteps’ class again, and refactor the ‘Then I see the Reddit homepage’ step so it initialises the Reddit page with the correct web elements and then class the assertIAmOnRedditPage() method, like below…

Running the Test Scenario

  1. Right-click on the scenario in the .feature file and click run, it should pass 🙂

In the next section, we will add extension/helper methods for actions we will perform a lot, e.g. clicking, sending text input…etc.

 

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.