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.
- Maven installed on your machine / eclipse plugin
- You know how to run a simple selenium script
- You know how Junit works.
- You know maven lifecycle phases – clean, compile, install, test
Github CodeBase
https://github.com/machzqcq/cucumber-jvm_sample
Import into Eclipse
Cucumber Scenario:
Let’s write a simple cucumber feature as below (src/test/resources/seleniumframework.feature)
1 |
Feature: seleniumframework.com features |
1 2 3 4 |
Scenario: Open seleniumframework website Given I open seleniumframework website And I navigate to ABOUT link Then I print the html |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import cucumber.api.java.After; import cucumber.api.java.Before; @Before public void startDriver() throws MalformedURLException { driver = new FirefoxDriver(); } @After /** * Embed a screenshot in test report if test is marked as failed */ public void quitDriver(Scenario scenario) { if(scenario.isFailed()) { try { scenario.write("Current Page URL is " + driver.getCurrentUrl()); byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); scenario.embed(screenshot, "image/png"); } catch (WebDriverException somePlatformsDontSupportScreenshots) { System.err.println(somePlatformsDontSupportScreenshots.getMessage()); } } driver.quit(); } |
2) Lets add the step definition code now
Import statements
1 2 3 4 |
import cucumber.api.Scenario; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; |
3) Actual Step definitions
1 2 3 4 5 6 7 |
@Given("^I open seleniumframework website$") public void i_open_seleniumframework_website() throws Throwable { // Write code here that turns the phrase above into concrete actions driver.get("http://www.seleniumframework.com"); driver.manage().window().maximize(); } |
1 2 3 4 5 6 7 8 |
@And("^I navigate to ABOUT link$") public void i_navigate_to_ABOUT_link() throws Throwable { WebDriverWait wait = new WebDriverWait(driver,10); wait.ignoring(WebDriverException.class); wait.ignoring(StaleElementReferenceException.class); wait.until(ExpectedConditions.elementToBeClickable(By.linkText("ABOUT"))); driver.findElement(By.linkText("ABOUT")).click(); } |
1 2 3 4 5 6 |
@Then("^I print the html$") public void i_print_the_html() throws Throwable { // Write code here that turns the phrase above into concrete actions System.out.println(driver.getTitle()); } |
Runner:
We will use @RunWith from Junit package. So below is the code in ‘src/test/java/RunCukesTest.java’
1 2 3 4 |
package step_definitions; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; |
1 2 3 4 5 6 7 8 9 |
@RunWith(Cucumber.class) @CucumberOptions( features = "classpath:features", plugin = {"pretty", "html:target/cucumber-html-report"}, tags = {} ) public class RunCukesTest{ } |
- 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
1 |
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
If you wish to run it as JUnit from Eclipse, do as below.
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.
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
Here is the Page Object Framework implemented with cucumber-jvm and selenium-java
http://www.seleniumframework.com/cucumber-jvm-3/cucumber-jvm-and-page-object/