java - 如何使用 rest api 响应验证 ui 数据
问题描述
我需要验证我的 ui 数据和 api 响应是否相同,
这是我尝试过的代码,
private ValidateContentPage cp = new ValidateContentPage();
public void getTitle() {
String UITitle = driver.findElement(titlepage).getText();
System.out.println(UITitle);
Assert.assertEquals(UITitle, cp.getAPICall(),"Passed");
}
我在这里得到我的 api 响应,
public class ValidateContentPage {
public common cm = new common();
public Properties prop;
public void baseURI() {
prop = cm.getProperties("./src/test/API/IndiaOne/propertyfile/EndpointURL.properties");
RestAssured.baseURI = prop.getProperty("baseURI");
}
public String getAPICall() {
objectpojo ps = given().expect().defaultParser(Parser.JSON).when().get(prop.getProperty("resources")).as(objectpojo.class, cm.getMapper());
int number = ps.getPosts().size();
System.out.println(number);
System.out.println(ps.getPosts().get(0).getTitle());
return ps.getPosts().get(0).getTitle();
}
如果我使用 testng 断言验证两者都会引发空指针异常,那么任何人都可以帮助我验证我的 ui 数据和 api 响应。
解决方案
您需要从@Test
自身或从调用 ValidateContentPage@BeforeTest
@Test
public void getTitle() {
String UITitle = driver.findElement(titlepage).getText();
System.out.println(UITitle);
ValidateContentPage cp = new ValidateContentPage();
Assert.assertEquals(UITitle, cp.getAPICall(),"Passed");
}
推荐阅读
- java - Selenium 自动化测试以检查后端的 XML 数据
- reactjs - 使用 reactjs 将数据从父组件传递到子组件,并使用 React.cloneElement 键入脚本
- c# - 如何以最佳方式从动态数据类型中搜索特定属性
- bash - Listing all directories in a bash arguments
- django - 如何在 docker 上使用 nginx 后面的散景服务器(server_document)和 django?
- mysql - 嵌套 MySQL 查询失败,错误代码 1060 Duplicate column name 'xxx'`
- python - 形状不匹配:无法将对象广播到单个形状如何解决python中的此类错误?
- mysql - mysql中的存储过程,无法找出语法错误
- javascript - 如何在 forEach 循环和 if 语句中正确返回字符串?
- javascript - 每隔 n 次在循环内运行代码的简单方法