Cleanup tests (#769)

This patch:
- cleans up a bunch of try-catch statements in favor of a concise pattern
- renames "Browser" test suite into "Puppeteer" test suite
This commit is contained in:
Andrey Lushnikov 2017-09-13 12:06:57 -07:00 committed by GitHub
parent 9779085b7c
commit 010872012f

View File

@ -81,59 +81,58 @@ afterAll(SX(async function() {
]); ]);
})); }));
describe('Browser', function() { describe('Puppeteer', function() {
it('Browser.Options.ignoreHTTPSErrors', SX(async function() { describe('Puppeteer.launch', function() {
const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions); it('should support ignoreHTTPSErrors option', SX(async function() {
const browser = await puppeteer.launch(options); const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
const page = await browser.newPage(); const browser = await puppeteer.launch(options);
let error = null; const page = await browser.newPage();
let response = null; let error = null;
try { const response = await page.goto(HTTPS_PREFIX + '/empty.html').catch(e => error = e);
response = await page.goto(HTTPS_PREFIX + '/empty.html'); expect(error).toBe(null);
} catch (e) { expect(response.ok).toBe(true);
error = e; browser.close();
} }));
expect(error).toBe(null); it('should reject all promises when browser is closed', SX(async function() {
expect(response.ok).toBe(true); const browser = await puppeteer.launch(defaultBrowserOptions);
browser.close(); const page = await browser.newPage();
})); let error = null;
it('should reject all promises when browser is closed', SX(async function() { const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e);
const browser = await puppeteer.launch(defaultBrowserOptions); browser.close();
const page = await browser.newPage(); await neverResolves;
let error = null; expect(error.message).toContain('Protocol error');
const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e); }));
browser.close(); it('userDataDir option', SX(async function() {
await neverResolves; const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
expect(error.message).toContain('Protocol error'); const options = Object.assign({userDataDir}, defaultBrowserOptions);
})); const browser = await puppeteer.launch(options);
it('Puppeteer.connect', SX(async function() { expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
const originalBrowser = await puppeteer.launch(defaultBrowserOptions); browser.close();
const browser = await puppeteer.connect({ expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
browserWSEndpoint: originalBrowser.wsEndpoint() rm(userDataDir);
}); }));
const page = await browser.newPage(); it('userDataDir argument', SX(async function() {
expect(await page.evaluate(() => 7 * 8)).toBe(56); const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
originalBrowser.close(); const options = Object.assign({}, defaultBrowserOptions);
})); options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
it('userDataDir option', SX(async function() { const browser = await puppeteer.launch(options);
const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir')); expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
const options = Object.assign({userDataDir}, defaultBrowserOptions); browser.close();
const browser = await puppeteer.launch(options); expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); rm(userDataDir);
browser.close(); }));
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); });
rm(userDataDir); describe('Puppeteer.connect', function() {
})); it('should work', SX(async function() {
it('userDataDir argument', SX(async function() { const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir')); const browser = await puppeteer.connect({
const options = Object.assign({}, defaultBrowserOptions); browserWSEndpoint: originalBrowser.wsEndpoint()
options.args = [`--user-data-dir=${userDataDir}`].concat(options.args); });
const browser = await puppeteer.launch(options); const page = await browser.newPage();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); expect(await page.evaluate(() => 7 * 8)).toBe(56);
browser.close(); originalBrowser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); }));
rm(userDataDir); });
}));
}); });
describe('Page', function() { describe('Page', function() {
@ -176,11 +175,7 @@ describe('Page', function() {
const neverResolves = newPage.evaluate(() => new Promise(r => {})); const neverResolves = newPage.evaluate(() => new Promise(r => {}));
newPage.close(); newPage.close();
let error = null; let error = null;
try { await neverResolves.catch(e => error = e);
await neverResolves;
} catch (e) {
error = e;
}
expect(error.message).toContain('Protocol error'); expect(error.message).toContain('Protocol error');
})); }));
}); });
@ -216,11 +211,7 @@ describe('Page', function() {
})); }));
it('should reject promise with exception', SX(async function() { it('should reject promise with exception', SX(async function() {
let error = null; let error = null;
try { await page.evaluate(() => not.existing.object.property).catch(e => error = e);
await page.evaluate(() => not.existing.object.property);
} catch (e) {
error = e;
}
expect(error).toBeTruthy(); expect(error).toBeTruthy();
expect(error.message).toContain('not is not defined'); expect(error.message).toContain('not is not defined');
})); }));
@ -455,12 +446,9 @@ describe('Page', function() {
document.querySelector = null; document.querySelector = null;
}); });
await page.goto(EMPTY_PAGE); await page.goto(EMPTY_PAGE);
try { let error = null;
await page.waitForSelector('*'); await page.waitForSelector('*').catch(e => error = e);
fail('Failed waitForSelector did not throw.'); expect(error.message).toContain('document.querySelector is not a function');
} catch (e) {
expect(e.message).toContain('document.querySelector is not a function');
}
})); }));
it('should throw when frame is detached', SX(async function() { it('should throw when frame is detached', SX(async function() {
await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE); await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE);
@ -532,12 +520,9 @@ describe('Page', function() {
await watchdog; await watchdog;
})); }));
it('should throw when unknown type', SX(async function() { it('should throw when unknown type', SX(async function() {
try { let error = null;
await page.waitFor({foo: 'bar'}); await page.waitFor({foo: 'bar'}).catch(e => error = e);
fail('Failed to throw exception'); expect(error.message).toContain('Unsupported target type');
} catch (e) {
expect(e.message).toContain('Unsupported target type');
}
})); }));
}); });
@ -595,11 +580,7 @@ describe('Page', function() {
})); }));
it('should fail when navigating to bad url', SX(async function() { it('should fail when navigating to bad url', SX(async function() {
let error = null; let error = null;
try { await page.goto('asdfasdf').catch(e => error = e);
await page.goto('asdfasdf');
} catch (e) {
error = e;
}
expect(error.message).toContain('Cannot navigate to invalid URL'); expect(error.message).toContain('Cannot navigate to invalid URL');
})); }));
it('should fail when navigating to bad SSL', SX(async function() { it('should fail when navigating to bad SSL', SX(async function() {
@ -609,20 +590,12 @@ describe('Page', function() {
page.on('requestfinished', request => expect(request).toBeTruthy()); page.on('requestfinished', request => expect(request).toBeTruthy());
page.on('requestfailed', request => expect(request).toBeTruthy()); page.on('requestfailed', request => expect(request).toBeTruthy());
let error = null; let error = null;
try { await page.goto(HTTPS_PREFIX + '/empty.html').catch(e => error = e);
await page.goto(HTTPS_PREFIX + '/empty.html');
} catch (e) {
error = e;
}
expect(error.message).toContain('SSL Certificate error'); expect(error.message).toContain('SSL Certificate error');
})); }));
it('should fail when main resources failed to load', SX(async function() { it('should fail when main resources failed to load', SX(async function() {
let error = null; let error = null;
try { await page.goto('http://localhost:44123/non-existing-url').catch(e => error = e);
await page.goto('http://localhost:44123/non-existing-url');
} catch (e) {
error = e;
}
expect(error.message).toContain('Failed to navigate'); expect(error.message).toContain('Failed to navigate');
})); }));
it('should fail when exceeding maximum navigation timeout', SX(async function() { it('should fail when exceeding maximum navigation timeout', SX(async function() {
@ -903,11 +876,7 @@ describe('Page', function() {
await page.setRequestInterceptionEnabled(true); await page.setRequestInterceptionEnabled(true);
page.on('request', request => request.abort()); page.on('request', request => request.abort());
let error = null; let error = null;
try { await page.goto(EMPTY_PAGE).catch(e => error = e);
await page.goto(EMPTY_PAGE);
} catch (e) {
error = e;
}
expect(error).toBeTruthy(); expect(error).toBeTruthy();
expect(error.message).toContain('Failed to navigate'); expect(error.message).toContain('Failed to navigate');
})); }));
@ -1240,12 +1209,9 @@ describe('Page', function() {
})); }));
it('should fail to click a missing button', SX(async function() { it('should fail to click a missing button', SX(async function() {
await page.goto(PREFIX + '/input/button.html'); await page.goto(PREFIX + '/input/button.html');
try { let error = null;
await page.click('button.does-not-exist'); await page.click('button.does-not-exist').catch(e => error = e);
fail('Clicking the button did not throw.'); expect(error.message).toBe('No node found for selector: button.does-not-exist');
} catch (error) {
expect(error.message).toBe('No node found for selector: button.does-not-exist');
}
})); }));
// @see https://github.com/GoogleChrome/puppeteer/issues/161 // @see https://github.com/GoogleChrome/puppeteer/issues/161
it('should not hang with touch-enabled viewports', SX(async function() { it('should not hang with touch-enabled viewports', SX(async function() {
@ -1843,11 +1809,7 @@ describe('Page', function() {
await page.setJavaScriptEnabled(false); await page.setJavaScriptEnabled(false);
await page.goto('data:text/html, <script>var something = "forbidden"</script>'); await page.goto('data:text/html, <script>var something = "forbidden"</script>');
let error = null; let error = null;
try { await page.evaluate('something').catch(e => error = e);
await page.evaluate('something');
} catch (e) {
error = e;
}
expect(error.message).toContain('something is not defined'); expect(error.message).toContain('something is not defined');
await page.setJavaScriptEnabled(true); await page.setJavaScriptEnabled(true);
@ -2052,11 +2014,7 @@ describe('Page', function() {
await page.tracing.start({path: outputFile}); await page.tracing.start({path: outputFile});
const newPage = await browser.newPage(); const newPage = await browser.newPage();
let error = null; let error = null;
try { await newPage.tracing.start({path: outputFile}).catch(e => error = e);
await newPage.tracing.start({path: outputFile});
} catch (e) {
error = e;
}
await newPage.close(); await newPage.close();
expect(error).toBeTruthy(); expect(error).toBeTruthy();
await page.tracing.stop(); await page.tracing.stop();