Installing TestCafe Module on Linux Apache Server

When TestCafe module is installed on your Linux server, you could take advantage of automatic test executions using a Continuous Integration system. By now you already have created and executed your tests and are now ready to be used by your quality assurance team. Next step is to copy it to your server and setup TestCafe module which only requires a single line to install. Navigate to the directory where you want to setup TestCafe module and execute this command npm install testcafe to start installation. You can add the version of which you want to use by adding @<version> and -g to install it globally.

  • npm install testcafe@14.1.1 – g

When you install TestCafe program on your local personal computer, you will be able to use the TestCafe Settings. You can skip the UI and indicate your settings programmatically in a script to run tests and to log the results. Note that the TestCafe module is not dependent on the TestCafe’s program version which has the Control Panel user interface.

Writing a Script to Run Tests and Log the Results

Create a new JS file in the directory where you installed TestCafe module or any directory if it was installed globally. In this example we will name the file as testcafe_getting_started.js. The application consists of 3 parts: Constructor, Methods and Events.

Constructors

  • controlPanelPort – port number to access TestCafe Control Panel UI (range: 0-65535)
  • servicePort – port number to access TestCafe to perform test (range: 0-65535)
  • hostname –server IP address (default: localhost|127.0.0.1, optional)
  • testDir – test scripts directory
  • browsers – list of browsers TestCafe available for testing use
    • browserKey – browser name that will be used in TestCafe UI
    • path – path of the browser’s executable file
    • cmd – command line parameters (optional)

Methods

  • listConnectedWorkers – list of connected remote workers
  • listAvailableBrowsers – list of available browsers in the server
  • runTests – runs the tests
  • listDirectory – gets information about fixtures in a path (optional)

Events

  • taskComplete– test that was ran has been completed
  • taskUpdated– a test was run
  • workerAdded– a worker was added
  • workerDisconnected– a worker has been disconnected

In our new js file, we’ll place these basic codes for our simple application to run tests.

  • var TestCafe = require('testcafe').TestCafe;
  • var options = {
    • controlPanelPort:1337,
    • servicePort:1338,
    • hostname:‘19*.***.***.***’,
    • testsDir:'./',
    • browsers:{
      • 'Safari':{
        • path: “./Applications/Safari.app”,
        • cmd:'-new-window'
      • },
    • }
  • }
  •  
  • var testCafe = new TestCafe(options);
  • var runOptions = {
    • workers: testCafe.listConnectedWorkers(),
    • browsers: testCafe.listAvailableBrowsers(),
    • emulateCursor: true
  • };
  •  
  • testCafe.runTests(runOptions, function () {
    • testCafe.on('taskComplete', function (report) {
      • console.log(report);
    • });
  • });

Note that TestCafe team found out that it is impossible to use var TestCafe = require(‘testcafe’);. It needs to have .TestCafe after it as you can see above in our code. Also note that using IP is safer because you might not have the necessary rights to access the TestCafe server using the domain name. Now, what our code above can do is to run the test ad will log the results into the console using the line console.log(report). “report” is an object output generated by TestCafe module detailing the results of the tests when it was run. To save the results in a file, let’s say a TXT file, we will insert additional codes inside the taskComplete event handler.

  • var fs = require('fs');
  • testCafe.on('taskComplete', function (report) {
    • var msg = '\n' + new Date().toString() + ':\n' + JSON.stringify(report);
    • fs.appendFile('log.txt', msg, function (err) {
      • if (err) { throw err; }
    • });
  • });

For more information, refer to the Comprehensive Reference on Continuous Integration API

Initiating Your Code

Running our code is done using shell commands. We’ll be using Node.js executable with our script’s filename as the parameter. Like installing the TestCafe module, we will only need to use one line to run our simple application. Note that if you do not install the module globally, you will have to copy its executable to the path of our new simple application file.

  • node testcafe_getting_started.js

Connecting Remote Workers

To use remote computers and mobile devices to the ran TestCafe fixtures, use the following url format.

  • http://<hostname>:<controlPanelPort>/worker/add/<workerName>

The <workername> is a string that you would like to use to distinguish that specific unit from the list of available workers. Use the controlPanelPort defined on our JS file we just created and the hostname is the IP of the server.

You could also add an event listener that would automatically run the test when a worker is added to the list. Using the event workerAdded, you can set it to run tests automatically after adding the new worker. So our new code for our simple test application that would automatically run tests when a new worker is added will look like the following:

    • var fs = require('fs');
    • var TestCafe = require('testcafe').TestCafe;
    • var testCafe = new TestCafe ({
      • controlPanelPort : 1337,
      • servicePort : 1338,
      • hostname: '19*.***.***.***',
      • testsDir : './',
    • });
    • testCafe.on('workerAdded', function (worker){
      • var runOptions = {
        • workers: [worker],
        • emulateCursor: true
      • };
      •  
      • testCafe.runTests(runOptions, function () {
        • testCafe.on('taskComplete', function (report) {
          • var msg = '\n'+newDate().toString()+'\n'+ JSON.stringify(report);
          • fs.appendFile('log.txt', msg, function(err){
            • if(err){throw err;}
          • });
        • });
      • });
    • });

Notice that we had omitted the browsers declaration since we are only interested in running tests on remote computers and mobile devices. When your browser has successfully accessed the server, it will automatically run the tests that you have laced side by side with our script above. When all you can see on the page on your browser or device is similar to the image below, it means that your browser has finished running the tests and is waiting for new tasks. The results would be written in the log file we had declared in our script.

Waiting Tasks

Now that your browser is waiting for new tasks you should start making new fixtures and create more tests tasks. Since everything is set now, have fun in creating and running more tests with TestCafe.