Skip to content

Commit

Permalink
mimic http keyword response with wait for response
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Oct 20, 2020
1 parent 4122489 commit 95d1ec5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Browser/keywords/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def _wait_for_http(self, method: Literal["Request", "Response"], matcher, timeou
)
logger.debug(response.log)
# Add format response back here
return response.body if method == "Request" else json.loads(response.json)
return (
response.body
if method == "Request"
else _format_response(json.loads(response.json))
)

@keyword(tags=["Wait", "HTTP"])
def wait_for_request(self, matcher: str = "", timeout: str = "") -> Any:
Expand Down
24 changes: 18 additions & 6 deletions atest/test/05_JS_Tests/http_with_waiting.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ Suite Setup New Page ${LOGIN_URL}

*** Test Cases ***
GET with waiting json response
${promise}= Promise To Wait For Response matcher=/api/get/json timeout=3s
${promise}= Promise To Wait For Response matcher=/api/get/json timeout=3s
&{response}= HTTP /api/get/json
${content}= Wait For ${promise}
Should be equal ${content} ${response}[body]
${content}= Wait For ${promise}
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]

POST with waiting json response
${promise}= Promise To Wait For Response matcher=/api/post timeout=3s
${promise}= Promise To Wait For Response matcher=/api/post timeout=3s
&{response}= HTTP /api/post POST {"name": "George"}
${content}= Wait For ${promise}
Should be equal ${content} ${response}[body]
${content}= Wait For ${promise}
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]

GET with text response
${promise}= Promise To Wait For Response matcher=/api/get/text timeout=3s
&{response}= HTTP /api/get/text
${content}= Wait For ${promise}
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]
20 changes: 17 additions & 3 deletions node/playwright-wrapper/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,23 @@ export async function waitForResponse(
) {
const urlOrPredicate = new RegExp(`.*${call.request.getUrlorpredicate()}`);
const timeout = call.request.getTimeout();
const result = await invokeOnPage(page, callback, 'waitForResponse', urlOrPredicate, { timeout: timeout });
const body = await result.json();
callback(null, jsonResponse(JSON.stringify(body), ''));
const data = await invokeOnPage(page, callback, 'waitForResponse', urlOrPredicate, {
timeout: timeout,
});
callback(
null,
jsonResponse(
JSON.stringify({
status: data.status(),
body: await data.text(),
headers: JSON.stringify(data.headers()),
statusText: data.statusText(),
url: data.url(),
ok: data.ok(),
}),
'',
),
);
}
export async function waitForRequest(
call: ServerUnaryCall<pb.Request.HttpCapture>,
Expand Down

0 comments on commit 95d1ec5

Please sign in to comment.