Skip to content

Commit

Permalink
HSPC-23 Add request interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
inuur committed Dec 4, 2022
1 parent 92ce34e commit 2e50f5a
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 7 deletions.
7 changes: 7 additions & 0 deletions dist/hstest/environment/page.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/hstest/environment/page.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hs-test-web-ts",
"version": "1.0.0",
"version": "4.0.1",
"description": "Hyperskill Testing Library on TypeScript",
"main": "dist/hstest/index.js",
"scripts": {
Expand Down Expand Up @@ -36,8 +36,8 @@
"style-loader": "^1.2.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.4",
"webpack": "^4.43.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
"webpack-dev-server": "^4.11.1"
}
}
9 changes: 9 additions & 0 deletions hstest/environment/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class Page {
isOpened: boolean;
pageInstance!: puppeteer.Page;
gotoOptions: puppeteer.WaitForOptions;
requests: Array<any>;

constructor(url: string, browser: Browser, gotoOptions: puppeteer.WaitForOptions) {
this.url = url;
this.browser = browser;
this.isOpened = false;
this.gotoOptions = gotoOptions;
this.requests = [];
}

async open(): Promise<void> {
Expand All @@ -29,9 +31,16 @@ class Page {
await this.pageInstance.goto(this.url, this.gotoOptions);
await BrowserPageHandler.initHyperskillContext(this.pageInstance);
await BrowserPageHandler.initKeyboardEvents(this.pageInstance);
await this.setUpRequestInterceptor();
this.isOpened = true;
}

async setUpRequestInterceptor(): Promise<void> {
this.pageInstance.on('request', async request => {
this.requests.push(request);
});
}

execute(func: NoArgsFunction): NoArgsFunction {
return async () => {
await this.open();
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hs-test-web-ts",
"version": "4.0.0",
"version": "4.0.1",
"description": "Hyperskill Testing Library on TypeScript",
"main": "dist/hstest/index.js",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions tests/vanilla/page/requests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index HTML</title>
</head>
<body>

<div id="test">
<button id="http">Make HTTP request</button>
<a href="test.html" id="navigate">Navigate to another page</a>
</div>

</body>
</html>

<script>
document.getElementById('http').addEventListener('click', async () => {
fetch("https://get.geojs.io/v1/ip/country.json?ip=8.8.8.8")
})
</script>
19 changes: 19 additions & 0 deletions tests/vanilla/page/requests/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>

<div id="test">
<button id="http">Make HTTP request</button>
</div>
</body>
</html>

<script>
document.getElementById('http').addEventListener('click', () => {
fetch('https://www.google.com').then(console.log)
})
</script>
33 changes: 33 additions & 0 deletions tests/vanilla/page/requests/testRequestInterception.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {StageTest, correct, wrong} from "../../../../dist/hstest/index.js"
import path from 'path'

global.browserOptions = {
args: ['--start-maximized', '--disable-infobar', '--disable-site-isolation-trials']
}

const pagePath = path.join(import.meta.url, '../index.html')

class TestRequestInterceptionTest extends StageTest {

page = this.getPage(pagePath)

tests = [
this.node.execute(async () => {
const button = await this.page.findById('http');
await button.click()
await new Promise(r => setTimeout(r, 2000));
if (this.page.requests.length !== 1) {
return wrong('Expected only one request!')
}
await button.click()
if (this.page.requests.length !== 2) {
return wrong('Expected 2 requests!')
}
return correct()
})
];
}

it('Test request interception', async () => {
await new TestRequestInterceptionTest().runTests()
}).timeout(15000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {StageTest, correct, wrong} from "../../../../dist/hstest/index.js"
import path from 'path'

const pagePath = path.join(import.meta.url, '../index.html')

class TestRequestInterceptionTest extends StageTest {

page = this.getPage(pagePath)

tests = [
this.node.execute(async () => {
const button = await this.page.findById('http');
await button.click();
if (this.page.requests.length !== 1) {
return wrong('Expected only one request');
}
const navigateButton = await this.page.findById('navigate');
await navigateButton.clickForNavigation();
const anotherButton = await this.page.findById('http');
await anotherButton.click();
if (this.page.requests.length === 1) {
return wrong('Expected more than 1 request');
}
if (this.page.requests[0].url() !== 'https://get.geojs.io/v1/ip/country.json?ip=8.8.8.8') {
return wrong('Wrong url')
}
return correct()
})
];
}

it('Test request interception', async () => {
await new TestRequestInterceptionTest().runTests()
});

0 comments on commit 2e50f5a

Please sign in to comment.