最近在学习spring,觉得也可以将spring运用到测试中。Spring很强大,这里只是其中一个简单的应用,不包含全部源码。有不合理的地方,欢迎指正。
核心依赖
代码语言:javascript复制<!--springboot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!--Springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<optional>true</optional>
</dependency>
配置
配置文件Application.yml,可以将webdriver和driverService配置放在这:webdriver和driverService,监听器。。。如下,浏览器,端口号,driverService地址等,还可以将测试环境地址放在这里。
代码语言:javascript复制logging:
level:
org.openqa.selenium: debug
org.springframework: info
webdriver:
#Service 是否远程
isRemote: false
#运行浏览器
browser: CHROME
#webdriver驱动地址
service:
location: chromedriver.exe
#启动service端口
port: 1411
capabilities:
javascriptEnabled: true
takesScreenshot: true
cssSelectorsEnabled: true
listener: org.openqa.selenium.support.events.AbstractWebDriverEventListener
配置类
代码语言:javascript复制//Webdriver配置类对应配置文件中各项
@ConfigurationProperties(prefix = "webdriver")
@Setter
@Getter
public class WebDriverProperties {
private String osName = System.getProperty("os.name");
private String browser = BrowserType.CHROME;
private Boolean isRemote = false;
private Map<String, Object> capabilities;
private String[] listeners;
}
//DriverService
@ConfigurationProperties(prefix = "webdriver.service")
@Getter
@Setter
public class DriverServiceProperties {
private int port = 1411;
private String location = "chromedriver.exe";
}
浏览器枚举类
代码语言:javascript复制public enum DriverType implements DriverBase {
/**
* chrome类型的driverService和webDriver
*/
CHROME{
private DriverService service;
@Override
public DriverService createDriverService(DriverServiceProperties driverServiceProperties){
ClassPathResource driverResource = new ClassPathResource(driverServiceProperties.getLocation());
try {
service = new ChromeDriverService.Builder()
.usingDriverExecutable(driverResource.getFile())
.usingPort(driverServiceProperties.getPort())
.build();
} catch (IOException e) {
throw new RuntimeException("driver.exe执行文件读取失败",e);
}
return service;
}
@Override
public WebDriver createDriver(WebDriverProperties webDriverProperties, DriverService driverService) {
if (!driverService.isRunning()){
try {
driverService.start();
} catch (IOException e) {
throw new RuntimeException("driverService启动失败");
}
}
DesiredCapabilities capabilities = new DesiredCapabilities(webDriverProperties.getCapabilities());
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(driverService.getUrl(), capabilities);
return remoteWebDriver;
}
}
}
创建Webdriver,DriverService类
代码语言:javascript复制@Configuration
@EnableConfigurationProperties({WebDriverProperties.class, DriverServiceProperties.class})
@ConditionalOnMissingBean(name = {"driverService","webDriver"})
@ConditionalOnClass(DriverService.class)
public class DriverConfig {
@Autowired
private WebDriverProperties webDriverProperties;
@Autowired
private DriverServiceProperties driverServiceProperties;
private DriverBase base;
@PostConstruct
public void init(){
base = DriverType.valueOf(webDriverProperties.getBrowser());
}
/** 创建driverService
* @return DriverService 驱动服务
* @throws IOException
*/
@Bean(destroyMethod = "stop")
public DriverService driverService(){
return base.createDriverService(driverServiceProperties);
}
/**
*
* @return WebDriver
*/
@Bean(destroyMethod = "quit")
@DependsOn("driverService")
public WebDriver webDriver(DriverService driverService){
WebDriver driver = base.createDriver(webDriverProperties,driverService);
EventFiringWebDriver webDriver = new EventFiringWebDriver(driver);
webDriver.register(new WebDriverListener());
webDriver.manage().window().maximize();
return driver;
}
@Bean
@DependsOn("webDriver")
public WebDriverWait webDriverWait(WebDriver webDriver){
return new WebDriverWait(webDriver, 10);
}
}
Page类
代码语言:javascript复制@Component
public class BasePage implements PageBase {
@Autowired
protected WebDriver webDriver;
@FindBy(xpath = "")
public Header header;
@PostConstruct
@Override
public void initElements() {
PageFactory.initElements(webDriver, this);
}
测试类
测试类中只需要将webdriver和page类注入即可。
代码语言:javascript复制@SpringBootTest
public class TestPage extends AbstractTestNGSpringContextTests {
@Autowired
private WebDriver webDriver;
@Autowired
private BasePage basePage;
@Test
public void test() {
webDriver.get("//https:www.baidu.com");
}
}
总结
与springboot结合,利用springboot的自动配置和依赖注入,将webdriver的创建和关闭以及其它page类的创建依赖管理交给spring,测试人员可以专注编写测试用例。
实现起来并不难,但确实很考验代码功底,作者源代码地址没法访问了,所以就别惦记了,学学思路即可。
六哥已经写过Spring Boot技术栈的Web、接口、安卓自动化框架了,你是不是也想尝试了?
自动化测试入门[1]
参考资料
[1]
Selenium SpringBoot自动化测试入门: https://blog.csdn.net/li277/article/details/92583659
- END -