springboot + selenide for ui test automation

use case

Posted on January 9, 2020

One of my recent projects used Selenide and then SpringBoot in the tech stack for UI test automation.

And I encountered one problem there.

The Problem

For that test automation project I implemented Page Object and Page Component patterns. It gives better code structure, better readability and, of course, better code maintainability. When SpringBoot was integrated I wanted to inject ‘page component’ into ‘page object’ using Spring DI mechanism.

And what happened? Unfortunately, I was not able to do so.

Input

I had a helper method at() that creates a page object instance and makes the test script more readable:

public static <T> T at(Class<T> pageClass) {
        return Selenide.page(pageClass);
    }

When you develop a test script it looks like following:

@Test
public void userCanRegisterTest(User user){

        at(HomePage.class)
                .acceptCookies()
                .registerUser(user);
                
        //rest of the test  
         
}

So you know exactly “where you are” - what page object you are currently working with. I find it pretty convenient.

And here’s the code that did not work for me:

@Component
public class HomePage {

    @Autowired
    private Header header;
    
    @Autowired
    private Footer footer;
    
    ...
    
}

Note that I didn’t create Header or Footer with new anywhere in the code and Header and Footer classes were annotated with @Component.

So these autowired fields are never resolved and are null. This is actually quite obvious but if you are new to Spring/Selenide it might be a little bit disappointing. The problem is how Selenide instantiates page objects. It uses newInstance() to create a page object. And Spring does not work this way.

Solution

Actually Spring has a mechanism how to handle autowiring in objects which are created with new or newInstance() etc. You can check several approaches described in this issue on StackOverflow.

But frankly speaking I think this is not a good idea. It’s better to rework your design not to overcomplicate your TAF.

Still, it’s test automation.. keep it simple.