Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

somewhat fix wait for response #432

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
28 changes: 28 additions & 0 deletions atest/test/05_JS_Tests/http_with_waiting.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
*** Settings ***
Resource imports.resource
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
&{response}= HTTP /api/get/json
${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
&{response}= HTTP /api/post POST {"name": "George"}
${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]
15 changes: 12 additions & 3 deletions node/playwright-wrapper/grpc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,22 @@ export class PlaywrightServer implements IPlaywrightServer {
): Promise<void> {
return evaluation.waitForElementState(call, callback, this.state);
}
async waitForRequest(call: ServerUnaryCall<Request.HttpCapture>, callback: sendUnaryData<Response.String>) {
async waitForRequest(
call: ServerUnaryCall<Request.HttpCapture>,
callback: sendUnaryData<Response.String>,
): Promise<void> {
return network.waitForRequest(call, callback, this.getActivePage());
}
async waitForResponse(call: ServerUnaryCall<Request.HttpCapture>, callback: sendUnaryData<Response.Json>) {
async waitForResponse(
call: ServerUnaryCall<Request.HttpCapture>,
callback: sendUnaryData<Response.Json>,
): Promise<void> {
return network.waitForResponse(call, callback, this.getActivePage());
}
async waitUntilNetworkIsIdle(call: ServerUnaryCall<Request.Timeout>, callback: sendUnaryData<Response.Empty>) {
async waitUntilNetworkIsIdle(
call: ServerUnaryCall<Request.Timeout>,
callback: sendUnaryData<Response.Empty>,
): Promise<void> {
return network.waitUntilNetworkIsIdle(call, callback, this.getActivePage());
}

Expand Down
23 changes: 19 additions & 4 deletions node/playwright-wrapper/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ServerUnaryCall, sendUnaryData } from 'grpc';

import { emptyWithLog, jsonResponse, stringResponse } from './response-util';
import { invokeOnPage } from './playwirght-invoke';

export async function httpRequest(
call: ServerUnaryCall<pb.Request.HttpRequest>,
callback: sendUnaryData<pb.Response.Json>,
Expand Down Expand Up @@ -61,11 +62,25 @@ export async function waitForResponse(
callback: sendUnaryData<pb.Response.Json>,
page?: Page,
) {
const urlOrPredicate = call.request.getUrlorpredicate();
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(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