cucumber-jvm, selenium and java

BACKGROUND:

So I have been trying to get cucumber-jvm working on the Java side and to have the same awesome experience as one would have with cucumber in Ruby world. I blogged about what cucumber-jvm already.So please read that if you do not know what that is and why should you use it.

That said, I do not think I can present an entire tutorial yet tutorial on cucumber-jvm has been started and posted on the main menu under CUCUMBER. You will find a much slower paced content with images and screenshots there. This is for the impatient (like me)

So what is in this post?

AGENDA:

If you are already a selenium-java developer and have been in that world using JUnit/TestNG/GRID/selenium/Maven/Eclipse, then this post might help you to get started on cucumber-jvm. To get an idea of what cucumber is , please read the Basic Tutorial – set up env section. You don’t have to sweat too much, because it just adds two numbers , that’s all. I am going to make a few assumptions here because this post is really geared towards someone who has been in Java world for quite sometime.

  1. Maven installed on your machine / eclipse plugin
  2. You know how to run a simple selenium script
  3. You know how Junit works.
  4. You know maven lifecycle phases – clean, compile, install, test

Github CodeBase

https://github.com/machzqcq/cucumber-jvm_sample

Import into Eclipse

eclipse_project

Cucumber Scenario:

Let’s write a simple cucumber feature as below (src/test/resources/seleniumframework.feature)

Step Definitions:

Now we will need to write the code behind (src/test/java/step_definitions/SeleniumFramework_stepdefs.java)

1) I would like to have a @Before and @After hooks (cucumber language for pre and post scenario hooks). The @After hook takes the current Scenario as argument and embeds the screenshot of the page if failed. The code is self explanatory.

You can do this by importing the below

2) Lets add the step definition code now

Import statements

3) Actual Step definitions

Runner:

We will use @RunWith from Junit package.  So below is the code in ‘src/test/java/RunCukesTest.java’

  • The plugin value “pretty” is for pretty formatter output
  • The plugin value html:target/cucumber-html-report is the output report
  • I don’t have any tags, but left it open there in case you plan to expand and add more of your tests with @tags

Maven POM.xml

I will be using maven surefire plugin to kick off my Junit tests, because in future I plan to add more capabilities. For now, it is barebones dependency added in maven. The entire pom looks as below

mvn-pom

Execute:

Now open a command prompt and execute using “mvn test”
The scenario should pass and you should see the steps in green as below

mvn_test_cmd

If you wish to run it as JUnit from Eclipse, do as below.

eclipse_luna_junit

Results:

The results were GREEN already in command line, however we might want to have html reports sometimes.

Since Cucumber produces html reports and we mentioned that in RunCukeTests.java with plugin option, the reports get generated under /target/cucumber-html-report‘ folder. They are not aesthetic to look at [at this point], however we will come to that discussion later to make the reports prettier and what our options are locally and online.

html_reports

Closing Thoughts:

Try changing the code so that it fails. And then open the html report and see how an image will be embedded inline.

The codebase is below from github to checkout

https://github.com/machzqcq/cucumber-jvm_sample

There is a lot of potential for cucumber-jvm and this is a very simple example. Think about the possibilities of having automation frameworks merge with cucumber-jvm. I almost wrote codebase to make page-object framework with selenium work with cucumber, however there are some kinks to be resolved and will post it once that is ready.

Let me know your comments

One thought on “cucumber-jvm and Selenium

Comments are closed.