-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathindex.spec.ts
More file actions
126 lines (110 loc) · 3.84 KB
/
index.spec.ts
File metadata and controls
126 lines (110 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Page, Request } from '@playwright/test';
import { test, expect } from './index';
let targetPage: Page;
let devtoolsPage: Page;
const beforeProxyRequest: Array<string> = [];
const afterProxyRequest: Array<string> = [];
const proxyUrl = 'http://localhost:3009/mf-manifest.json';
const mockUrl = 'http://localhost:6666/mf-manifest.json';
const targetOrigin = 'http://localhost:3013/basic';
const sleep = (timeout: number) =>
new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
const beforeHandler = (request: Request) => {
const url = request.url();
if (url.includes('manifest.json') && !beforeProxyRequest.includes(url)) {
beforeProxyRequest.push(url);
}
};
const afterHandler = (request: Request) => {
const url = request.url();
if (url.includes('manifest.json')) {
afterProxyRequest.push(url);
}
};
test.beforeEach(async ({ context: browserContext, extensionId }) => {
beforeProxyRequest.length = 0;
afterProxyRequest.length = 0;
targetPage = await browserContext.newPage();
targetPage.on('request', beforeHandler);
await targetPage.goto(targetOrigin);
devtoolsPage = await browserContext.newPage();
const extensionUrl = `chrome-extension://${extensionId}/html/main/index.html`;
await devtoolsPage.goto(extensionUrl);
await devtoolsPage.waitForLoadState('domcontentloaded');
await devtoolsPage.evaluate(async (openUrl: string) => {
const queryTabs = async () => {
try {
const result = chrome.tabs.query(
{
url: `${openUrl}/*`,
},
(tabs) => {
if (tabs && tabs.length) {
window.targetTab = tabs[0];
}
},
);
if (result && typeof (result as any).then === 'function') {
return await result;
}
} catch (e) {
// fall through to callback-based query
}
return await new Promise<any[]>((resolve) => {
chrome.tabs.query({ url: `${openUrl}/*` }, (tabs) => {
resolve(tabs || []);
});
});
};
const tabs = await queryTabs();
if (Array.isArray(tabs) && tabs.length) {
window.targetTab = tabs[0];
}
}, targetOrigin);
await devtoolsPage.waitForFunction(() => Boolean(window.targetTab?.id));
});
test('test proxy', async ({ request }) => {
targetPage.removeListener('request', beforeHandler);
await sleep(3000);
await sleep(3000);
// Setting proxy logic
const addButton = devtoolsPage.locator('[data-set-e2e=e2eAdd]');
await expect(addButton).toBeVisible({ timeout: 60000 });
await addButton.click();
const proxyKeySelect = devtoolsPage.locator('[data-set-e2e=e2eProxyKey]');
await expect(proxyKeySelect).toBeVisible();
await proxyKeySelect.click();
const moduleKeys = await devtoolsPage.$$('.arco-select-option');
for (let i = 0; i < moduleKeys.length; i++) {
const optionEl = moduleKeys[i];
const text = await (await optionEl.getProperty('textContent')).jsonValue();
if (text === 'webpack_provider') {
await optionEl.click();
break;
}
}
await sleep(3000);
// Configure resource forwarding in advance
targetPage.on('request', afterHandler);
const response = await request.fetch(proxyUrl);
const json = await response.json();
await targetPage.route(mockUrl, async (route) => {
await route.fulfill({ json });
});
await sleep(2000);
await sleep(3000);
await devtoolsPage.getByPlaceholder('Custom Manifest URL').fill(mockUrl);
const optionsEle = await devtoolsPage.$$('.arco-select-option');
await optionsEle[0].click();
await sleep(3000);
await targetPage.bringToFront();
expect(beforeProxyRequest).toContain(proxyUrl);
expect(beforeProxyRequest).not.toContain(mockUrl);
expect(afterProxyRequest).toContain(mockUrl);
expect(afterProxyRequest).not.toContain(proxyUrl);
console.log(beforeProxyRequest, afterProxyRequest);
});