Run a Testcafe Script on Chrome Headless in a Docker Container Run a Testcafe Script on Chrome Headless in a Docker Container kubernetes kubernetes

Run a Testcafe Script on Chrome Headless in a Docker Container


As you can see in the Dockerfile, the testcafe/testcafe Docker image is based on the Alpine Linux image. It doesn't contain the Chrome browser, but you can run your tests using the Chromium browser.More information can be found in this documentation

TestCafe provides a preconfigured Docker image with Chromium and Firefox installed.


Docker

I've created simple example for you to illustrate how it works.On my local machine I have tests directory that contains one simple test script script.js:

root@server1:~# cat /tests/script.js import { Selector } from 'testcafe';fixture `First test`    .page `http://google.com`;test('Test 1', async t => {        // Test code});

I'm able to run this test script in container using below command:

root@server1:~# docker run -v /tests:/tests -it testcafe/testcafe chromium:headless /tests/script.js Running tests in: - Chrome 86.0.4240.111 / Linux 0.0 First test  Test 1 1 passed (1s)

Kubernetes

In addition you may want to run some tests in Kubernetes using for example Jobs.

I created Dockerfile based on testcafe/testcafe image that copies my test script to appropriate location and then built an image from this Dockerfile:

FROM testcafe/testcafe...COPY tests/script.js /tests/script.js...

Finally, I created Job using above image (it can be CronJob as well):

apiVersion: batch/v1kind: Jobmetadata:  name: simple-testspec:  backoffLimit: 3  template:    spec:      containers:      - image: <IMAGE_NAME>        name: simple-test        args: [ "chromium:headless", "/tests/script.js" ]      restartPolicy: Never

As we can see Job successfully completed:

$ kubectl get job,podNAME                    COMPLETIONS   DURATION   AGEjob.batch/simple-test   1/1           18s        14mNAME                    READY   STATUS      RESTARTS   AGEpod/simple-test-w72g2   0/1     Completed   0          14m