Skip to content

Commit b194766

Browse files
feat(assertions): add "Then I see response body contains"
1 parent 9c5428a commit b194766

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

cypress/e2e/httpbin/get.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Feature: httpbin
66
Scenario: GET response body
77
Given I make a "GET" request to "https://httpbin.org/base64/SFRUUEJJTiBpcyBhd2Vzb21l"
88
Then I see response body "HTTPBIN is awesome"
9+
And I see response body contains "awesome"

cypress/e2e/typicode/jsonplaceholder.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Feature: JSONPlaceholder
33
Given I make a "GET" request to "https://jsonplaceholder.typicode.com/todos/1"
44
Then I see response status 200
55
And I see response body '{"userId":1,"id":1,"title":"delectus aut autem","completed":false}'
6+
And I see response body contains '{"id":1,"userId":1}'

src/assertions/response.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Then('I see response status {int}', Then_I_see_response_status);
4242
* @example
4343
*
4444
* ```gherkin
45-
* Then I see response body "OK
45+
* Then I see response body "OK"
4646
* ```
4747
*
4848
* @remarks
@@ -57,11 +57,48 @@ Then('I see response status {int}', Then_I_see_response_status);
5757
export function Then_I_see_response_body(body: string) {
5858
getCypressElement().should((response: Cypress.Response<object>) => {
5959
if (typeof response.body === 'object') {
60-
expect(response.body).to.eql(JSON.parse(body));
60+
expect(response.body).to.deep.equal(JSON.parse(body));
6161
} else {
6262
expect(response.body).to.equal(body);
6363
}
6464
});
6565
}
6666

6767
Then('I see response body {string}', Then_I_see_response_body);
68+
69+
/**
70+
* Then I see response body contains:
71+
*
72+
* ```gherkin
73+
* Then I see response body contains {string}
74+
* ```
75+
*
76+
* @example
77+
*
78+
* ```gherkin
79+
* Then I see response body contains "OK"
80+
* ```
81+
*
82+
* @remarks
83+
*
84+
* A preceding step like {@link When_I_make_a_request | "When I make a request"} is required. For example:
85+
*
86+
* ```gherkin
87+
* When I make a "GET" request to "/user.json"
88+
* Then I see response body contains '{"name":"Mark"}'
89+
* ```
90+
*/
91+
export function Then_I_see_response_body_contains(body: string) {
92+
getCypressElement().should((response: Cypress.Response<object>) => {
93+
if (typeof response.body === 'object') {
94+
expect(response.body).to.deep.include(JSON.parse(body));
95+
} else {
96+
expect(response.body).to.include(body);
97+
}
98+
});
99+
}
100+
101+
Then(
102+
'I see response body contains {string}',
103+
Then_I_see_response_body_contains,
104+
);

0 commit comments

Comments
 (0)