Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
- Added console logs support in environment.js file;
Browse files Browse the repository at this point in the history
- Added enableConsoleLogs method in helpers class;
- Refactored goToUrlAndLoad method in helpers class to accept timeout parameter;
- Changed eslintrc.js file parameter linebreak-style to unix.
  • Loading branch information
ernestas-zekas committed May 15, 2020
1 parent ff1b8f6 commit e836d4d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
],
"linebreak-style": [
"error",
"windows"
"unix"
],
"quotes": [
"error",
Expand Down
6 changes: 3 additions & 3 deletions example/tests/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global page:true browser*/
/*global page:true*/
import { Element, Helpers } from "test-juggler";
const fs = require("fs");

Expand Down Expand Up @@ -40,11 +40,11 @@ describe("Helpers", () => {

it("should wait for navigation to be finished", async () => {
//Arrange
page = await browser.newPage();
const progressLoader = new Element("html.nprogress-busy");
const loadTimeout = 30000;

//Act
await helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/");
await helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/", loadTimeout);

//Assert
await expect(progressLoader.exists()).resolves.toBeFalsy();
Expand Down
1 change: 1 addition & 0 deletions example/tests/interceptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("Interceptor", () => {
console.log(`Running test: '${jasmine["currentTest"].fullName}'`);
//this is workaraound to avoid 'Request is already handled!' error. Shoud be removed when https://github.com/smooth-code/jest-puppeteer/issues/308 defect is fixed.
page = await browser.newPage();
helpers.enableConsoleLogs();
});

it("should block requests by any url fragment while test case running", async () => {
Expand Down
30 changes: 28 additions & 2 deletions framework/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*global page*/
const fs = require("fs");
const retry = require("async-retry");
const config = require(process.cwd() + "/framework.config");
const defaultTimeout = config.defaultTimeout;

export default class Helpers {
async takeScreenshot(filename) {
Expand All @@ -22,9 +24,9 @@ export default class Helpers {
});
}

async goToUrlAndLoad(url) {
async goToUrlAndLoad(url, timeout = defaultTimeout) {
await page.goto(url, {
waitUntil: "networkidle0"
waitUntil: "networkidle0", timeout: timeout
});
}

Expand All @@ -33,4 +35,28 @@ export default class Helpers {
const elementHandle = await page.$(selector);
return await elementHandle.contentFrame();
}

async enableConsoleLogs() {
const chalk = require("chalk");
page
.on("console", msg => {
const type = msg.type().substr(0, 3).toUpperCase();
const colors = {
LOG: text => text,
ERR: chalk.red,
WAR: chalk.yellow,
INF: chalk.cyan
};
const color = colors[type] || chalk.blue;
console.log(color(`${type} ${msg.text()}`));
})
.on("pageerror", ({ message }) => console.log(chalk.red(message)))
.on("response", response => {
if (response.status() > 399)
{
console.log(chalk.red(`${response.status()} ${response.url()}`));
}})
.on("requestfailed", request =>
console.log(chalk.magenta(`${request.failure().errorText} ${request.url()}`)));
}
}
21 changes: 21 additions & 0 deletions test-environment/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ class CustomEnvironment extends PuppeteerEnvironment {
async setup() {
await super.setup();
this.global.page.setDefaultTimeout(config.defaultTimeout);
const chalk = require("chalk");
this.global.page
.on("console", msg => {
const type = msg.type().substr(0, 3).toUpperCase();
const colors = {
LOG: text => text,
ERR: chalk.red,
WAR: chalk.yellow,
INF: chalk.cyan
};
const color = colors[type] || chalk.blue;
console.log(color(`${type} ${msg.text()}`));
})
.on("pageerror", ({ message }) => console.log(chalk.red(message)))
.on("response", response => {
if (response.status() > 399)
{
console.log(chalk.red(`${response.status()} ${response.url()}`));
}})
.on("requestfailed", request =>
console.log(chalk.magenta(`${request.failure().errorText} ${request.url()}`)));
if (config.useTracing) {
const targetDir = "./logs/Timelines";
fs.mkdirSync(targetDir, { recursive: true });
Expand Down

0 comments on commit e836d4d

Please sign in to comment.