Konrad Garus
Konrad Garus

Reputation: 54035

Running a Java program from Protractor

Let's say I have a Java program for setting up and cleaning up data for end to end tests. It can delete everything from all tables, populate them with some initial data, that kind of stuff.

Is there any way to execute it from within Protractor?

I'm interested in both a way to do it right from the spec (e.g. in beforeEach and afterEach) as well as having Protractor do it between tests.

Upvotes: 1

Views: 1331

Answers (1)

Eitan Peer
Eitan Peer

Reputation: 4345

Since Protractor is a NodeJS application you can use NodeJS API.

I had a similar need and I executed Maven/Java from using Node's child_process module's exec method. The problem was performance, since it needed to start a new JVM instance on each call to exec.

Its not ideal but it does the job...

var deferred = Q.defer();
try {
    var child = process.exec('mvn verify',
        function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.error('exec error: ' + error);
                deferred.reject();
            }
            else {
                deferred.resolve();
            }
        });
} catch (err) {
    console.error('Caught ' + err);
}
return deferred.promise;

You can explore the node-java project.

Upvotes: 1

Related Questions