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

Commit

Permalink
#85
Browse files Browse the repository at this point in the history
- Refactored interceptor class;
- Fixed interceptor example tests;
- Fixed elementsActions example tests.
- Updated readme file.
  • Loading branch information
ernestas-zekas committed Jan 22, 2021
1 parent 2c1b061 commit ab2e55b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
### Intercept requests ###

* Interceptor helper introduced to take some actions with requests and responses:
* interceptor.abortRequests() allows to abort all requests or requests by url fragment.
* interceptor.abortRequestsDuringAction() allows to abort all requests or requests by url fragment when specific action is being performed.
* interceptor.abortRequests() allows to abort all requests or requests by url glob pattern, regex pattern (More inforamtion in [Playwright documentation](https://playwright.dev/docs/api/class-page?_highlight=page.route#pagerouteurl-handler))
* interceptor.abortRequestsDuringAction() allows to abort all requests or requests by url glob pattern, regex pattern when specific action is being performed (More inforamtion about url matching in [Playwright documentation](https://playwright.dev/docs/api/class-page?_highlight=page.route#pagerouteurl-handler))
* interceptor.getAllRequestsData() allows to get all requests information when specific action is being performed.
* interceptor.waitForRequestAfterAction() waits for specific or any first request and returns all its data.
* interceptor.waitForResponseAfterAction() waits for specific or any first response and returns all its data.
Expand Down
13 changes: 7 additions & 6 deletions example/tests/elementActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,11 @@ describe("Element Actions", () => {
await action();

//Assert
expect(await sliceToClick.getAttributeValue("selected")).toEqual(selectedAttr);
expect(await sliceToClick.getAttributeValue("data:pieClicked")).toEqual(pieClickedAttr);
expect(await toolTip.isVisible()).toBe(true);
expect(await toolTip.text()).toEqual("series-2: 55");
expect(await sliceToClick.getAttributeValue("selected")).toEqual(selectedAttr);
expect(await sliceToClick.getAttributeValue("data:pieClicked")).toEqual(pieClickedAttr);

});

it("should type element's text value", async () => {
Expand Down Expand Up @@ -291,10 +292,10 @@ describe("Element Actions", () => {

it("should get coordinates of element", async () => {
//Arrange
const expectedXCoordinate = 108; //width (200px) / 2 + left margin (8px)
const expectedYCoordinate = 179.875; //height (200px) / 2 + top heading (~79.88px)
const rectangleCanvas = new Element("#canvas");
await page.goto("http://www.cs.sjsu.edu/~mak/archive/CMPE280/code/canvas/rectangles.html");
const expectedXCoordinate = 640; //width: default viewport 1280px / 2
const expectedYCoordinate = 34; //height: top container 68px / 2
const rectangleCanvas = new Element(".w3-container.top");
await page.goto("https://www.w3schools.com/");

//Act
const coordinates = await rectangleCanvas.getCoordinates();
Expand Down
17 changes: 8 additions & 9 deletions example/tests/interceptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Interceptor", () => {
it("should block requests by any url fragment while test case running", async () => {
//Arrange
const navBar = new Element(".navbar");
const requestUrlFragment = "topmenu";
const requestUrlFragment = "**/topmenu.*";

await Interceptor.abortRequests(requestUrlFragment);

Expand All @@ -27,7 +27,7 @@ describe("Interceptor", () => {
await expect(navBar.exists()).resolves.toBeFalsy();

//Act
await page.reload( { waitUntil: "networkidle2" } );
await page.reload();

//Assert
await expect(navBar.exists()).resolves.toBeFalsy();
Expand All @@ -36,7 +36,7 @@ describe("Interceptor", () => {
it("should block requests by any url fragment after abort method is used", async () => {
//Arrange
const navBar = new Element(".navbar");
const requestUrlFragment = "topmenu";
const requestUrlFragment = "**/topmenu.*";

//Act
await page.goto(DemoGuruSite);
Expand All @@ -46,7 +46,7 @@ describe("Interceptor", () => {

//Act
await Interceptor.abortRequests(requestUrlFragment);
await page.reload( { waitUntil: "networkidle2" } );
await page.reload();

//Assert
await expect(navBar.exists()).resolves.toBeFalsy();
Expand All @@ -55,22 +55,22 @@ describe("Interceptor", () => {
it("should block request by any url fragment after action", async () => {
//Arrange
const navBar = new Element(".navbar");
const requestUrlFragment = "topmenu";
const requestUrlFragment = "**/topmenu.*";
await Interceptor.abortRequestsAfterAction(page.goto(DemoGuruSite), requestUrlFragment);

//Assert
await expect(navBar.exists()).resolves.toBeFalsy();

//Act
await page.reload( { waitUntil: "networkidle2" } );
await page.reload();

//Assert
await expect(navBar.exists()).resolves.toBeTruthy();
});

it("should block any request after action", async () => {
//Arrange
await Helpers.goToUrlAndLoad(DemoOpenCartSite);
await page.goto(DemoOpenCartSite);
var alertMessage = null;
page.on("dialog", dialog => {
console.log(`Alert was detected: '${dialog.message()}'`);
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("Interceptor", () => {
await expect(successMessage.isVisible()).resolves.toBeTruthy();
expect(responseAfterAction).toBeTruthy();
console.log(`Request Url after action: '${responseAfterAction.url()}'`);
console.log(`Request Body: '${await responseAfterAction.text()}'`);
console.log(`Response Body: '${await responseAfterAction.text()}'`);
});

it("should detect any request after action", async () => {
Expand All @@ -121,6 +121,5 @@ describe("Interceptor", () => {
await expect(successMessage.isVisible()).resolves.toBeTruthy();
expect(requestAfterAction).toBeTruthy();
console.log(`Request Url after action: '${requestAfterAction.url()}'`);
console.log(`Request Body: '${await requestAfterAction.response().text()}'`);
});
});
24 changes: 8 additions & 16 deletions framework/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@ class Interceptor {
await page.screenshot({ path: `${targetDir}/${filename || Date.now()}.png` });
}

async abortRequests(requestUrlFragment = "") {
const requestStopper = request => {
if (request.url().indexOf(requestUrlFragment) > -1) {
request.abort();
console.log(`Aborted request Url: '${request.url()}'`);
}
else
request.continue();
};
await page.setRequestInterception(true);
page.on("request", requestStopper);
return requestStopper;
async abortRequests(requestUrlFragment = "**") {
await page.route(requestUrlFragment, route => {
route.abort();
console.log(`Aborted request Url: '${route.request().url()}'`);
} );
}

async abortRequestsAfterAction(action, requestUrlFragment = "", waitDuration = 500) {
let requestStopper = await this.abortRequests(requestUrlFragment);
await this.abortRequests(requestUrlFragment);
await action;
await page.waitFor(waitDuration);
page.removeListener("request", requestStopper);
await page.setRequestInterception(false);
await page.waitForTimeout(waitDuration);
await page.unroute(requestUrlFragment);
}

async getAllRequestsData(action) {
Expand Down

0 comments on commit ab2e55b

Please sign in to comment.