提交 3468b668 authored 作者: 黄承天's avatar 黄承天

测试用例增加标题字段。测试步骤的操作类型增加switch(切换至指定的元素)和home(回到起始页面),由2种增加至4种。

上级 fe91e65f
......@@ -12,10 +12,7 @@ public interface Action {
String CLICK = "click";
String GET_TEXT = "gettext";
String READ_ONLY = "readonly";
String REGISTER = "register";
String SWITCH = "switch";
String HOME = "home";
}
......@@ -4,6 +4,6 @@ public interface Assertion {
String VALUE = "value";
String TITLE = "tiele";
String TITLE = "title";
}
......@@ -20,7 +20,7 @@ import java.util.Map;
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@RequestMapping("/testcase")
@RestController
@Api(value = "自动化测试管理接口")
@Api(tags = "测试用例")
public class TestCaseController {
@Autowired
......
......@@ -24,49 +24,44 @@ public class Measure {
/**
* 步骤序号
*/
@ApiModelProperty(value = "步骤序号",example = "1")
@JSONField(ordinal = 1)
@ApiModelProperty(value = "步骤序号",example = "1",position = 1)
private Integer order;
/**
* 步骤名
*/
@ApiModelProperty(value = "步骤名",example = "输入关键字")
@JSONField(ordinal = 2)
@ApiModelProperty(value = "步骤名",example = "输入关键字",position = 2)
private String title;
/**
* 是否成功
*/
@ApiModelProperty(value = "是否成功",example = "true")
@JSONField(ordinal = 3)
@ApiModelProperty(value = "是否成功",example = "true",position = 3)
private Boolean success;
/**
* 判断类型
*/
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题")
@JSONField(ordinal = 4)
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题",position = 4)
private String assertion;
/**
* 期望结果
*/
@ApiModelProperty(value = "期望结果",example = "ty")
@ApiModelProperty(value = "期望结果",example = "ty",position = 5)
@JSONField(ordinal = 5)
private String expected;
/**
* 实际结果
*/
@ApiModelProperty(value = "实际结果",example = "ty")
@JSONField(ordinal = 6)
@ApiModelProperty(value = "实际结果",example = "ty",position = 6)
private String practice;
/**
* 信息
*/
@ApiModelProperty(value = "相关信息",example = "成功.")
@JSONField(ordinal = 7)
@ApiModelProperty(value = "相关信息",example = "成功.",position = 7)
private String message;
}
......@@ -22,22 +22,27 @@ import java.util.List;
public class Report {
/**
* url
* 测试用例标题
*/
@ApiModelProperty(value = "网站地址",example = "http://www.baidu.com")
private String url;
@ApiModelProperty(value = "测试用例标题",example = "百度一下",position = 1)
private String title;
/**
* 浏览器
*/
@ApiModelProperty(value = "浏览器",example = "firefox")
private String explorer;
@ApiModelProperty(value = "浏览器",example = "firefox",position = 2)
private String browser;
/**
* 网站地址
*/
@ApiModelProperty(value = "网站地址",example = "http://www.baidu.com",position = 3)
private String url;
/**
* 每个步骤结果详情
*/
@ApiModelProperty(value = "每个步骤结果详情")
@ApiModelProperty(value = "每个步骤结果详情",position = 4)
private List<Measure> measures;
}
......@@ -22,62 +22,52 @@ import lombok.NoArgsConstructor;
@ApiModel(value = "测试用例步骤")
public class Step {
/**
* 步骤序号
*/
@ApiModelProperty(value = "序号",example = "1")
@JSONField(ordinal = 1)
@ApiModelProperty(value = "序号",example = "1",position = 1)
private Integer order;
/**
* 标题
*/
@ApiModelProperty(value = "步骤的标题",example = "输入关键字")
@JSONField(ordinal = 2)
@ApiModelProperty(value = "步骤的标题",example = "输入关键字",position = 2)
private String title;
/**
* 元素的定位类型
*/
@ApiModelProperty(value = "元素的定位类型",example = "id")
@JSONField(ordinal = 3)
@ApiModelProperty(value = "元素的定位类型",example = "id",position = 3)
private String type;
/**
* 元素的定位关键值
*/
@ApiModelProperty(value = "元素的定位关键值",example = "kw")
@JSONField(ordinal = 4)
@ApiModelProperty(value = "元素的定位关键值",example = "kw",position = 4)
private String key;
/**
* 元素的操作方式
*/
@ApiModelProperty(value = "元素的操作方式",example = "input",notes = "目前有2种:input-输入 click-点击")
@JSONField(ordinal = 5)
@ApiModelProperty(value = "元素的操作方式",example = "input",notes = "目前有2种:input-输入 click-点击",position = 5)
private String action;
/**
* 输入值 操作为input时需要
*/
@ApiModelProperty(value = "输入值 操作为input时需要",example = "ty")
@JSONField(ordinal = 6)
@ApiModelProperty(value = "输入值 操作为input时需要",example = "ty",position = 6)
private String value;
/**
* 判断类型
*/
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题")
@JSONField(ordinal = 7)
@ApiModelProperty(value = "判断类型",example = "value",notes = "目前有2种:value-当前元素的值 title-页面标题",position = 7)
private String assertion;
/**
* 期望结果
*/
@ApiModelProperty(value = "期望结果",example = "ty")
@JSONField(ordinal = 8)
@ApiModelProperty(value = "期望结果",example = "ty",position = 8)
private String expected;
}
......@@ -29,6 +29,8 @@ public class TestCase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
String title;
/**
* 浏览器
*/
......
......@@ -19,24 +19,34 @@ import java.util.List;
@ApiModel(value = "测试用例")
public class TestCaseVo {
@ApiModelProperty(value = "主键",example = "1")
/**
* 主键
*/
@ApiModelProperty(value = "主键",example = "1",position = 0)
Integer id;
/**
* 测试用例标题
*/
@ApiModelProperty(value = "测试用例标题",example = "百度一下",position = 1)
String title;
/**
* 浏览器
*/
@ApiModelProperty(value = "浏览器",example = "firefox")
@ApiModelProperty(value = "浏览器",example = "firefox",position = 2)
String browser;
/**
* 起始网站
*/
@ApiModelProperty(value = "网站地址",example = "http://www.baidu.com")
@ApiModelProperty(value = "网站地址",example = "http://www.baidu.com",position = 3)
String url;
/**
* 步骤详情
*/
@ApiModelProperty(value = "步骤详情")
@ApiModelProperty(value = "步骤详情",position = 4)
List<Step> steps;
}
......@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import static com.zjty.automatedtesting.common.action.Action.*;
import static com.zjty.automatedtesting.common.action.ByType.*;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
/**
* @author C
......@@ -36,7 +37,7 @@ public class SeleniumServiceImpl implements SeleniumService {
public Report execute(TestCaseVo testCase) {
WebDriver driver = initDriver(testCase.getBrowser());
driver.get(testCase.getUrl());
return executeSteps(driver, testCase);
return executeSteps(testCase.getUrl(), driver, testCase);
}
/**
......@@ -54,54 +55,57 @@ public class SeleniumServiceImpl implements SeleniumService {
} else if (Objects.equals(browser, Browser.IE)) {
System.setProperty("webdriver.ie.driver", CommonUtils.IE_EXE);
return new ChromeDriver();
}else {
} else {
throw new RuntimeException("该浏览器不存在:" + browser);
}
}
private Report executeSteps(WebDriver driver, TestCaseVo testCase) {
private Report executeSteps(String url, WebDriver driver, TestCaseVo testCase) {
List<Measure> measures = Lists.newArrayList();
List<Step> steps = testCase.getSteps().stream().sorted(Comparator.comparingInt(Step::getOrder)).collect(Collectors.toList());
for (Step step : steps) {
boolean success = false;
String practice = "";
String practice = null;
String message;
log.info("Executing Step:{}", step.getTitle());
WebElement webElement = null;
log.info("执行步骤:{}", step.getTitle());
try {
if (Objects.equals(step.getAction(), HOME)){
driver.get(url);
}else {
webElement = getWebElement(step.getType(), step.getKey(), driver);
if (Objects.equals(step.getAction(), INPUT)) {
String value = isNull(step.getValue()) ? "" : step.getValue();
WebElement webElement = getWebElement(step.getType(), step.getKey(), driver);
webElement.sendKeys(value);
} else if (Objects.equals(step.getAction(), CLICK)) {
WebElement webElement = getWebElement(step.getType(), step.getKey(), driver);
webElement.click();
} else if (Objects.equals(step.getAction(), SWITCH)) {
driver.switchTo().frame(webElement);
} else {
throw new RuntimeException("不匹配的操作类型:" + step.getAction());
}
}
if (Objects.equals(step.getAssertion(), Assertion.VALUE)) {
WebElement webElement = getWebElement(step.getType(), step.getKey(), driver);
if (nonNull(webElement)){
practice = webElement.getAttribute("value");
if (Objects.equals(practice, step.getExpected())) {
success = true;
message = String.format("[%s] success.", step.getTitle());
} else {
message = String.format("[%s] failed.the value is incorrect. expected:%s practice:[%s] ", step.getTitle(), step.getExpected(), practice);
}
} else if (Objects.equals(step.getAssertion(), Assertion.TITLE)){
}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 = String.format("[%s] success.", step.getTitle());
message = "成功";
} else {
message = String.format("[%s] failed.the page is incorrect. expected:[%s] practice:[%s] ", step.getTitle(), step.getExpected(), practice);
}
}else {
throw new RuntimeException("不匹配的判断类型:" + step.getAssertion());
message = String.format("失败 实际与预期不符 预期:[%s] 实际:[%s] ",step.getExpected(), practice);
}
} catch (Exception e) {
e.printStackTrace();
success = false;
message = String.format("error detected:[%s]", e.getMessage());
message = String.format("出现错误:[%s]", e.getMessage());
log.error("出现错误:", e);
}
measures.add(new Measure(
step.getOrder(),
......@@ -114,15 +118,17 @@ public class SeleniumServiceImpl implements SeleniumService {
));
}
return new Report(
testCase.getTitle(),
testCase.getUrl(),
testCase.getBrowser(),
measures
);
}
@SuppressWarnings("DuplicatedCode")
private WebElement getWebElement(String getMethod, String key, WebDriver driver) {
WebElement webElement;
WebElement webElement = null;
if (Objects.equals(getMethod, XPATH)) {
webElement = driver.findElement(By.xpath(key));
} else if (Objects.equals(getMethod, CSS)) {
......@@ -131,9 +137,14 @@ public class SeleniumServiceImpl implements SeleniumService {
webElement = driver.findElement(By.id(key));
} else if (Objects.equals(getMethod, NAME)) {
webElement = driver.findElement(By.name(key));
} else {
throw new RuntimeException("未找到元素 type:" + getMethod + " key: " + key);
}
if (Objects.nonNull(webElement)) {
return webElement;
} else {
throw new RuntimeException("未找到元素 定位方式:" + getMethod + " 关键值: " + key);
}
}
}
......@@ -15,6 +15,7 @@ public class TransHelper {
String steps = JsonUtil.toJSon(testCaseVo.getSteps());
return new TestCase(
testCaseVo.getId(),
testCaseVo.getTitle(),
testCaseVo.getBrowser(),
testCaseVo.getUrl(),
steps
......@@ -25,6 +26,7 @@ public class TransHelper {
List<Step> steps = JsonUtil.readValueToList(testCase.getSteps(),Step.class);
return new TestCaseVo(
testCase.getId(),
testCase.getTitle(),
testCase.getBrowser(),
testCase.getUrl(),
steps
......
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/automated_testing?useSSL=false&serverTimezone=UTC
spring.datasource.url=jdbc:mysql://localhost:3306/automated_testing?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论