Skip to content

EmbedITCZ/jbehave-support

Repository files navigation

Maven Central CI BrowserStack Maintainability Rating

jbehave-support

Light extension to JBehave using Spring framework.

Provides several base steps for working with REST, SOAP, SQL, SSH, (Selenium based) web testing, health checks along with support for verification, expression commands and basic reporting.

Currently supported Java versions are 17, 21 and 22 (latest LTS and latest version).

Contents

  1. Modules
  2. Contributors guide
  3. Examples
  4. Best practices
  5. Known issues
  6. Simple use case
    1. Add to Java project as a Maven dependency
    2. Create a TestConfig configuration file
    3. Create a ui-mapping file
    4. Write your story
    5. Write your story class
  7. Thanks

Modules

  • core - details about integration and usage
  • core-test - contains details about the simple application used for integration tests

Contributors guide

Contributors guide can be found in CONTRIBUTING.md

Examples

Best practices

Known issues

Simple use case - Web testing

To show you how to set up a project using the jbehave-support library, I am going to make a test that Google searches EmbedITCZ jbehave-support and checks the result. To learn more about this example check out Web-testing.md.

Of course you can use jbehave-support for much more than just selenium based testing. For example server communication (SOAP, REST or database manipulation (SQL).

Add to Java project as a Maven dependency

To add jbehave-support to a java project, just add this dependency to your pom.xml.

<dependency>
    <groupId>org.jbehavesupport</groupId>
    <artifactId>jbehave-support-core</artifactId>
    <version>[current version number]</version>
</dependency>

Then build your project (mvn clean install) to download all the necessary dependencies.

Also, make sure to have the test resources setup correctly, simple setup to use can be like this:

<build>
    <testResources>
        <testResource>
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.story</include>
                <include>**/*.table</include>
            </includes>
        </testResource>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
</build>

Create a TestConfig configuration file

From this file, jbehave-support will take all the necessary information about tested applications and the types of reports you want. First create a Java class and call it TestConfig. Add the spring annotations @EnableAutoConfiguration and @SpringBootConfiguration (or @Configuration if your project already contains a spring boot configuration).

@SpringBootConfiguration
@EnableAutoConfiguration
public class TestConfig {

Setting up the application you want to test largely depends on what do you want to test. Generally, you need to add a Spring bean method setting up the necessary parameters. We will be setting-up a WebSetting for Selenium to access google.com. (More about setting up web testing)

@Bean
@Qualifier("GOOGLE")
public WebSetting google() {
    return WebSetting.builder()
          .homePageUrl("https://www.google.com")
          .elementLocatorsSource("home.yaml")
          .build();
}

The @Qualifier annotation sets up the name, under which we will be able to access this application in our story.

The homePageUrl method sets the url of the web applications home page.

The elementLocatorsSource methods sets the name of a file containing addresses of web page elements we want to interact with.

Create a ui mapping file

In this yaml file, we need to setup the links to web page elements we want to interact with. It has to be placed in the resources directory, which is on one level above your main code directory:

  • Project
    • src
      • main
        • java
          • your.main.code.directory
        • resources
          • home.yaml
      • test
        • java
          • your.test.code.directory

At this point, your.main.code.directory should contain the TestConfig class created previously.
Note also the location of your.test.code.directory — this is where your story files will go later.

The links and names should be written like this:

home:
  search.button.css: "#tsf > div:nth-child(2) > div > div.FPdoLc > center > input[type='submit']:nth-child(1)"
  search.text.css: "input[type='text'][name='q']"
  search.output.css: "#rso > div:nth-child(1) > div > div.r > a > h3"

The title home: is the name of the page these elements can be found on.

Under the names search.button, search.text and search.output, we can use these elements in a story.

The .css extension tells the code, what type of address to look for. (Ex. .xpath)

The part after the colon is the address of the element itself.

More about ui mapping files

Write your story

In your your.test.code.directory directory create a .story file. I will call it Google.story.

(Optional) Inside, write the narrative, which should explain the purpose of this story. It has 3 mandatory parts: In order to, As a and I want to.

Narrative:
In order to try jbehave-support
As a confused human
I want to see if I can set it up

Then write your scenario:

Scenario: Open Google
Given [GOOGLE] homepage is open
When on [home] page these actions are performed:
| element       | action | data                      |
| search.text   | FILL   | embeditcz jbehave-support |
| search.button | CLICK  |                           |
Then on [home] page these conditions are verified:
| element       | property | data    | verifier |
| search.output | TEXT     | EmbedIT | CONTAINS |

This scenario opens www.google.com, writes embeditcz jbehave-support into the Google search bar. Clicks search and checks if the first result contains the text EmbedIT. Notice instead of lengthy element addresses, the element names defined in home.yaml are used.

More about web testing steps for your story

Write your story class

In the same directory as your .story file, create a Java class that extends AbstractSpringStories and call it <yourStoryName>Story (naming is important). The test configuration will automatically be picked up by Spring Boot. You can leave this class empty.

public class GoogleStory extends AbstractSpringStories {
}

This class is runnable. When you run it, it runs your story.

Thanks

Jetbrains

For providing us open source licenses to IntelliJ IDEA.

Browserstack

for providing us with a free license for our project. We use BrowserStack in our build pipeline to make sure that our selenium based testing components are working correctly across multiple browsers.