【问题标题】:Integrate Java selenium with Spring boot将 Java selenium 与 Spring 引导集成
【发布时间】:2022-01-09 23:23:58
【问题描述】:

在我用于运行自动化测试脚本的 Java selenium 应用程序中,我想与 Spring/Spring boot 集成以使用 .properties 文件来运行不同的环境。 是否有任何示例或如何进行,我已经看到了很多链接,但无法找到正确的实现方式。 根据应用所处的环境,它应该选择相应的 .properties 文件。

ReadConfigurations.java // 它应该在哪里读取属性文件并获取一些值,例如,

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

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

}

应用程序本地属性

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);
    }

}

【问题讨论】:

    标签: java spring spring-boot selenium


    【解决方案1】:

    对于运行测试,我假设您想从 test 包中读取属性。

    所以,用@Configuration@TestPropertySource 注释你的ReadConfigurations

    @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;
    
        ...
    
    }
    

    然后使用localtest 弹簧配置文件开始您的测试。

    -Dspring.profiles.active=test
    
    @SpringBootTest
    public class LoginTest {
    
    @Autowired
    ReadConfigurations readConfigurations;
    
        @Test
        private void login() throws  InterruptedException {
                System.setProperty("webdriver.chrome.driver", readConfigurations.getDriverPath);
        }
    
    }
    

    这对我开始 Spring Boot 项目很有用: https://github.com/spring-guides/gs-spring-boot/tree/main/initial

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

    【讨论】:

    • 它没有拾取属性文件。 -Dspring.profiles.active=test 在 Maven 配置文件中设置?
    • 不,不使用maven配置文件,只是系统属性。我已经对此进行了多次测试并且它有效..您的属性文件位置在哪里?就我而言:src/test/resources/application-local.propertiessrc/test/resources/application-test.properties
    • 我在 srs/test/resources 中有 .properties。但是我需要使用 Maven 来运行...
    • 我也用 maven 运行过这个,maven 包装器在 spring-boot 项目模板中可用,所以我使用 ./mvnw 运行 maven 命令,但运行 mvn test -Dspring.profiles.active=test 也适用于我。我可以建议运行mvn clean 并仔细检查拼写错误。我希望mvn test -Dspring.profiles.active=test 应该适合你。
    • 我能够通过 Maven 运行,但仍然 ReadConfigurations .java 没有选择属性。该类是否也应该在 src/test/java.感觉应该在srs/main/java
    猜你喜欢
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多