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,18 +81,14 @@ afterAll(SX(async function() {
]); ]);
})); }));
describe('Browser', function() { describe('Puppeteer', function() {
it('Browser.Options.ignoreHTTPSErrors', SX(async function() { describe('Puppeteer.launch', function() {
it('should support ignoreHTTPSErrors option', SX(async function() {
const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions); const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
const browser = await puppeteer.launch(options); const browser = await puppeteer.launch(options);
const page = await browser.newPage(); const page = await browser.newPage();
let error = null; let error = null;
let response = null; const response = await page.goto(HTTPS_PREFIX + '/empty.html').catch(e => error = e);
try {
response = await page.goto(HTTPS_PREFIX + '/empty.html');
} catch (e) {
error = e;
}
expect(error).toBe(null); expect(error).toBe(null);
expect(response.ok).toBe(true); expect(response.ok).toBe(true);
browser.close(); browser.close();
@ -106,15 +102,6 @@ describe('Browser', function() {
await neverResolves; await neverResolves;
expect(error.message).toContain('Protocol error'); expect(error.message).toContain('Protocol error');
})); }));
it('Puppeteer.connect', SX(async function() {
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
const browser = await puppeteer.connect({
browserWSEndpoint: originalBrowser.wsEndpoint()
});
const page = await browser.newPage();
expect(await page.evaluate(() => 7 * 8)).toBe(56);
originalBrowser.close();
}));
it('userDataDir option', SX(async function() { it('userDataDir option', SX(async function() {
const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir')); const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
const options = Object.assign({userDataDir}, defaultBrowserOptions); const options = Object.assign({userDataDir}, defaultBrowserOptions);
@ -134,6 +121,18 @@ describe('Browser', function() {
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0); expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
rm(userDataDir); rm(userDataDir);
})); }));
});
describe('Puppeteer.connect', function() {
it('should work', SX(async function() {
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
const browser = await puppeteer.connect({
browserWSEndpoint: originalBrowser.wsEndpoint()
});
const page = await browser.newPage();
expect(await page.evaluate(() => 7 * 8)).toBe(56);
originalBrowser.close();
}));
});
}); });
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.');
} catch (error) {
expect(error.message).toBe('No node found for selector: button.does-not-exist'); 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();