Keet Sugathadasa
Keet Sugathadasa

Reputation: 13542

Run a Testcafe Script on Chrome Headless in a Docker Container

I have a Testcafe script (script.js). I want to run this on a Chrome browser, but in the headless mode. So, I use the following command.

testcafe chrome:headless script.js

This works fine. But, now I wish to Dockerize this and run it in a container. The purpose is to get this running in Kubernetes. How can I achieve this?

I see the Testcafe Docker image, but this is just to run a Testcafe instance. It does not cater to my requirement of running this script in a Chrome Headless in a container.

(This question is different from what I am asking)

Upvotes: 1

Views: 3581

Answers (1)

matt_j
matt_j

Reputation: 4614

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/v1
kind: Job
metadata:
  name: simple-test
spec:
  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,pod
NAME                    COMPLETIONS   DURATION   AGE
job.batch/simple-test   1/1           18s        14m

NAME                    READY   STATUS      RESTARTS   AGE
pod/simple-test-w72g2   0/1     Completed   0          14m

Upvotes: 5

Related Questions