Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 20 additions & 13 deletions example/tests/interceptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ import { Element, Interceptor } from "test-juggler";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 tests are failing in interceptor suite:

  Interceptor
    √ should block requests by any url fragment using Regex pattern while test case running (7423 ms)
    √ should block requests by any url fragment using Glob pattern after abort method is used (3831 ms)
    √ should block request by any url fragment after action (3455 ms)
    × should block any request after action (30176 ms)
    √ should count all requests (368 ms)
    × should detect specific response after action (30182 ms)
    × should detect any request after action (3 ms)

const DemoQaSite = "https://demoqa.com/books";
const DemoOpenCartSite = "https://demo.opencart.com/";
const successMessage = new Element(".alert-success");
const addToCartButton = new Element(".product-layout:nth-child(1) > div button:nth-child(1)");
const loadingWrapper = new Element("#loading-wrapper");
const booksWrapper = new Element(".books-wrapper");
let page;
let successMessage;
let addToCartButton;
let loadingWrapper;
let booksWrapper;
let interceptor;

describe("Interceptor", () => {
beforeEach(async () => {
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.
const context = await browser.newContext();
page = await context.newPage();
successMessage = new Element(".alert-success", page);
addToCartButton = new Element(".product-layout:nth-child(1) > div button:nth-child(1)", page);
loadingWrapper = new Element("#loading-wrapper", page);
booksWrapper = new Element(".books-wrapper", page);
interceptor = new Interceptor(page);
});

it("should block requests by any url fragment using Regex pattern while test case running", async () => {
//Arrange
const requestUrlRegex = /BookStore/;
await Interceptor.abortRequests(requestUrlRegex);
await interceptor.abortRequests(requestUrlRegex);

//Act
await page.goto(DemoQaSite);
Expand All @@ -41,13 +48,13 @@ describe("Interceptor", () => {

//Act
await page.goto(DemoQaSite);
await loadingWrapper.waitUntilInvisible();
await booksWrapper.waitUntilVisible();

//Assert
await expect(booksWrapper.exists()).resolves.toBeTruthy();

//Act
await Interceptor.abortRequests(requestUrlGlob);
await interceptor.abortRequests(requestUrlGlob);
await page.reload();
await loadingWrapper.waitUntilVisible();

Expand All @@ -59,7 +66,7 @@ describe("Interceptor", () => {
//Arrange

const requestUrlGlob = "**/BookStore/**";
await Interceptor.abortRequestsAfterAction(page.goto(DemoQaSite), requestUrlGlob);
await interceptor.abortRequestsAfterAction(page.goto(DemoQaSite), requestUrlGlob);

//Assert
await loadingWrapper.waitUntilVisible();
Expand All @@ -69,7 +76,7 @@ describe("Interceptor", () => {

//Act
await page.reload();
await loadingWrapper.waitUntilInvisible();
await booksWrapper.waitUntilVisible();

//Assert
await expect(booksWrapper.exists()).resolves.toBeTruthy();
Expand All @@ -86,7 +93,7 @@ describe("Interceptor", () => {
});

//Act
await Interceptor.abortRequestsAfterAction(addToCartButton.click());
await interceptor.abortRequestsAfterAction(addToCartButton.click());

//Assert
await expect(successMessage.isVisible()).resolves.toBeFalsy();
Expand All @@ -95,7 +102,7 @@ describe("Interceptor", () => {

it("should count all requests", async () => {
//Act
var totalRequests = await Interceptor.getAllRequestsData(page.goto(DemoOpenCartSite));
var totalRequests = await interceptor.getAllRequestsData(page.goto(DemoOpenCartSite));

//Assert
expect(totalRequests.length > 0).toBeTruthy();
Expand All @@ -108,7 +115,7 @@ describe("Interceptor", () => {
await page.goto(DemoOpenCartSite);

//Act
var responseAfterAction = await Interceptor.waitForResponseAfterAction(addToCartButton.click(), responseUrlFragment);
var responseAfterAction = await interceptor.waitForResponseAfterAction(addToCartButton.click(), responseUrlFragment);

//Assert
await expect(successMessage.isVisible()).resolves.toBeTruthy();
Expand All @@ -122,7 +129,7 @@ describe("Interceptor", () => {
await page.goto(DemoOpenCartSite);

//Act
var requestAfterAction = await Interceptor.waitForRequestAfterAction(addToCartButton.click());
var requestAfterAction = await interceptor.waitForRequestAfterAction(addToCartButton.click());

//Assert
await expect(successMessage.isVisible()).resolves.toBeTruthy();
Expand Down
24 changes: 14 additions & 10 deletions framework/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const fs = require("fs");

class Interceptor {
export default class Interceptor {

constructor(interceptorPage = page) {
this.page = interceptorPage
}

async takeScreenshot(filename) {
const targetDir = `./logs/${jasmine["currentSuite"].fullName}/${jasmine["currentTest"].description}`;
fs.mkdirSync(targetDir, { recursive: true });
await page.screenshot({ path: `${targetDir}/${filename || Date.now()}.png` });
await this.page.screenshot({ path: `${targetDir}/${filename || Date.now()}.png` });
}

async abortRequests(requestUrlFragment = "**") {
await page.route(requestUrlFragment, route => {
await this.page.route(requestUrlFragment, route => {
route.abort();
console.log(`Aborted request Url: '${route.request().url()}'`);
} );
Expand All @@ -17,34 +22,33 @@ class Interceptor {
async abortRequestsAfterAction(action, requestUrlFragment = "", waitDuration = 500) {
await this.abortRequests(requestUrlFragment);
await action;
await page.waitForTimeout(waitDuration);
await page.unroute(requestUrlFragment);
await this.page.waitForTimeout(waitDuration);
await this.page.unroute(requestUrlFragment);
}

async getAllRequestsData(action) {
let requestsData = [];
const requestLogger = request => {
requestsData.push(request);
};
page.on("request", requestLogger);
this.page.on("request", requestLogger);
await action;
page.removeListener("request", requestLogger);
this.page.removeListener("request", requestLogger);
return requestsData;
}

async waitForRequestAfterAction(action, requestUrlFragment = "") {
const requestAferAction = await page.waitForRequest(request => request.url().indexOf(requestUrlFragment) > -1);
const requestAferAction = await this.page.waitForRequest(request => request.url().indexOf(requestUrlFragment) > -1);
await action;
console.log(`Url: '${requestAferAction.url()}'`);
return requestAferAction;
}

async waitForResponseAfterAction(action, responsetUrlFragment = "") {
const responseAferAction = await page.waitForResponse(response => response.url().indexOf(responsetUrlFragment) > -1);
const responseAferAction = await this.page.waitForResponse(response => response.url().indexOf(responsetUrlFragment) > -1);
await action;
console.log(`Url: '${responseAferAction.url()}'`);
return responseAferAction;
}
}

export default new Interceptor();