Travis CI обновил среды сборки Ubuntu с поддержкой sudo (dist: trusty
), поэтому установка google-chrome-stable
больше не требуется.
Вот полный рабочий пример:
.travis.yml
dist: trusty
sudo: required
language: node_js
node_js:
- "8.11.3"
script:
- yarn test
package.json
{
"dependencies": {
"express": "4.16.3"
},
"devDependencies": {
"jasmine": "3.2.0",
"puppeteer": "1.9.0"
},
"main": "src/Server.js",
"name": "example-puppeteer-travis-ci",
"scripts": {
"test": "jasmine"
},
"version": "1.0.0"
}
src / Server.js
const express = require('express');
class Server {
constructor() {
this.app = express();
this.app.get('/', (request, response) => response.send('<title>Hello</title>'));
}
start(port = 8080) {
return new Promise((resolve, reject) => {
if (this.server) {
reject(new Error('Server is already running.'));
} else {
this.server = this.app.listen(port, () => resolve(port));
}
});
}
stop() {
if (this.server) {
this.server.close();
this.server = undefined;
}
}
}
module.exports = Server;
spec / support / ServerSpec.js
const puppeteer = require('puppeteer');
const Server = require('../../src/Server');
describe('Server', () => {
let browser = undefined;
let server = undefined;
beforeEach(async () => {
browser = await puppeteer.launch({args: ['--disable-setuid-sandbox', '--no-sandbox'], dumpio: true});
server = new Server();
});
afterEach(async () => {
if (browser) await browser.close();
if (server) await server.stop();
});
it('serves a homepage with a title', async () => {
const port = await server.start();
const url = `http://localhost:${port}/`;
const page = await browser.newPage();
await page.goto(url);
const title = await page.title();
expect(title).toBe('Hello');
});
});