<p>I have a Testcafe script (<code>script.js</code>). I want to run this on a Chrome browser, but in the headless mode. So, I use the following command.</p>
<pre><code>testcafe chrome:headless script.js
</code></pre>
<p>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?</p>
<p>I see the <a href="https://hub.docker.com/r/testcafe/testcafe/" rel="nofollow noreferrer">Testcafe Docker image</a>, 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.</p>
<p>(<a href="https://stackoverflow.com/questions/59776886/is-there-a-way-to-run-non-headless-browser-in-testcafe-docker">This question</a> is different from what I am asking)</p>

Keet Sugathadasa
<p>As you can see in the <a href="https://github.com/DevExpress/testcafe/blob/master/docker/Dockerfile" rel="nofollow noreferrer">Dockerfile</a>, the <code>testcafe/testcafe</code> Docker image is based on the Alpine Linux image. It doesn't contain the <code>Chrome</code> browser, but you can run your tests using the <code>Chromium</code> browser.
More information can be found in this <a href="https://devexpress.github.io/testcafe/documentation/guides/advanced-guides/use-testcafe-docker-image.html" rel="nofollow noreferrer">documentation</a></p>
<blockquote>
<p>TestCafe provides a preconfigured Docker image with Chromium and Firefox installed.</p>
</blockquote>
<hr />
<h3>Docker</h3>
<p>I've created simple example for you to illustrate how it works.
On my local machine I have <code>tests</code> directory that contains one simple test script <code>script.js</code>:<br></p>
<pre><code>root@server1:~# cat /tests/script.js 
import { Selector } from 'testcafe';

fixture `First test`
    .page `http://google.com`;

test('Test 1', async t =&gt; {
        // Test code
});
</code></pre>
<p>I'm able to run this test script in container using below command:<br></p>
<pre><code>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)
</code></pre>
<h3>Kubernetes</h3>
<p>In addition you may want to run some tests in Kubernetes using for example <code>Jobs</code>.</p>
<p>I created <code>Dockerfile</code> based on <code>testcafe/testcafe</code> image that copies my test script to appropriate location and then built an image from this <code>Dockerfile</code>:<br></p>
<pre><code>FROM testcafe/testcafe
...
COPY tests/script.js /tests/script.js
...
</code></pre>
<p>Finally, I created <code>Job</code> using above image (it can be <code>CronJob</code> as well):</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: simple-test
spec:
  backoffLimit: 3
  template:
    spec:
      containers:
      - image: &lt;IMAGE_NAME&gt;
        name: simple-test
        args: [ &quot;chromium:headless&quot;, &quot;/tests/script.js&quot; ]
      restartPolicy: Never
</code></pre>
<p>As we can see <code>Job</code> successfully completed:<br></p>
<pre><code>$ 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
</code></pre>
