Mathias Maciossek
Mathias Maciossek

Reputation: 245

Puppeteer Travis-CI chrome-headless not working

I'm trying to automate tests for my students. As a very basic example: write some HTML. So I created a test case to check for an unordered list. Anyway: It works locally, but it seems I can't get it to work on travis. I might miss a tiny thing, but no idea what's wrong:

https://travis-ci.com/maciossek/hft-asgmt-html-01/jobs/127338669/config https://github.com/maciossek/hft-asgmt-html-01

Any help highly appreciated!

Upvotes: 2

Views: 1054

Answers (2)

Benny Code
Benny Code

Reputation: 54832

Travis CI updated the sudo-enabled Ubuntu build environments (dist: trusty), so there is no need anymore to install google-chrome-stable.

Here is a complete running example:

.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');
  });
});

Upvotes: 2

Mathias Maciossek
Mathias Maciossek

Reputation: 245

This is the travis.yml I ended up with (working)

language: node_js
node_js:
  - "9"
dist: trusty
sudo: false  
addons:
  chrome: stable
before_install:
  - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
cache:
  yarn: true
  directories:
    - node_modules
install:
  - yarn install
script:
  - yarn test

Upvotes: 3

Related Questions