cucumber-JVM and Page Object

Context

This post is a quick tutorial that focuses on using Page Object framework using Selenium Java. Please skip this post if you are a beginner and trying to learn Selenium Java with Cucumber jvm. This page is a quick tutorial for the impatient.

In our previous post, we talked about how we can use Selenium with cucumber jvm. That was the first project. However most of us would want to incorporate it into your real time projects where you might already be using tools like selenium java, TestNG, frameworks like page-object/Page Object model etc.This section we are going to see how

  1. Write some page objects (or use existing ones if you already use them in your projects)
  2. Define Modules/keywords (blocks of functionality that are named to make sense)
  3. Data read from an excel workbook (data-driven frameworks context)
  4. Tie it all together in a cucumber-JVM BDD framework.

The project code base is in Github to checkout: https://github.com/machzqcq/cucumber-jvm-template

Agenda

  1. Write a simple Cucumber scenario that logs in and logs out from an e-commerce website
  2. Write Page Objects for the pages we touch in the above workflow
  3. Write modules/keywords that perform a series of actions/operations
  4. Read Data from an excel workbook (or populate data into a HashMap, that is globally available)
  5. Write Step definitions that call the modules (in turn use page objects and data from excel workbook)
  6. Execute and watch the results

If you are unclear about the terms Page Object, data-driven, keywords or modules, click those words to get a context. The project looks like this when imported into eclipse

projectstructure

1) Write cucumber scenario

We will create a ShoppingCart.feature under ‘src/test/resources’ folder and have the below steps.

2) Write Page Objects

Now lets write Page Objects for the pages we will navigate in our scenario. They will be HomePage and LoginPage and a BaseClass [We are following pretty much the same standards what most Page Object frameworks out there that use Selenium java do, no major changes]

src/test/java/pageObjects/AutomationHomePage.java

 

src/test/java/pageObjects/LoginPage.java

src/test/java/pageObjects/BaseClass.java

3) Modules/Keywords

Lets define two keywords, one that does SignInAction and other does SignOutAction. We will describe the individual operations of clicking the sign in link, typing username password etc inside the action classes.

src/test/java/modules/SignInAction.java

src/test/java/modules/SignoutAction.java

4)Read Data from excel workbook

We will read the excel workbook that is in ‘src/test/resources/testData/TestData-seleniumframework.xlsx‘ and return a List of HashMaps [The HashMap keys are the column names in the first row and the values are each row values, so that we can iterate through data sets]. In the below example, I am not using it , however it is relatively easy for anyone who has been in the Java space. All the logic for parsing the workbook and returning is defined in ‘src/test/java/helpers/DataHelper.java

5)Lets write step definitions now

The logic is the same as in my previous post. We get the driver from Hooks class and then use that driver to perform the actions, in this case SignInAction and SignoutAction

src/test/java/step_definitions/ShoppingCart.java

Now its time to execute isn’t it!

5)Execute and watch the results

Right click on the scenario and Run as – Cucumber feature

execute_cucumber_feature

results_eclipse

Command line output would look something like this. Some more options on how to pass command line parameters etc. is explained in the video on previous post.

results_cmd

Closing Thoughts

This post was written with the sole intention of giving you the information in the fastest possible manner. If there is some context missing, please write to me and I will ensure to write up a more detailed example post.

Cheers !