-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
144 lines (117 loc) · 4.61 KB
/
test.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* globals page */
/* Keep file in sync with https://github.com/fregante/webext-dynamic-content-scripts/blob/main/test/test.js */
import {describe, beforeAll, it} from '@jest/globals';
import puppeteer from 'expect-puppeteer';
// https://github.com/smooth-code/jest-puppeteer/commit/bcd0415#r76081519
const expect = puppeteer.default;
async function expectToNotMatchElement(window, selector) {
try {
await expect(window).toMatchElement(selector);
throw new Error(`Expected ${selector} element found`);
} catch (error) {
if (!error.message.startsWith(`Element ${selector} not found`)) {
throw error.message;
}
}
}
// TODO: Ensure that the elements are only injected once
// TODO: Test CSS injection
describe('tab', () => {
beforeAll(async () => {
await page.goto('https://ephiframe.vercel.app/Parent-page?iframe=./Framed-page');
});
it('should load page', async () => {
await expect(page).toMatch('Parent page');
});
it('should load static content script', async () => {
await expect(page).toMatchElement('.static');
});
it('should load file-based dynamic content script', async () => {
await expect(page).toMatchElement('.dynamic');
});
it('should load code-based dynamic content script', async () => {
await expect(page).toMatchElement('.dynamic-code');
});
it.skip('should execute dynamic content scripts in the right order', async () => {
await expect(page).toMatchElement('.dynamic + .dynamic-code');
});
it('should load static content script after a reload', async () => {
await page.reload();
await expect(page).toMatchElement('.static');
});
it('should load file-based dynamic content script after a reload', async () => {
await page.reload();
await expect(page).toMatchElement('.dynamic');
});
it('should load code-based dynamic content script after a reload', async () => {
await page.reload();
await expect(page).toMatchElement('.dynamic-code');
});
});
let iframe;
describe('iframe', () => {
beforeAll(async () => {
await page.goto('https://ephiframe.vercel.app/Parent-page?iframe=./Framed-page');
const elementHandle = await page.waitForSelector('iframe');
iframe = await elementHandle.contentFrame();
});
it('should load iframe page', async () => {
await expect(iframe).toMatch('Framed page');
});
it('should load static content script', async () => {
await expect(iframe).toMatchElement('.static');
});
it('should load file-based dynamic content script', async () => {
await expect(iframe).toMatchElement('.dynamic');
});
it('should load code-based dynamic content script', async () => {
await expect(iframe).toMatchElement('.dynamic-code');
});
it.skip('should execute dynamic content scripts in the right order', async () => {
await expect(page).toMatchElement('.dynamic + .dynamic-code');
});
it('should load static content script after a reload', async () => {
await iframe.goto(iframe.url());
await expect(iframe).toMatchElement('.static');
});
it('should load file-based dynamic content script after a reload', async () => {
await iframe.goto(iframe.url());
await expect(iframe).toMatchElement('.dynamic');
});
it('should load code-based dynamic content script after a reload', async () => {
await iframe.goto(iframe.url());
await expect(iframe).toMatchElement('.dynamic-code');
});
});
let iframeOfExcludedParent;
describe('excludeMatches', () => {
beforeAll(async () => {
await page.goto('https://alt-ephiframe.vercel.app/Parent-page?iframe=./Framed-page');
const elementHandle = await page.waitForSelector('iframe');
iframeOfExcludedParent = await elementHandle.contentFrame();
});
it('should load page and iframe', async () => {
await expect(page).toMatchElement('title', {text: 'Parent page'});
await expect(iframeOfExcludedParent).toMatchElement('title', {text: 'Framed page'});
});
it('should load static content script only in iframe', async () => {
await expectToNotMatchElement(page, '.static');
await expect(iframeOfExcludedParent).toMatchElement('.static');
});
it('should load file-based dynamic content script only in iframe', async () => {
await expectToNotMatchElement(page, '.dynamic');
await expect(iframeOfExcludedParent).toMatchElement('.dynamic');
});
it('should load code-based dynamic content script', async () => {
await expectToNotMatchElement(page, '.dynamic-code');
await expect(iframeOfExcludedParent).toMatchElement('.dynamic-code');
});
});
// Uncomment to hold the browser open a little longer
// import {jest} from '@jest/globals';
// jest.setTimeout(10000000);
// describe('hold', () => {
// it('should wait forever', async () => {
// await new Promise(resolve => setTimeout(resolve, 1000000))
// })
// });