How to run npm run command in onComplete hook in wdio - javascript

In wdio test project, I want to generate and open allure report once test execution is completed and to do this I've added a command ("allure-report": "allure generate ui-tests/allure-results --clean && allure open") as a script in package.json file. To open the allure report post-execution, I need to run this command "npm run allure-report" manually, which successfully opens the report in the web browser. But instead of running this command manually every time post-execution, I want to add this command to be executed itself by putting it in the onComplete hook in the wdio.config.js file.
To do this I have added the following in onComplete hook which is not working:
onComplete: function(exitCode) { require('child_process').spawnSync('npm', ['run', 'allure-report'], {cwd: __dirname + '/package.json' }) }
Does anyone know how to make this command run itself in onComplete hook post test execution which open the allure report

Related

Entering a kubeCTL pod from a node script

I have the following code:
exec('kubectl exec -it -n production my-kubectl-pod-id bash',
function (e, stdo, stde) {
console.log(stdo)
})
However it doesnt work. When it runs, the program just stalls and I can't see the command line prompt at all, but i can type new lines but it is not running any commands i type.
I'm expecting to enter a pod and be able to work inside the pod however i need.
Is there a way to do this from inside a node script? Or a way for the script to end and then run that command?

Why does Cypress test in GUI(cypress open) pass but fails in command line(cypress run) even though I using chrome browser?

I'm writing a test for 4 endpoints and I can see it's successfully completed when trying with cypress open command. However, If I try to run cypress run command, I see a timeout issue. Is there any difference between these 2 commands ?
Try running below commands:
cypress run -C <path to your config file>
and
cypress open -C <path to your config file>
In the config mention the browser else it will run in electron browser
See in your report if the test are run in same browser else pass --browser explicitly for both the commands

Run pre and post commands to npm test

I wanted to run pre and post commands while executing npm test.
As a pre command, i wanted to run my .exe which will need for testing and after the testing is completed and i wanted to kill the .exe.
I have add below code in my package.json tried to execute npm test command, it is executing batch file and my server.exe is running but my tests are paused.
Batch file contains the command start C:\server.exe
'test' : 'C:/test.bat && ng test'

How do I open a new terminal and run a command with Gulp.js

I'm using gulp.js to run a few task
What I need gulp to do is to run some command in a new terminal
e.g:
cd <directory_name>
yarn start
or
run other command like yarn install or build
That above has to be done in a new terminal.
When I run the first task it will be to open a new terminal and run the serve that is inside a child directory. The second to run the frontEnd project so it will need to open another new terminal and run a command.
I check out this two question on stackOverflow and still not getting what I need.
Gulp – How Can I Open A New Tab In Terminal?
Running a command with gulp to start Node.js server
I would recommend you to check ShellJs and gulp-shell if you are trying to run Unix shell commands within Gulp. If you are specifically looking to open a new terminal in your GUI, you could attempt to run one the following commands (depending on your Operating System):
gnome-terminal -e command
or
xterm -e command
or
konsole -e command
pretty much:
terminal -e command
Source: https://askubuntu.com/questions/46627/
... with either ShellJs or gulp-shell from a gulp task.
Have you tried this solution?
var exec = require('child_process').exec;
gulp.task('node exec', function (cb) {
exec('cd some/where && yarn start', (err, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
cb(err);
});
});
It Spawns a shell then executes the command within that shell according to the node.js doc, exactly doing what you want: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Node's spawn() silently failing when called from a forever script scheduled on boot

This is kind of a doozy. This issue is most likely server related and so my first recourse was AskUbuntu over here.
I'm trying to have crontab or rc.local or init.d to start a forever script on boot. It attaches a server to a port I can ping with some information and have it run a headless browser for me.
That said, it seems that I'm unable to get a response from Node.js's spawn():
var CASPER_PATH = '/home/ubuntu/dev/casperjs/bin/casperjs'; // actual binary location, not a symlink
var SCRIPTS_PATH = '/home/custom_user/endpoints/server.js';
var fileName = req.body.source + '_' + req.body.type + '.coffee'; // looks like: mysource_my_scrape_type.coffee
var scrapeId = 'test_scrape';
var user = 'user123';
var pass = 'pass123';
if (fs.existsSync(SCRIPTS_PATH + fileName)) {
// If file is in place, spawn casperjs
var sP = spawn(CASPER_PATH,
[SCRIPTS_PATH + fileName, '--ssl-protocol=any', '--user='+user, '--scrapeId='+scrapeId, '--pass='+pass],
{ detached: true },
function (err, stdout, stderr) {});
sP.stdout.on('data', function(data) { console.log('stdout', data.toString('utf8')); });
sP.stderr.on('data', function(data) { console.log('stderr', data.toString('utf8')); });
sP.stdout.on('close', function(code) { console.log('close', code); });
res.send({ scheduled: true, key: scrapeId });
} else {
res.send({ scheduled: false, error: 'Incorrect source, type or the script is missing.' });
}
Before I added the PHANTOMJS_EXECUTABLE env to crontab or rc.local (doesnt seem to matter no matter the user level), stdout was useful:
stdout Fatal: [Errno 2] No such file or directory; did you install
phantomjs?
close false
Now that the environment var is there, there is no output at all after spawn().
Mind you, Casper starts up just fine if a user (of any privilege level) runs node/forever from bash.
How can I see why spawn() is failing?
This actually looks like a combo-bug between forever, spawn and casperjs (maybe phantomjs).
I was able to reproduce your problem, here is the full code of my test application.
You didn't show the full code, so my guess is that you have an express application and there is a special URL to run the casperjs script.
I build a simple app like this and it behaved this way:
Just start app with node script.js (script.js is the express app which runs the casperjs script in server.js) - it works OK, renders response and writes output from the child process event handlers to console
Start app as root with init.d script - doesn't work, once the child is spawned, no event handlers are triggered
Start app as root with init.d script, replace casperjs with echo - the same, doesn't work (see, here we have this problem with just forever running as root, spawn and echo)
Start app as a regular user (not root) with init.d, replace casperjs with 'echo' - it works, event handlers are triggered, here I was almost sure the issue is solved, but ... :(
Start app as a regular user (not root) with init.d, put back casperjs - it doesn't work again, event handlers are not triggered
The practical solution to this it to use pm2, I did this:
# install pm2
sudo npm install -g pm2
# generate init.d scripts for pm2
# this command will fail, but hint about the correct format with sudo
pm2 startup ubuntu
# do this in the folder with your application
pm2 start script.js
# remember your application
pm2 save
# also useful
# sudo service stop/start/restart pm2
# pm2 stop/start/restart script
Now pm2 will start automatically with the system and it will launch your application. Everything works, child process event handlers are triggered.
I did not understand your requirement completely. But i do have a similar situation with Ubuntu headless server.
what i am trying to do here is what i did
First, How is my crontab ?
crontab -u USER -e
#reboot exec sudo -u USER /bin/bash /home/USER/SHELL_SCRIPT.sh
See, here i am actually starting a shell script, and not a node server
Now inside this shell script(SHELL_SCRIPT.sh)
#! /bin/bash
# SHELL_SCRIPT.sh
cd /home/USER/
/home/USER/.npm-packages/bin/forever start -p /home/USER -a -d --watch false --pidFile /home/USER/forever.pid -l /home/USER/forever.log -o /home/USER/forever.out -e /home/USER/forever.err /home/USER/MY_NODE.js
and even inside my MY_NODE.js i follow absolute path, i just ignore $PATH, and don't use that.
Inside this node server, I do 100's of spawn
Now, i did this around 2 years back, so if you ask me why do this way, which i cannot answer

Categories