首页 > 解决方案 > 如何使用 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 响应。

标签: javarestselenium-webdriverassertrest-assured

解决方案


您需要从@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");
    }

推荐阅读