This overview will go over creating an end-to-end test for searching for a movie on RottenTomatoes.com.

Pre-Requisites

👍 This guide is helpful for getting ChromeDriver set up as an environment variable in Windows.

First Time Setup

  • Click on "Configure" in the bottom corner of the start window
  • Click Plugins

From there,
  • Search for both Gherkin and Cucumber plugins.
  • Install them
  • Restart IntelliJ

Project Setup

Once you have the pre-requisites prepared,
  • Click on Create a New Project.
  • Select Maven on the sidebar,
  • Select your Java version
  • Click next.

  • Name your project
  • Set the Location where you want to store your project locally
  • Click 'Finish' to create your project

Dependencies

IntelliJ will generate and open a barebones pom.xml file where you will add your dependencies, properties, and other necessities for this project.

Cucumber Java Skeleton is helpful for building out similar projects.
  • View the pom.xml file listed
  • Copy the properties and dependencies tags to your pom.xml file.

For this project, you will be using TestNG instead of JUnit so

From there
  • Grab the latest stable version
  • Insert the dependency into our pom.xml.

You will also be replacing all instances of JUnit in our pom.xml as well, updating the version information in the properties section.

To do this, you will:

You will need to edit this further:
  • Updating your Java Version
  • Add the Maven compiler source and compiler target.

So far, your pom.xml should look like so:

  • Save your pom.xml file with Ctrl+S.

Setting Up Selenium and Chrome Driver

  • Head back to Maven Repository
  • Search for 'Selenium Java'
  • Copy the latest stable version
  • Note: Ensure you are copying the Maven option
  • Paste the dependency into your pom.xml file.

  • You will need to repeat this for 'Selenium Chrome Driver' as well

After you have added the last two dependencies to the pom.xml file
  • save it once again with Ctrl+S.

On the right-hand side of our IDE,
  • Click Maven Projects tab to show the Maven lifecycle.
  • Click re-import Maven Projects button to update from the POM.

Now that you have created the Maven project and added all the dependencies, it's time to write the first feature file.

Feature Files

A Note About File Structure Projects using Cucumber-JVM should follow Maven's Standard Directory Layout. In a Maven project, feature files belong under /src/test/resources since they are not Java source files.

  • Create a resources directory under /src/test to house your feature files
  • Within the resources folder, right-click and create another directory for your feature files


Finally…
  • Create your feature file by right-clicking on the features folder and clicking "New File". I named my file search.feature for ease of recognition.

Writing the Feature File

The purpose of a feature file is to provide a high-level description of a feature and to group related scenarios

Each scenario is a concrete example that illustrates general acceptance criteria.

For this example, I created a feature file that searches for a specific movie on RottenTomatoes.com and tests that it was able to successfully find the correct movie.

  • Right-click (or Ctrl+Shift+F10) and run your feature file.

You will see that the feature file is running but looking for step definitions that don't currently exist.

From the Run pane at the bottom of IntelliJ, it will suggest some boiler-plate code for you.
  • Select the code and copy it to the clipboard. You'll paste it into a new file step definitions file soon.

Step Definitions

Step definition classes are Java classes containing methods that implement Gherkin steps.

I created a new package under /java/ named "RT" since that is the project I'm currently working on.y

Inside of that package, create your step definitions file by:
  • Right-click on the package and select "New Java Class"

  • Copy the step definitions from the previous run's output and paste them into the class

  • Create private variables to be used throughout
  • Creating a new ChromeDriver instance as well as initializing WebDriverWait .

Using Robust Locators

Robust locators
  • work after you change the properties of a UI element
  • are small and as simple as possible
  • work after you change the UI elements around the element you're targeting
A good selenium locator is short, easily readable, and doesn't need to be updated if the location of the element in the page changes.

👍 For more information about choosing the correct locator for the job, this is a helpful README.

Going forward, I'll start covering each test method.

➡️ Given A User Is on the Homepage

This includes the setup of the WebDriver as well as getting the page title of the website to which I navigated.

I am also asserting that we went to the website we intended.

➡️ When A User Searches for a Movie

Here I am finding the search bar by class name since the ID is also used by another element on the homepage.


Afterwards, I
  • send the keyword I'm searching for and
  • send the enter button.

➡️ And A User Selects the Correct Listing from the Search Page

This is a simple snippet of code where I
  • Wait until the link is clickable and
  • Select the specific link we want

➡️ Then A User Should Be Taken to the Correct Movie Page

With this, I am simply
  • Saving the page title as a string, and then
  • Asserting it is equal to the movie listing we wanted.

Creating the TestRunner

You need to add a final class to allow Maven to hook into Cucumber.
  • Create a new class inside src/test/java package named TestRunner

Differences between JUnit and TestNG in the TestRunner

@RunWith(ExtendedCucumber.class) is used for JUnit

AbstractTestNGCucumberTests is used for TestNG where you extend your TestRunner class

Setting up @CucumberOptions:

  • points to where our feature files are stored, as well as
  • the glue, which is simply the package where our step definitions are stored.

Unlike JUnit, there is no need to specify the test runner explicitly as by default AbstractTestNGCucumberTests class will use TestNG to execute the scenarios.

Here is also where you set up your reporting features.

Using IntelliJ, you can also set your glue and features path using the Run → Edit Configurations menu.

Reporting

There are several reporting plugins built into Cucumber. I've used pretty here and modified monochrome.

Pretty reporting is a more verbose output of the default reporting.
When monochrome reporting is set to true, the output of the will be much more readable and well-formatted as opposed to leaving monochrome defaulted to false.

I also set the location of where the reports will show up.

There are many formats available for generating reports, such as HTML and JSON.
You can also generate all of the available reports at once, so you are not limited to just one reporting option.

Running the tests

From here, I'll be checking that everything works as I expected by
  • Right-clicking the green checkmark button and clicking Run 'TestRunner'
  • or using Ctrl-Shift-F10

And here is the generated HTML report with a successful test!

Further reading…

More information on parallel execution, hooks, checking assertions, as well as other helpful cucumber documentation, can be found at cucumber.io.


I am by no means an expert on any of these topics discussed here but I hope that this goes over what I've learned adequately. 💜
Topic revision: r9 - 29 Jul 2020, KaylaGilbert
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co