From 6fc54665e4263810b539170e03194ca6d14e13ea Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 12 May 2017 16:36:37 -0700 Subject: [PATCH] Add minimal test framework This patch adds some minimal tests for puppeteer's Page using Jasmine. --- README.md | 12 +++++++++++- package.json | 7 ++++++- test/test.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/test.js diff --git a/README.md b/README.md index 699aa6915f8..a60328ce08f 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,17 @@ node examples/screenshot.js ### Tests +Run all tests: +``` +npm test +``` + Run phantom.js tests using puppeteer: ``` -./third_party/phantomjs/test/run-tests.py +npm run test-phantom +``` + +Run puppeteer tests: +``` +npm run test-puppeteer ``` diff --git a/package.json b/package.json index 4fa90178da6..280e54908a5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "", "main": "index.js", "scripts": { - "test": "python third_party/phantomjs/test/run-tests.py", + "test-puppeteer": "jasmine test/test.js", + "test-phantom": "python third_party/phantomjs/test/run-tests.py", + "test": "npm run test-puppeteer && npm run test-phantom", "install": "node install.js" }, "author": "The Chromium Authors", @@ -24,5 +26,8 @@ }, "puppeteer": { "chromium_revision": "468266" + }, + "devDependencies": { + "jasmine": "^2.6.0" } } diff --git a/test/test.js b/test/test.js new file mode 100644 index 00000000000..77d0b34144e --- /dev/null +++ b/test/test.js @@ -0,0 +1,38 @@ +var Browser = require('../lib/Browser'); + +describe('Page', function() { + var browser; + var page; + + beforeAll(function() { + browser = new Browser(); + }); + + afterAll(function() { + browser.close(); + }); + + beforeEach(SX(async function() { + page = await browser.newPage(); + })); + + afterEach(function() { + page.close(); + }); + + it('Page.evaluate', SX(async function() { + var result = await page.evaluate(() => 7 * 3); + expect(result).toBe(21); + })); + + it('Page.evaluateAsync', SX(async function() { + var result = await page.evaluateAsync(() => Promise.resolve(8 * 7)); + expect(result).toBe(56); + })); +}); + +// Since Jasmine doesn't like async functions, they should be wrapped +// in a SX function. +function SX(fun) { + return done => Promise.resolve(fun()).then(done).catch(done.fail); +}