- place rest-assured before the JUnit dependency declaration in pom.xml
- use statically import methods, i.e. 'import static io.restassured.RestAssured.*;'
2. example
given(). // parameter
contentType()
param().
formParam().
queryParam().
when(). // action
get().
put()
post().
then(). // field
body()
statusCode()
assertion // locator + matcher
locator
--------
lotto.lottoId
lotto.winners.winnerId
find { it.@type == 'groceries' } //Groovy
hamcrest matchers
-----------------------
is
equalTo
hasItems
hasXPath
matchesXsd
matchesJsonSchemaInClasspath
matchesXsd
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[
{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},
{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}
]
}
}
3. pass parameters
given(). //'get' will automatically convert param to query
param("param1", "value1").
when().
get("/something");
given(). //'post' and 'put' user will hava choice
formParam("formParamName", "value1").
queryParam("queryParamName", "value2").
when().
post("/something");
4. others
- log().all()
- request.relaxedHTTPSValidation()
5. multipart/form data
- multipart -> multiPart (file)
- form data -> formParam (json, name value pair)
6. link in rest-assured/SSLITest.java for example on using of certificate
7. link for client certificate example on using .jks as truststore and .p12 as keystore
8. two ways to parse json response and 1 way to reconstruct into json
Map responseBody = RestAssuredClientFactory.responseToObject(response, Map)
assert responseBody.BSBResponse.BSBName
@Step("Map response to generic Object")
public static <T> T responseToObject(Response response, Class<T> responseType) {
return response.getBody().as(responseType);
}
or
response.jsonPath().get("results[0].data.type")
then
new JsonBuilder(payload).toPrettyString()
9. do not use default charset in content header
RestAssured.config =
RestAssured.config().encoderConfig(
RestAssured.config().encoderConfig
.appendDefaultContentCharsetToContentTypeIfUndefined(false))
10. create json payload
ObjectMapper objectMapper = new ObjectMapper()
def payload = [
assetId: assetId,
udid: playerEnum.udid
]
objectMapper.writeValueAsString(payload)
11. package io.restassured.path.json
public class JsonPath {}
public class JsonPath {}
can be used to parse json STRING in general
import com.fasterxml.jackson.databind.ObjectMapper
String path = FileSystem.getResourcePath(fileName)
Map map = objectMapper.convertValue(loadFile(path), Map) //convert from jsonNode to map
map.channel.name = channelName //change value
objectMapper.writeValueAsString(map) //convert back to json
static JsonNode loadFile(String file) {
InputStream fileStream = new FileInputStream(file)
objectMapper.readValue(fileStream, JsonNode)
}
13. groovy define json payload
def payload = [records: []]
payload.records << [
key : [
channelName: playList.channelName,
origin : playList.origin
],
value: [
channel: [
name : playList.channelName,
enabled: false
],
date : playList.playlistDate
]
reference
1. getting started
2. user guide
3. pass parameter in restassured
4. free online rest api
5. sample rest assured project
6. advanced restassured
7. testing-rest-endpoints-using-rest-assured
8. best practice
9. powerful Jayway JsonPath
10. restassured with groovy
11. multipart form data file upload
12. SSLITest.java
13. client certificate using jks and p12
No comments:
Post a Comment