I have task: "Check that each Color field has a color value (format string "#FFFFFF")".
I have a solution:
@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 #");
}
I think I solved the problem incorrectly is there a more suitable solution?
Additional information: The URL https://reqres.in/api/unknown is public and returns e.g.
{
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"
},
...
This problem can be solved using 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();
You can experiment with regular expressions here