Skip to content

Commit 4cbfa09

Browse files
authored
test: remove describes (5) (microsoft#3294)
1 parent 1673e62 commit 4cbfa09

23 files changed

+1535
-1397
lines changed

test/browsertype-basic.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
* Modifications copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const path = require('path');
19+
const fs = require('fs');
20+
const utils = require('./utils');
21+
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
22+
23+
it('browserType.executablePath should work', async({browserType}) => {
24+
const executablePath = browserType.executablePath();
25+
expect(fs.existsSync(executablePath)).toBe(true);
26+
expect(fs.realpathSync(executablePath)).toBe(executablePath);
27+
});
28+
29+
it('browserType.name should work', async({browserType}) => {
30+
if (WEBKIT)
31+
expect(browserType.name()).toBe('webkit');
32+
else if (FFOX)
33+
expect(browserType.name()).toBe('firefox');
34+
else if (CHROMIUM)
35+
expect(browserType.name()).toBe('chromium');
36+
else
37+
throw new Error('Unknown browser');
38+
});

test/browsertype-connect.spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
* Modifications copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const path = require('path');
19+
const fs = require('fs');
20+
const utils = require('./utils');
21+
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
22+
23+
it.slow()('should be able to reconnect to a browser', async({browserType, defaultBrowserOptions, server}) => {
24+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
25+
{
26+
const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
27+
const browserContext = await browser.newContext();
28+
const page = await browserContext.newPage();
29+
await page.goto(server.EMPTY_PAGE);
30+
await browser.close();
31+
}
32+
{
33+
const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
34+
const browserContext = await browser.newContext();
35+
const page = await browserContext.newPage();
36+
await page.goto(server.EMPTY_PAGE);
37+
await browser.close();
38+
}
39+
await browserServer._checkLeaks();
40+
await browserServer.close();
41+
});
42+
43+
it.fail(USES_HOOKS || (CHROMIUM && WIN)).slow()('should handle exceptions during connect', async({browserType, defaultBrowserOptions, server}) => {
44+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
45+
const __testHookBeforeCreateBrowser = () => { throw new Error('Dummy') };
46+
const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint(), __testHookBeforeCreateBrowser }).catch(e => e);
47+
await browserServer._checkLeaks();
48+
await browserServer.close();
49+
expect(error.message).toContain('Dummy');
50+
});
51+
52+
it('should set the browser connected state', async ({browserType, defaultBrowserOptions}) => {
53+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
54+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
55+
expect(remote.isConnected()).toBe(true);
56+
await remote.close();
57+
expect(remote.isConnected()).toBe(false);
58+
await browserServer._checkLeaks();
59+
await browserServer.close();
60+
});
61+
62+
it('should throw when used after isConnected returns false', async({browserType, defaultBrowserOptions}) => {
63+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
64+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
65+
const page = await remote.newPage();
66+
await Promise.all([
67+
browserServer.close(),
68+
new Promise(f => remote.once('disconnected', f)),
69+
]);
70+
expect(remote.isConnected()).toBe(false);
71+
const error = await page.evaluate('1 + 1').catch(e => e);
72+
expect(error.message).toContain('has been closed');
73+
});
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
* Modifications copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const path = require('path');
19+
const fs = require('fs');
20+
const utils = require('./utils');
21+
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
22+
23+
it('should work', async({browserType, defaultBrowserOptions}) => {
24+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
25+
const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
26+
const browserContext = await browser.newContext();
27+
expect(browserContext.pages().length).toBe(0);
28+
expect(browserServer.wsEndpoint()).not.toBe(null);
29+
const page = await browserContext.newPage();
30+
expect(await page.evaluate('11 * 11')).toBe(121);
31+
await page.close();
32+
await browser.close();
33+
await browserServer._checkLeaks();
34+
await browserServer.close();
35+
});
36+
37+
it('should fire "disconnected" when closing the server', async({browserType, defaultBrowserOptions}) => {
38+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
39+
const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
40+
const disconnectedEventPromise = new Promise(resolve => browser.once('disconnected', resolve));
41+
const closedPromise = new Promise(f => browserServer.on('close', f));
42+
browserServer.kill();
43+
await Promise.all([
44+
disconnectedEventPromise,
45+
closedPromise,
46+
]);
47+
});
48+
49+
it('should fire "close" event during kill', async({browserType, defaultBrowserOptions}) => {
50+
const order = [];
51+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
52+
const closedPromise = new Promise(f => browserServer.on('close', () => {
53+
order.push('closed');
54+
f();
55+
}));
56+
await Promise.all([
57+
browserServer.kill().then(() => order.push('killed')),
58+
closedPromise,
59+
]);
60+
expect(order).toEqual(['closed', 'killed']);
61+
});
62+
63+
it('should return child_process instance', async ({browserType, defaultBrowserOptions}) => {
64+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
65+
expect(browserServer.process().pid).toBeGreaterThan(0);
66+
await browserServer.close();
67+
});
68+
69+
it('should fire close event', async ({browserType, defaultBrowserOptions}) => {
70+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
71+
const [result] = await Promise.all([
72+
new Promise(f => browserServer.on('close', (exitCode, signal) => f({ exitCode, signal }))),
73+
browserServer.close(),
74+
]);
75+
expect(result.exitCode).toBe(0);
76+
expect(result.signal).toBe(null);
77+
});
78+
79+
it('should reject navigation when browser closes', async({browserType, defaultBrowserOptions, server}) => {
80+
server.setRoute('/one-style.css', () => {});
81+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
82+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
83+
const page = await remote.newPage();
84+
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', {timeout: 60000}).catch(e => e);
85+
await server.waitForRequest('/one-style.css');
86+
await remote.close();
87+
const error = await navigationPromise;
88+
expect(error.message).toContain('Navigation failed because page was closed!');
89+
await browserServer._checkLeaks();
90+
await browserServer.close();
91+
});
92+
93+
it('should reject waitForSelector when browser closes', async({browserType, defaultBrowserOptions, server}) => {
94+
server.setRoute('/empty.html', () => {});
95+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
96+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
97+
const page = await remote.newPage();
98+
const watchdog = page.waitForSelector('div', { state: 'attached', timeout: 60000 }).catch(e => e);
99+
100+
// Make sure the previous waitForSelector has time to make it to the browser before we disconnect.
101+
await page.waitForSelector('body', { state: 'attached' });
102+
103+
await remote.close();
104+
const error = await watchdog;
105+
expect(error.message).toContain('Protocol error');
106+
await browserServer._checkLeaks();
107+
await browserServer.close();
108+
});
109+
110+
it('should throw if used after disconnect', async({browserType, defaultBrowserOptions}) => {
111+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
112+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
113+
const page = await remote.newPage();
114+
await remote.close();
115+
const error = await page.evaluate('1 + 1').catch(e => e);
116+
expect(error.message).toContain('has been closed');
117+
await browserServer._checkLeaks();
118+
await browserServer.close();
119+
});
120+
121+
it('should emit close events on pages and contexts', async({browserType, defaultBrowserOptions}) => {
122+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
123+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
124+
const context = await remote.newContext();
125+
const page = await context.newPage();
126+
let pageClosed = false;
127+
page.on('close', e => pageClosed = true);
128+
await Promise.all([
129+
new Promise(f => context.on('close', f)),
130+
browserServer.close()
131+
]);
132+
expect(pageClosed).toBeTruthy();
133+
});
134+
135+
it('should terminate network waiters', async({browserType, defaultBrowserOptions, server}) => {
136+
const browserServer = await browserType.launchServer(defaultBrowserOptions);
137+
const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() });
138+
const newPage = await remote.newPage();
139+
const results = await Promise.all([
140+
newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
141+
newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e),
142+
browserServer.close()
143+
]);
144+
for (let i = 0; i < 2; i++) {
145+
const message = results[i].message;
146+
expect(message).toContain('Page closed');
147+
expect(message).not.toContain('Timeout');
148+
}
149+
});

test/browsertype-launch.spec.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
* Modifications copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const path = require('path');
19+
const fs = require('fs');
20+
const utils = require('./utils');
21+
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
22+
23+
it('should reject all promises when browser is closed', async({browserType, defaultBrowserOptions}) => {
24+
const browser = await browserType.launch(defaultBrowserOptions);
25+
const page = await (await browser.newContext()).newPage();
26+
let error = null;
27+
const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e);
28+
await page.evaluate(() => new Promise(f => setTimeout(f, 0)));
29+
await browser.close();
30+
await neverResolves;
31+
expect(error.message).toContain('Protocol error');
32+
});
33+
34+
it('should throw if userDataDir option is passed', async({browserType, defaultBrowserOptions}) => {
35+
let waitError = null;
36+
const options = Object.assign({}, defaultBrowserOptions, {userDataDir: 'random-path'});
37+
await browserType.launch(options).catch(e => waitError = e);
38+
expect(waitError.message).toContain('launchPersistentContext');
39+
});
40+
41+
it.skip(FFOX)('should throw if page argument is passed', async({browserType, defaultBrowserOptions}) => {
42+
let waitError = null;
43+
const options = Object.assign({}, defaultBrowserOptions, { args: ['http://example.com'] });
44+
await browserType.launch(options).catch(e => waitError = e);
45+
expect(waitError.message).toContain('can not specify page');
46+
});
47+
48+
it.fail(true)('should reject if launched browser fails immediately', async({browserType, defaultBrowserOptions}) => {
49+
// I'm getting ENCONRESET on this one.
50+
const options = Object.assign({}, defaultBrowserOptions, {executablePath: path.join(__dirname, 'assets', 'dummy_bad_browser_executable.js')});
51+
let waitError = null;
52+
await browserType.launch(options).catch(e => waitError = e);
53+
expect(waitError.message).toContain('== logs ==');
54+
});
55+
56+
it('should reject if executable path is invalid', async({browserType, defaultBrowserOptions}) => {
57+
let waitError = null;
58+
const options = Object.assign({}, defaultBrowserOptions, {executablePath: 'random-invalid-path'});
59+
await browserType.launch(options).catch(e => waitError = e);
60+
expect(waitError.message).toContain('Failed to launch');
61+
});
62+
63+
it.skip(USES_HOOKS)('should handle timeout', async({browserType, defaultBrowserOptions}) => {
64+
const options = { ...defaultBrowserOptions, timeout: 5000, __testHookBeforeCreateBrowser: () => new Promise(f => setTimeout(f, 6000)) };
65+
const error = await browserType.launch(options).catch(e => e);
66+
expect(error.message).toContain(`browserType.launch: Timeout 5000ms exceeded.`);
67+
expect(error.message).toContain(`[browser] <launching>`);
68+
expect(error.message).toContain(`[browser] <launched> pid=`);
69+
});
70+
71+
it.skip(USES_HOOKS)('should handle exception', async({browserType, defaultBrowserOptions}) => {
72+
const e = new Error('Dummy');
73+
const options = { ...defaultBrowserOptions, __testHookBeforeCreateBrowser: () => { throw e; }, timeout: 9000 };
74+
const error = await browserType.launch(options).catch(e => e);
75+
expect(error.message).toContain('Dummy');
76+
});
77+
78+
it.skip(USES_HOOKS)('should report launch log', async({browserType, defaultBrowserOptions}) => {
79+
const e = new Error('Dummy');
80+
const options = { ...defaultBrowserOptions, __testHookBeforeCreateBrowser: () => { throw e; }, timeout: 9000 };
81+
const error = await browserType.launch(options).catch(e => e);
82+
expect(error.message).toContain('<launching>');
83+
});
84+
85+
it.slow()('should accept objects as options', async({browserType, defaultBrowserOptions}) => {
86+
const browser = await browserType.launch({ ...defaultBrowserOptions, process });
87+
await browser.close();
88+
});
89+
90+
it('should fire close event for all contexts', async({browserType, defaultBrowserOptions}) => {
91+
const browser = await browserType.launch(defaultBrowserOptions);
92+
const context = await browser.newContext();
93+
let closed = false;
94+
context.on('close', () => closed = true);
95+
await browser.close();
96+
expect(closed).toBe(true);
97+
});
98+
99+
it('should be callable twice', async({browserType, defaultBrowserOptions}) => {
100+
const browser = await browserType.launch(defaultBrowserOptions);
101+
await Promise.all([
102+
browser.close(),
103+
browser.close(),
104+
]);
105+
await browser.close();
106+
});

0 commit comments

Comments
 (0)