Integrate Java selenium with Spring boot

Viewed 363

In my Java selenium application for running automation testscripts, I want to integrate with Spring/Spring boot to use make use of .properties files for different environment to run. Is there any example or how to proceed, I have seen many links , but not able to find the correct way of implementing it. Depending on the environment the app is in, it should pick the corresponding .properties file.

ReadConfigurations.java // Where it should read the properties file and get some values like,

public class ReadConfigurations  {
//  @Value( "${local.driverpath}" )
//  private String getdriverpath;

//  @Value( "${login_name}" )
//    String getLoginName;

}

application-local.properties

local.driverpath = ..\\chromedriver_win32\\chromedriver.exe
local.implicit_wait_time=30
local.app_url = http://localhost:8080/app
local.login_name= 
local.login_password = 

application-test.properties

local.driverpath = ..\\chromedriver_win32\\chromedriver.exe
local.implicit_wait_time=30
local.app_url = http://testurl:8080/app
local.login_name= 
local.login_password =
public class LoginTest {
ReadConfigurations readConfigurations;

    @Test
    private void login() throws  InterruptedException {
            System.setProperty("webdriver.chrome.driver", readConfigurations.getDriverPath);
    }

}
1 Answers

For running tests, I assume you want to read properties from test package.

So, annotate your ReadConfigurations with @Configuration and @TestPropertySource

@Configuration
@TestPropertySource("application-${spring.profiles.active}.properties")
public class ReadConfigurations {

    // this should be added to resolve ${..}
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Value( "${local.driverpath}" )
    String getDriverPath;

    @Value( "${local.login_name}" )
    String getLoginName;

    ...

}

Then start your tests with local or test spring profile.

-Dspring.profiles.active=test
@SpringBootTest
public class LoginTest {

@Autowired
ReadConfigurations readConfigurations;

    @Test
    private void login() throws  InterruptedException {
            System.setProperty("webdriver.chrome.driver", readConfigurations.getDriverPath);
    }

}

This worked for me for the Getting started Spring Boot project: https://github.com/spring-guides/gs-spring-boot/tree/main/initial

./mvnw test -Dspring.profiles.active=test
Related