【问题标题】:Check that each Color field has a color value (format string "#FFFFFF")检查每个颜色字段是否有颜色值(格式字符串“#FFFFFF”)
【发布时间】:2021-12-17 20:01:51
【问题描述】:

我有任务:“检查每个颜色字段是否有颜色值(格式字符串“#FFFFFF”)”。

我有一个解决方案:

@Test
public void sixTest() {
    Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
    Response response = given()
            .when()
            .get("https://reqres.in/api/unknown")
            .then()
            .log().all()
            .extract().response();

    ResponseBody body = response.getBody();
    String bodyAsString = body.asString();
    Assert.assertEquals(bodyAsString.contains("#"), true, "Response body contains #");

}

我认为我错误地解决了问题,有没有更合适的解决方案?

附加信息:URL https://reqres.in/api/unknown 是公开的并返回例如

{
    page: 1,
    per_page: 6,
    total: 12,
    total_pages: 2,
    data: [
    {
    id: 1,
    name: "cerulean",
    year: 2000,
    color: "#98B2D1",
    pantone_value: "15-4020"
    },
    {
    id: 2,
    name: "fuchsia rose",
    year: 2001,
    color: "#C74375",
    pantone_value: "17-2031"
    },
    ...

【问题讨论】:

  • 为什么你认为你已经错误地解决了它?有错误吗?响应的内容是什么?
  • 没有错误,我只是想改进代码,如果可能的话,让颜色检查更正确。

标签: java rest-assured qa autotest


【解决方案1】:

这个问题可以用regular expressions解决

ResponseBody body = response.getBody();
String bodyAsString = body.asString();

//Regexp for a HEX color
String patternString = "#[a-zA-Z0-9]{6}"

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

//Check that the text matches the regular expression
boolean matches = matcher.matches();

您可以尝试使用正则表达式here

【讨论】:

  • assertTrue(matches) - 我们为什么要写它?代码对其抛出异常。没有它,一切似乎都可以正常工作
  • 可以去掉。
  • 我只是在断言上得到一个异常,显然是因为该字段的值必须包含所有元素patternString
  • 或者更确切地说,为什么匹配返回false
【解决方案2】:

我和 Arthur 有同样的想法,但更具体的是要测试什么。

检查每个颜色是否匹配正则表达式#[0-9A-Z]{6}

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

Response res = given().get("https://reqres.in/api/unknown");
List<String> colors = res.jsonPath().getList("data.color");
assertThat(colors, everyItem(matchesPattern("#[0-9A-Z]{6}")));

注意:Hamcrest 版本 1.x 没有方法 matchesPattern

将 2.2 版添加到您的 pom.xml

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
</dependency>

更多信息:api docs

【讨论】:

  • 'List colors = res.jsonPath().getList("data.color");'从哪里来?
  • @AdriaanKoster 来自链接中的 json https://reqres.in/api/unknown
  • Tnx,现在才发现并更新了问题。
  • 你导入import static org.hamcrest.Matchers.*;了吗?
  • @OttoRahn matchesPattern 表示colors 列表中的每个项目都将匹配正则表达式模式
【解决方案3】:

首先使用 json 解析器读取响应。 我用过杰克逊:

https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.13.0 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.0

遍历data数组节点并迭代每个元素并检查color节点。

public static boolean everythingIsColor(String json) throws JsonProcessingException{

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonBody = objectMapper.readTree(json); //JsonProcessingException

    Pattern pattern = Pattern.compile("#[0-9A-F]{6}"); //regex for color format

    JsonNode dataNode = jsonBody.get("data");
    for(JsonNode node : dataNode){

        JsonNode colorNode = node.get("color");
        if(!colorNode.isTextual()){ //is not text node
            return false;
        }

        Matcher matcher = pattern.matcher(colorNode.asText());
        if(!matcher.matches()){ //does not match with color format
            return false;
        }

    }

    //if reach here means every color node has a color value
    return true;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 2018-07-07
    • 2021-03-31
    • 2015-12-06
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多