Executing jar file from protractor - protractor

I am trying to execute command "java -jar mytest.jar" using child_process inside my spec file of protractor. Code is getting executed but nothing is happening.
With successful execution it will create new file. Pls help me to resolve this.
Below is my code:
it('execute Jar', ()=>{
let exec = require('child_process').exec;
const child = exec("java -jar mytest.jar", (error, stdout, stderr)=>{
if (error) {
console.error('exec error: ${error}');
return;
}
console.log('stdout: ${stdout}');
console.log('stderr: ${stderr}');
});
});
Node code which executed and returns as expected
let exec = require('child_process').exec;
let childprocess = exec("java -jar mytest.jar",
function (err, stdout, stderr){
if (err){
console.log(err);
}
console.log(stdout);
});

Related

gulp task with gulp-run-command doesn't work properly

I'm trying to run json-server in a gulp task and I'm checking if the server runs, with the function portInUse.
Like this:
var gulputil = require('gulp-util')
var run = require('gulp-run-command').default
var gulp = require('gulp')
const args = [
'json-server --watch .\\src\\main\\app\\reactjs\\api\\db.json --port 3005'
]
var net = require('net');
var portInUse = function(port, callback) {
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(port, '127.0.0.1');
server.on('error', function (e) {
callback(true);
});
server.on('listening', function (e) {
server.close();
callback(false);
});
};
gulp.task("initLocalJsonServer", function() {
portInUse(3005, function(returnValue) {
gulputil.log('1 ' + returnValue);
});
run(args);
portInUse(3005, function(returnValue) {
gulputil.log('2 ' + returnValue);
});
});
That command that is the value of args here, works as intended when I run it in command line, so there's nothing wrong with the command or json-server itself. If I run it, I get to see the json-server at localhost:3005
Now the output from the portInUse function indicates that the server starts, as the output is:
[10:33:56] 1 false
[10:33:56] 2 true
But if I go to localhost:3005 after the gulp tasks are done, I can't see any server running. What might be the reason?

How to run protractor with different config file in sequence?

I have different configuration file for protractor, and I would like to create a gulp task which run protractor for each config file in sequence.
Here is my actual code:
gulp.src('conf/protractor.conf.*.js')
.pipe($.debug())
.pipe($.foreach(function(stream, file){
var configFileName = path.join('conf/', path.basename(file.path));
console.log(configFileName);
gulp.src(path.join(conf.paths.e2e, '/**/*.js'))
.pipe($.protractor.protractor({
configFile: configFileName,
args: args
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
console.log('Error catch by gulp');
throw err;
})
.on('end', function () {
// Close browser sync server
browserSync.exit();
done();
return stream;
});
}));
it run protractor only with the first configuration file and then stop, even if the different conf file were listed by foreach.
Does anyone has an idea of what I am missing?
Thanks
I found a workaround: instead of trying to use gulp only to do what I want to do, I created a little shell script.
Now, my gulp task take protractor config file path from command line argument like that:
var argv = require('minimist')(process.argv.slice(2));
if (!argv.conf || typeof argv.conf !== 'string' ) throw new Error('protractor configuration file path required');
function runProtractor (done) {
gulp.src(path.join(conf.paths.e2e, '/**/*.js'))
.pipe($.protractor.protractor({
configFile: argv.conf
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
})
.on('end', function () {
// Close browser sync server
browserSync.exit();
done();
});
}
gulp.task('protractor', ['protractor:src']);
gulp.task('protractor:src', ['serve:e2e', 'webdriver-update'], runProtractor);
And I use a shell script to parse my folder and call gulp protractor for each configuration file:
#!/usr/bin/env bash
for filename in conf/protractor.conf.*.js; do
gulp protractor --conf=$filename
done
It works like a charm.

Running gulp exec based on condition

I am working on ionic project integrated with gulp. How can run exec command based on condition?
I have tried following
var isIOSBuild = false;
if(args.iosBuild){
isIOSBuild = true;
console.log("Creating IOS Build...");
}
// Build application
gulp.task('ios_build', function (cb) {
gulp.src('', {read: false})
.pipe(gulpif(false,
exec(IOS_BUILD_COMMAND,
{
cwd : './',
maxBuffer: 1024 * 1024
},
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
})
));
});
Input: gulp -r
I have put up condition for running exec command still the IOS_BUILD_COMMAND is running. Unable to understand why this is happening...
Alternatively, you can call the gulp task with or without arguments and run the task internally on the argument condition.
I use yargs to make my life easier.
var args = require('yargs').argv;
gulp.task('some_task', function(){
if(args.condition){
// run your task
}
})
Call gulp like so: gulp some_task --condition=true or gulp some_task --condition=false
That allows you to maintain the condition outside of gulp

Karma exits with code 1 when it doesnt execute any spec tests

Karma test runs fine but exits with code 1 if 0 of 0 tests are run. Does anyone know how to return exit code 0 and normally exit in this case? Using gulp-karma which fails the task when no specs are run.
There is a configuration option that allows for empty test suites. Just add
failOnEmptyTestSuite: false
to your karma.conf.js and the process will exit with exit code 0.
BR
Chris
In your gulpfile, replace the "throw err" on the error callback in the your gulp test task with "this.emit('end')".
gulp.task('test', function() {
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
throw err;
});
});
so your test task now looks like;
gulp.task('test', function() {
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
this.emit('end');
});
});

Node.js TCP client behaving differently from Netcat/Telnet

I am issuing newline-separated text commands to a custom protocol TCP server. In the example below I issue 2 commands and receive a response written back. It works as expected in telnet and netcat:
$ nc localhost 1234
command1
command2
theresponse
The same workflow is not working when connecting with Node.js:
var net = require('net');
var client = net.connect(1234, 'localhost');
client.on('data', function(data) {
console.log('data:', data.toString());
});
client.on('error', function(err) {
console.log('error:', err.message);
});
client.write('command1\n');
client.write('command2\n');
I would expect that after running this program I would see "data: theresponse" written to the console, however, nothing is ever printed. I have also tried performing the writes inside of the "connect" callback, but I have the same results. The curious thing is that when I try this in the Node REPL...it works:
$ node
> var net = require('net')
undefined
> var client = net.connect(1234, 'localhost')
undefined
> client.on('data', function(data) { console.log('data:', data.toString()); })
{ ... }
> client.write('command1\n')
true
> client.write('command2\n')
true
> data: theresponse
Anyone have ideas about this bizarre behavior?
Thanks.
-Scott
Without testing the code, I'm presuming it's the asynchronous nature of Node.js that's biting you. In the REPL the connection happens before you can type in another command. In your code above you are writing before you are connecting.
Change the above code to this:
var net = require('net');
var client = net.connect(1234, function(){
client.on('data', function(data) {
console.log('data:', data.toString());
});
client.on('error', function(err) {
console.log('error:', err.message);
});
client.write('command1\n');
client.write('command2\n');
});