4

I am wondering how I can run a protractor test as a script and not as a child process or from a task runner such as grunt and gulp. I am wanting to run the test suits in order when my sauce queuing application notifies the test runner I am building. This way, my tests do not conflict with my co-workers tests.

I am using node, so is there something like this?

var protractor = require('protractor');

protractor.run('path/to/conf', suites, callback);
protractor.on('message', callback)
protractor.on('error', callback)
protractor.end(callback);
jemiloii
  • 21,771
  • 7
  • 47
  • 76

3 Answers3

4

It will not be possible. I tried to do that but by reading the protractor source code there is no way to perform this.

https://github.com/angular/protractor/blob/master/lib/launcher.js#L107

This function is called with your config as a json object, but as you can see it calls a bunch of process.exit, according to this it will not be possible to run this without at least forking your process.

My solution for programmatically call protractor is the following:

var npm = require('npm');
var childProcess = require('child_process');
var address = ...some address object
var args = ['--baseUrl', url.format(address)];

npm.load({}, function() {
  var child = childProcess
  .fork(path.join(npm.root, 'protractor/lib/cli'), args)
  .on('close', function(errorCode) {
    console.log('error code: ', errorCode);
  });
  process.on('SIGINT', child.kill);
});
IxDay
  • 3,487
  • 2
  • 19
  • 26
3
const Launcher = require("protractor/built/launcher");
Launcher.init('path/to/conf');
Guilhermevrs
  • 1,180
  • 7
  • 12
  • This works! But in my case I need kind of a callback or thenable to know when the tests are completed and their status (passed, pending, failed). – manuman94 Jul 10 '20 at 07:04
0
const protractorFlake = require('protractor-flake'),
    baseUrl = process.argv[2],
    maxAttempts = process.argv[3];

if (process.argv.length > 2) {
    console.info('Launching protractor with baseUrl: %s, maxAttempts: %d', baseUrl, maxAttempts);

    protractorFlake({
        maxAttempts: maxAttempts,
        parser: 'multi',
        protractorArgs: [
        './protractor.conf.js',
        '--baseUrl',
        baseUrl
    ]
    }, function (status, output) {
        process.exit(status);
});
} else {
    console.error(`
        Usage: protractor-wrapper <baseUrl>
    `);
}
James McGuigan
  • 107
  • 1
  • 6