提交 36e96fdd authored 作者: 黄承天's avatar 黄承天

数据结构改动

测试步骤的断言改为集合形式 并重新定义了断言的数据结构和判断机制
上级 c30917f8
package com.zjty.automatedtesting.common.action;
/**
* equals、not_equals、exist、not_exist
*/
public interface Assertion {
String VALUE = "value";
String EQUALS = "equals";
String TITLE = "title";
String NOT_EQUALS = "not_equals";
String EXIST = "exist";
String NOT_EXIST = "not_exist";
}
......@@ -2,7 +2,9 @@ package com.zjty.automatedtesting.controller;
import com.zjty.automatedtesting.pojo.test.System;
import com.zjty.automatedtesting.pojo.test.SystemVo;
import com.zjty.automatedtesting.service.SystemService;
import com.zjty.automatedtesting.service.impl.TransHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -19,6 +21,9 @@ public class SystemController {
@Autowired
SystemService systemService;
@Autowired
TransHelper transHelper;
@ApiOperation(value = "新增测试系统.")
@PostMapping(value="/create")
public ResponseEntity<System> create(@RequestBody System system){
......@@ -28,10 +33,10 @@ public class SystemController {
@ApiOperation(value = "根据id获取测试系统.")
@GetMapping(value="/get/{id}")
public ResponseEntity<System> findById(@PathVariable String id){
public ResponseEntity<SystemVo> findById(@PathVariable String id){
System system = systemService.findById(id);
return ResponseEntity.ok(system);
SystemVo result = transHelper.toSystemVo(system);
return ResponseEntity.ok(result);
}
}
......@@ -47,10 +47,17 @@ public class TestCaseController {
.collect(Collectors.toList()));
}
@ApiOperation(value = "保存测试用例.", notes = "不附带id为新增 附带id为修改")
@PostMapping(value = "/save")
public ResponseEntity<Map<String, String>> save(@RequestBody CaseVo caseVo) {
caseService.save(caseVo);
@ApiOperation(value = "新增测试用例.", notes = "不附带id")
@PostMapping(value = "/create")
public ResponseEntity<Map<String, String>> create(@RequestBody CaseVo caseVo) {
caseService.create(caseVo);
return ResponseEntity.ok(ImmutableMap.of("message", "success"));
}
@ApiOperation(value = "修改测试用例.", notes = "必须附带id")
@PostMapping(value = "/update")
public ResponseEntity<Map<String, String>> update(@RequestBody CaseVo caseVo) {
caseService.update(caseVo);
return ResponseEntity.ok(ImmutableMap.of("message", "success"));
}
......
......@@ -34,32 +34,13 @@ public class Measure {
/**
* 是否成功
*/
@ApiModelProperty(value = "是否成功",example = "true",position = 3)
@ApiModelProperty(value = "是否成功 true/false",example = "true",position = 3)
private Boolean success;
/**
* 判断类型
*/
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题",position = 4)
private String assertion;
/**
* 期望结果
*/
@ApiModelProperty(value = "期望结果",example = "ty",position = 5)
@JSONField(ordinal = 5)
private String expected;
/**
* 实际结果
*/
@ApiModelProperty(value = "实际结果",example = "ty",position = 6)
private String practice;
/**
* 信息
*/
@ApiModelProperty(value = "相关信息",example = "成功.",position = 7)
@ApiModelProperty(value = "相关信息",example = "成功.",position = 4)
private String message;
}
package com.zjty.automatedtesting.pojo.test;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "断言")
public class Assert {
/**
* 元素的定位类型
*/
@ApiModelProperty(value = "元素的定位类型", example = "id", position = 1)
private String locateType;
/**
* 元素的定位关键值
*/
@ApiModelProperty(value = "元素的定位关键值", example = "kw", position = 2)
private String locateKey;
/**
* 断言类型
*/
@ApiModelProperty(value = "断言类型", example = "equals", position = 3)
private String assertType;
/**
* 断言键
*/
@ApiModelProperty(value = "断言键", example = "name", position = 4)
private String assertKey;
/**
* 断言值
*/
@ApiModelProperty(value = "断言值", example = "wd", position = 5)
private String assertValue;
}
......@@ -6,6 +6,8 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* <p>Description : selenium-test
......@@ -21,49 +23,49 @@ public class Step {
/**
* 步骤序号
*/
@ApiModelProperty(value = "序号",example = "1",position = 1)
@ApiModelProperty(value = "序号", example = "1", position = 1)
private Integer order;
/**
* 标题
*/
@ApiModelProperty(value = "步骤的标题",example = "输入关键字",position = 2)
@ApiModelProperty(value = "步骤的标题", example = "输入关键字", position = 2)
private String title;
/**
* 元素的定位类型
*/
@ApiModelProperty(value = "元素的定位类型",example = "id",position = 3)
@ApiModelProperty(value = "元素的定位类型", example = "id", position = 3)
private String type;
/**
* 元素的定位关键值
*/
@ApiModelProperty(value = "元素的定位关键值",example = "kw",position = 4)
@ApiModelProperty(value = "元素的定位关键值", example = "kw", position = 4)
private String key;
/**
* 元素的操作方式
*/
@ApiModelProperty(value = "元素的操作方式",example = "input",notes = "目前有2种:input-输入 click-点击",position = 5)
@ApiModelProperty(value = "元素的操作方式", example = "input", position = 5)
private String action;
/**
* 输入值 操作为input时需要
*/
@ApiModelProperty(value = "输入值 操作为input时需要",example = "ty",position = 6)
@ApiModelProperty(value = "输入值 操作为input时需要", example = "ty", position = 6)
private String value;
/**
* 判断类型
*/
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题",position = 7)
private String assertion;
@ApiModelProperty(value = "判断类型", position = 7)
private List<Assert> asserts;
/**
* 期望结果
*/
@ApiModelProperty(value = "期望结果",example = "ty",position = 8)
@ApiModelProperty(value = "期望结果", example = "ty", position = 8)
private String expected;
}
package com.zjty.automatedtesting.pojo.test;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
......@@ -15,6 +16,7 @@ import javax.persistence.*;
@Entity
@Table(name = "AUTO_TEST_SYSTEM")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class System {
@Id
......
package com.zjty.automatedtesting.pojo.test;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "测试系统")
public class SystemVo {
@ApiModelProperty(value = "主键id 也是系统编码",example = "40285a81")
private String id;
@ApiModelProperty(value = "系统名",example = "A系统")
private String name;
@ApiModelProperty(value = "测试用例")
private List<CaseVo> cases;
}
......@@ -8,14 +8,21 @@ import java.util.List;
@Service
public interface CaseService {
/**
* 新增测试用例
* 不可附带id
* @param caseVo testCaseVo
* @return 保存的id
*/
Integer create(CaseVo caseVo);
/**
* 保存测试用例
* 不附带id为新增
* 附带id为修改
* 必须附带id
* @param caseVo testCaseVo
* @return 保存的id
*/
Integer save(CaseVo caseVo);
Integer update(CaseVo caseVo);
/**
* 指定id删除测试用例
......
......@@ -12,7 +12,14 @@ public interface SystemService {
* 不可附带id
* @return 保存后的测试系统对象
*/
System create(System systemVo);
System create(System system);
/**
* 更新测试系统
* 必须附带id
* @return 保存后的测试系统对象
*/
System update(System system);
/**
* 根据id查找测试系统
......
......@@ -12,6 +12,9 @@ import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@Service
public class CaseServiceImpl implements CaseService {
......@@ -23,13 +26,29 @@ public class CaseServiceImpl implements CaseService {
TransHelper transHelper;
@Override
public Integer save(CaseVo caseVo) {
public Integer create(CaseVo caseVo) {
Integer id;
if (isNull(caseVo.getId())) {
Case aCase = transHelper.toTestCase(caseVo);
id = repository.save(aCase).getId();
} else {
throw new RuntimeException("新增不可附带id");
}
return id;
}
@Override
public Integer update(CaseVo caseVo) {
Integer id;
if (Objects.isNull(aCase.getId()) || repository.existsById(aCase.getId())) {
if (nonNull(caseVo.getId())){
if (repository.existsById(caseVo.getId())) {
Case aCase = transHelper.toTestCase(caseVo);
id = repository.save(aCase).getId();
} else {
throw new RuntimeException("指定的id不存在:" + aCase.getId());
throw new RuntimeException("指定的id不存在:" + caseVo.getId());
}
} else {
throw new RuntimeException("修改必须附带id");
}
return id;
}
......
package com.zjty.automatedtesting.service.impl;
import com.google.common.collect.Lists;
import com.zjty.automatedtesting.common.action.Assertion;
import com.mysql.cj.exceptions.AssertionFailedException;
import com.zjty.automatedtesting.common.action.Browser;
import com.zjty.automatedtesting.pojo.report.Measure;
import com.zjty.automatedtesting.pojo.report.Report;
import com.zjty.automatedtesting.pojo.report.ReportVo;
import com.zjty.automatedtesting.pojo.test.Assert;
import com.zjty.automatedtesting.pojo.test.Step;
import com.zjty.automatedtesting.pojo.test.CaseVo;
import com.zjty.automatedtesting.service.ReportService;
import com.zjty.automatedtesting.service.SeleniumService;
import com.zjty.automatedtesting.util.CommonUtils;
import com.zjty.automatedtesting.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
......@@ -25,10 +23,10 @@ import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.zjty.automatedtesting.common.action.Action.*;
import static com.zjty.automatedtesting.common.action.Assertion.*;
import static com.zjty.automatedtesting.common.action.ByType.*;
import static com.zjty.automatedtesting.util.JsonUtil.toJSon;
import static java.util.Objects.isNull;
......@@ -79,9 +77,8 @@ public class SeleniumServiceImpl implements SeleniumService {
List<Step> steps = testCase.getSteps().stream().sorted(Comparator.comparingInt(Step::getOrder)).collect(Collectors.toList());
for (Step step : steps) {
boolean success;
String practice = null;
String message;
WebElement webElement = null;
String message = "通过测试";
WebElement webElement;
log.info("执行步骤:{}", step.getTitle());
try {
if (Objects.equals(step.getAction(), HOME)) {
......@@ -96,45 +93,20 @@ public class SeleniumServiceImpl implements SeleniumService {
} else if (Objects.equals(step.getAction(), SWITCH)) {
driver.switchTo().frame(webElement);
} else {
throw new RuntimeException("不匹配的操作类型:" + step.getAction());
}
}
if (isNull(step.getAssertion()) || Objects.equals(step.getAssertion(), "")) {
success = true;
message = "成功";
} else {
waitTime(2000L);
if (Objects.equals(step.getAssertion(), Assertion.VALUE)) {
if (nonNull(webElement)) {
practice = webElement.getAttribute("value");
}
} else if (Objects.equals(step.getAssertion(), Assertion.TITLE)) {
practice = driver.getTitle();
} else {
throw new RuntimeException("不匹配的判断类型:" + step.getAssertion());
}
if (Objects.equals(practice, step.getExpected())) {
success = true;
message = "成功";
} else {
success = false;
message = String.format("失败 实际与预期不符 预期:[%s] 实际:[%s] ", step.getExpected(), practice);
throw new WebDriverException("不匹配的操作类型:" + step.getAction());
}
}
success = assertions(step.getAsserts(), driver);
waitTime(2000L);
} catch (Exception e) {
success = false;
message = String.format("出现错误:[%s]", e.getMessage());
log.error("出现错误:", e);
log.error("出现错误 : " + e.getMessage());
}
measures.add(new Measure(
step.getOrder(),
step.getTitle(),
success,
step.getAssertion(),
step.getExpected(),
practice,
message
));
}
......@@ -155,10 +127,51 @@ public class SeleniumServiceImpl implements SeleniumService {
);
Integer id = reportService.save(report);
reportVo.setId(id);
driver.close();
waitTime(4000L);
driver.quit();
return reportVo;
}
private Boolean assertions(List<Assert> asserts, WebDriver driver) {
String practice = "";
for (Assert assertion : asserts) {
WebElement element = getWebElement(assertion.getLocateType(), assertion.getLocateKey(), driver);
boolean hasKey = nonNull(assertion.getAssertKey()) && !Objects.equals(assertion.getAssertKey(), "");
if (Objects.equals(assertion.getAssertType(), EQUALS)) {
if (hasKey) {
practice = element.getAttribute(assertion.getAssertKey());
}
if (Objects.equals(assertion.getAssertValue(), practice)) {
return true;
} else {
throw new AssertionFailedException("");
}
} else if (Objects.equals(assertion.getAssertType(), NOT_EQUALS)) {
if (hasKey) {
practice = element.getAttribute(assertion.getAssertKey());
}
if (!Objects.equals(assertion.getAssertValue(), practice)) {
return true;
} else {
throw new AssertionFailedException("");
}
} else if (Objects.equals(assertion.getAssertType(), EXIST)) {
if (nonNull(element)) {
return true;
} else {
throw new AssertionFailedException("");
}
} else if (Objects.equals(assertion.getAssertType(), NOT_EXIST)) {
if (isNull(element)) {
return true;
} else {
throw new AssertionFailedException("");
}
}
}
return true;
}
@SuppressWarnings("DuplicatedCode")
private WebElement getWebElement(String getMethod, String key, WebDriver driver) {
WebElement webElement = null;
......@@ -176,7 +189,7 @@ public class SeleniumServiceImpl implements SeleniumService {
if (Objects.nonNull(webElement)) {
return webElement;
} else {
throw new RuntimeException("未找到元素 定位方式:" + getMethod + " 关键值: " + key);
throw new ElementNotVisibleException("未找到元素 定位方式:" + getMethod + " 关键值: " + key);
}
}
......
......@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@Service
......@@ -25,6 +26,15 @@ public class SystemServiceImpl implements SystemService {
}
}
@Override
public System update(System system) {
if (nonNull(system.getId())) {
return repository.save(system);
}else {
throw new RuntimeException("新增数据必须附带id");
}
}
@Override
public System findById(String id) {
return repository.findById(id).orElseThrow(() -> new RuntimeException("未找到该id的测试系统"));
......
......@@ -5,13 +5,21 @@ import com.zjty.automatedtesting.pojo.report.Measure;
import com.zjty.automatedtesting.pojo.report.Report;
import com.zjty.automatedtesting.pojo.report.ReportVo;
import com.zjty.automatedtesting.pojo.test.*;
import com.zjty.automatedtesting.pojo.test.System;
import com.zjty.automatedtesting.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@Service
public class TransHelper {
@Autowired
CaseService caseService;
public Case toTestCase(CaseVo caseVo) {
String steps = JSON.toJSONString(caseVo.getSteps());
return new Case(
......@@ -19,6 +27,7 @@ public class TransHelper {
caseVo.getSystemId(),
caseVo.getTitle(),
caseVo.getBrowser(),
caseVo.getEnvironment(),
caseVo.getUrl(),
steps
);
......@@ -31,6 +40,7 @@ public class TransHelper {
aCase.getSystemId(),
aCase.getTitle(),
aCase.getBrowser(),
aCase.getEnvironment(),
aCase.getUrl(),
steps
);
......@@ -47,5 +57,13 @@ public class TransHelper {
);
}
public SystemVo toSystemVo(System system){
List<CaseVo> cases = caseService.findBySystemId(system.getId());
return new SystemVo(
system.getId(),
system.getName(),
cases
);
}
}
......@@ -2,7 +2,6 @@ package com.zjty.automatedtesting.util;
import com.zjty.automatedtesting.pojo.report.Measure;
import com.zjty.automatedtesting.pojo.report.Report;
import com.zjty.automatedtesting.pojo.report.ReportVo;
import com.zjty.automatedtesting.pojo.test.Case;
import lombok.extern.slf4j.Slf4j;
......@@ -12,7 +11,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import static java.util.Objects.isNull;
......@@ -108,8 +106,6 @@ public class FileUtil {
String rowTemp = "<tr>\n" +
"\t<td>#{order}</td>\n" +
"\t<td>#{title}</td>\n" +
"\t<td>#{expected}</td>\n" +
"\t<td>#{practice}</td>\n" +
"\t<td>#{success}</td>\n" +
"\t<td>#{message}</td>\n" +
"</tr>\n";
......@@ -118,8 +114,6 @@ public class FileUtil {
String row = rowTemp
.replace("#{order}", isNull(measure.getOrder()) ? "" : measure.getOrder().toString())
.replace("#{title}", isNull(measure.getTitle()) ? "" : measure.getTitle())
.replace("#{expected}", isNull(measure.getExpected()) ? "" : measure.getExpected())
.replace("#{practice}", isNull(measure.getPractice()) ? "" : measure.getPractice())
.replace("#{success}", measure.getSuccess() ? "成功" : "失败")
.replace("#{message}", measure.getMessage());
content.append(row);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论