On a previous post I wrote about how to write tests for polymer components. Now, I want to hook those tests into my automated test suite that runs for all commits in a repo. The problem is that we are kind of in a low budget so we don’t have a selenium grid we can connect to. What we do have is a machine where we have Jenkins installed. Because we run many different jobs in this machine, we usually use docker to keep our environment isolated.

The problem now is that we can’t run polymer tests in a headless browser like phantomjs, because it is not supported. We have to run our tests in a real browser like Chrome or Firefox. These browsers need a GUI to work which docker doesn’t provide, so we have to do a few things to work around this issue.

xvfb

Xvfb stands for X virtual framebuffer. It is a display server that implements the X11 protocol, but does everything in memory, so it doesn’t really need a screen to work. This is exactly what we need. To use it we just need to create a Dockerfile that uses xvfb to run the tests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
FROM ubuntu

# General sanity
RUN apt-get update;

# Install chrome to run the tests
RUN apt-get install -y curl
RUN curl -sL https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google.list
RUN apt-get update
RUN apt-get install -y google-chrome-stable

# Install xvfb so we can run tests headless
RUN apt-get install -y xvfb;

# Install git, needed by bower
RUN apt-get install git -y

# Install java, needed by selenium
RUN apt-get install default-jre -y

# Some cleanup
RUN apt-get clean

# Install Node
WORKDIR /usr/src
ENV NODE_VERSION 5.0.0
RUN wget https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
RUN tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1
RUN rm "node-v$NODE_VERSION-linux-x64.tar.gz"

# Copy project files
COPY ncona-accordion.html /usr/src
COPY wct.conf.json /usr/src
COPY package.json /usr/src
COPY bower.json /usr/src
COPY test/ test/

# Install dependencies
RUN npm install
RUN ./node_modules/bower/bin/bower install --config.interactive=false --allow-root

# Run tests using xvfb
RUN xvfb-run  ./node_modules/web-component-tester/bin/wct

As easy as that, you can have now run polymer tests on a real chrome browser under docker.

[ docker  javascript  polymer  programming  testing  ]
Sorting algorithms javascript programming
Writing tests for C++ code programming testing
Introduction to Beego docker programming
Dependency management with golang/dep docker programming
The Rabin-Karp algorithm javascript programming