playwright#Response TypeScript Examples
The following examples show how to use
playwright#Response.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: stringify-request.ts From playwright-fluent with MIT License | 6 votes |
async function toJsonOrText(response: Response): Promise<string | unknown> {
try {
const payload = await response.json();
return payload;
} catch (error) {
const payload = await response.text();
return encodeHtml(payload);
}
}
Example #2
Source File: get-outdated-mocks.ts From playwright-fluent with MIT License | 5 votes |
async function tryGetContentAsJson(response: Response): Promise<unknown> {
try {
return await response.json();
} catch (e) {
return undefined;
}
}
Example #3
Source File: get-outdated-mocks.ts From playwright-fluent with MIT License | 5 votes |
async function tryGetContentAsText(response: Response): Promise<string | undefined> {
try {
return await response.text();
} catch (e) {
return undefined;
}
}
Example #4
Source File: common.ts From jitsu with MIT License | 5 votes |
public async init() {
this.app = express();
this.app.use(bodyParser.json());
this.app.use(bodyParser.text());
const { port, shutdown } = await createServer(this.app, this.port);
this.port = port;
this.shutdown = shutdown;
let libJs = __dirname + "/../../dist/web/lib.js";
if (!fs.existsSync(libJs)) {
throw new Error(
`File ${libJs} does not exists. Please, run the build first!`
);
}
this.app.use("/s/lib.js", express.static(libJs));
this.app.get("/test-case/:name", (req, res, next) => {
let path = `${__dirname}/../html/${req.params.name}`;
let html = fs.readFileSync(path).toString();
res.send(html.split("%%SERVER%%").join(`http://localhost:${this.port}`));
next();
});
const eventHandler = (req, res: core.Response, next) => {
let bodyJson =
typeof req.body === "object" ? req.body : JSON.parse(req.body);
this._requestLog.push(bodyJson);
console.log(
"Received payload from JS SDK",
JSON.stringify(bodyJson, null, 2)
);
if (bodyJson.jitsu_sdk_extras) {
//to simulate synchronous destination produce response from incoming event.
res.send(
JSON.stringify({ jitsu_sdk_extras: bodyJson.jitsu_sdk_extras })
);
} else {
res.send("{}");
}
next();
};
this.app.post("/api/v1/event", eventHandler);
this.app.post("/api.*", eventHandler);
}
Example #5
Source File: common.ts From jitsu with MIT License | 5 votes |
/**
* Opens an url with given browser:
* - throws an exception (fails the test) if result is not successful
* - takes a screenshot and saves it to `artifacts`
* @param browser
* @param url
*/
export async function runUrl(
browser: Browser,
url: string
): Promise<PageResult> {
let allRequests: Request[] = [];
let consoleErrors: string[] = [];
const page = await browser.newPage();
page.on("request", (request) => {
console.log("Page requested " + request.url());
allRequests.push(request);
});
page.on("console", (msg: ConsoleMessage) => {
if (msg.type() === "error") {
consoleErrors.push(msg.text());
}
console.log(`Browser console message: [${msg.type()}] ${msg.text()}`);
});
page.on("pageerror", (err) => {
consoleErrors.push(err.message);
console.log(`Browser console error: ${err.message}`);
});
let artifactsDir = __dirname + "/../artifacts/";
let screenshotPath =
artifactsDir +
url.replace("http://", "").replace("https://", "").split("/").join("_") +
".png";
let result = await page.goto(url);
console.log(`Saving screenshot of ${url} to ${screenshotPath}`);
await page.screenshot({ path: screenshotPath });
expect(result?.ok()).toBeTruthy();
console.log("Waiting");
await sleep(4000);
await page.close();
return { allRequests, consoleErrors, pageResponse: result as Response };
}