Create a cucumber selenium spring boot project


646
646 points
cucumber spring boot project

Creating a selenium project every automation engineer probably knows how to do it , or they will get it done by following some examples.

Here I want to talk about cucumber selenium spring boot , which in the end delivers the same output but the wy it’s being delivered is different . We can use the spring boot magic to inject the driver and not have to care about picocontainer injector anymore. I’m using as an example an already public project , cucumber selenium spring boot example .

Let’s have a look at the overall structure:

├── jenkins
│   └── Jenkinsfile
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md

├── src
│   └── test
│       ├── java
│       │   └── com
│       │       └── example
│       │           └── selenium
│       │               ├── Application.java
│       │               ├── helpers
│       │               │   └── VisibilityHelper.java
│       │               ├── pages
│       │               │   ├── BasePage.java
│       │               │   ├── HomePage.java
│       │               │   └── ResultsPage.java
│       │               ├── runners
│       │               │   ├── Hook.java
│       │               │   ├── SpringBootRunner.java
│       │               │   └── TestRunner.java
│       │               └── steps
│       │                   ├── BaseSteps.java
│       │                   ├── HomeSteps.java
│       │                   └── ResultsSteps.java
│       └── resources
│           ├── application.properties
│           ├── cucumber.properties
│           ├── features
│           │   └── HomeSearch.feature
│           └── logback.xml
        ├── application.properties
        ├── com
        │   └── example
        │       └── selenium
        │           ├── Application.class
        │           ├── helpers
        │           │   └── VisibilityHelper.class
        │           ├── pages
        │           │   ├── BasePage.class
        │           │   ├── HomePage.class
        │           │   └── ResultsPage.class
        │           ├── runners
        │           │   ├── Hook.class
        │           │   ├── SpringBootRunner.class
        │           │   └── TestRunner.class
        │           └── steps
        │               ├── BaseSteps.class
        │               ├── HomeSteps.class
        │               └── ResultsSteps.class
        ├── cucumber.properties
        ├── features
        │   └── HomeSearch.feature
        └── logback.xml

So what are we observing here ?

Selenium Page objects as spring components

  1. The page objects are classes as spring @Component which will allow us to initlaise the page factory using spring magics inside the BaseSteps class.
@Autowired
private List<BasePage> pages;
    
@Before(order = 2)
    public void initializeDriver() {
        final WebDriver driver = hooks.getDriver();
        // Intialize all page elements
        pages.stream().forEach(p -> {
            initElements(driver, p);
        });
        // --
    }

So here we will initialize the page factory for all the pages objects which are implementing the BasePage interface.

Selenium driver instance as spring @Component

As you can see above the driver instance is being injected through spring and called inside initializeDriver();

Advertisements

    @Autowired
    private Hook hooks;

Without spring here we would’ve have to use amother injector such as picoContainer .

Page objects usage inside step definitions :

The only thing we have to do here is to initialise the pages we want to use in a specific step :

@Autowired
private HomePage homePage;

 @Given("^I search \"([^\"]*)\" in the search input of the home page$")
    public void iSearchInTheSearchInputOfTheHomePage(String search) {
        homePage.inputSearch(search);
    }

There is a nice article how to use cucumber with spring boot api applications and run your api tests in memory with cucumber.

So dear reader , what do you thing comparing with the classic page object , which one do you prefer more?

Personally I prefer the spring boot one , easy and more maintainable.

I hope you enjoy it , happy testing !


Like it? Share with your friends!

646
646 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.