Run protractor on live-server via grunt - node.js

I am trying to run my portractor tests on live-server via one grunt task.
I have installed live-server (https://www.npmjs.com/package/live-server) and grunt-execute. With grunt execute I managed to start live-server with a grunt command in 2 steps:
1st I created a node script (liveServer.js)
var liveServer = require("live-server");
var params = {
port: 8080,
host: "localhost",
open: true,
wait: 1000
};
liveServer.start(params);
2nd I created a task in my grunt file to start the script:
(in grunt.initConfig)
execute: {
liveserver: {
src: ['liveServer.js']
},
}
and registered a command to trigger it:
grunt.registerTask('live', [
'execute:liveserver'
]);
Now if I run "grunt live" in my commandline live-server starts, opens a browser, and I can browse my application.
I also created a protractor task in my grunt file, which works just fine as well.
(in grunt.initConfig)
protractor: {
options: {
keepAlive: false,
noColor: false
},
e2e: {
options: {
configFile: 'protractor.conf.js',
}
}
},
If I trigger it with a registered task the protractor tests run just fine, only I have to make sure live-server is running first.
So ofcourse I want to combine the two in one command that starts live-server and then runs my protractor tests.
So I tried:
grunt.registerTask('runProtractor', [
'execute:liveserver',
'protractor'
]);
But unfortunately this does not work, live-server starts and then ... nothing happens, the protractor tests aren't run. I tried changing some of the live-server parameters such as open and wait, but without any luck. There are no error messages either.
As I said before separately the tasks work both fine (with two command windows, first start live-server in one and then protractor in the other)
Does anybody have a clue why my does not continue after the live-server has started?

The execution of live-server blocks all subsequent tasks, since it doesn't "finish", i.e. to grunt the task is still running, which is why it won't proceed to the next task. You can use grunt-concurrent to run tasks in parallel.

Related

Jenkins: Replace running Express-app with the most current Express-app

I have created a Jenkins-file, which first pulls the sources of a Express-app from the GitHub-Repository, then installs the dependencies, then starts the Express-App.
pipeline {
agent any
tools {
nodejs 'NodeJS'
}
stages {
stage('Build') {
steps {
sh 'npm install'
echo "install dependencies."
}
}
stage('Deploying') {
steps {
sh 'node index.js'
echo "run express-app ..."
}
}
}
}
Now I have configured "Scan Repository Triggers" to 15 minutes. So, that Jenkins runs the Jenkins-file every 15 minutes, in case there have been changes in the GitHub-repository.
The problem is, that the previous app is still running and occupying the port, which is defined in the sources.
How can I stop the older, running app and replace it with the updated app? The target is, that the respective most current version of the app is supplied, if one enters the URL.
There are multiple ways to get this done. One way is to use something like nodemon. Another clean way to manage our node server is by using something like forever. Then you can Gracefully manage the server.
forever start app.js
forever restart app.js
If you don't want to rely on additional tools. You can kill the Node server before starting it again. There are multiple ways to do this. One option is to get the process ID by the port and then kill the server. You can refer to this question.

How can I run some code in Node prior to running a browser test with Intern?

With Intern, how can I run some setup code in Node prior to running browser tests, but not when running Node tests? I know that I could do that outside of Intern completely, but is there anything that's a part of Intern that could handle that?
For a more concrete example: I'm running tests for an HTTP library that communicates with a Python server. When running in Node, I can run spawn("python", ["app.py"]) to start the server. However, in the browser, I would need to run that command before the browser begins running the tests.
Phrased another way: is there a built-in way with Intern to run some code in the Node process prior to launching the browser tests?
By default, Intern will run the plugins configured for node regardless of which environment you're running in.
So, you could create a plugin that hooks into the runStart and runEnd events like this:
intern.on("runStart", () => {
console.log("Starting...");
// Setup code here
});
intern.on("runEnd", () => {
console.log("Ending...");
// Teardown code here
});
These handlers will run inside the Node process, and thus have access to all the available Node APIs.
Additionally, you can detect which environments are being tested by looking at intern.config.environments:
{
environments: [
{
browserName: 'chrome',
browserVersion: undefined,
version: undefined
}
]
}
By looking at the environments, you can determine whether or not you need to run your setup code.

How to execute Node .JS APIs test cases using Jenkinsfile

I am new to Jenkins. I have a small Node .JS server and the test cases are written using Mocha(Integration test cases, not unit test cases). I am trying to create a CI Pipeline for this using Jenkins. My Jenkinsfile looks as follows:
#!/usr/bin/env groovy
pipeline {
agent {
docker {
image 'node'
args '-u root'
}
}
stages {
stage('Build') {
steps {
echo 'Installing Dependencies...'
sh 'npm install'
}
}
stage('Run') {
steps {
echo 'Starting application...'
sh 'npm start'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'npm test'
}
}
}
}
In the run stage, the server is started using the command node server.js, Once the server is up I want the test cases to be executed against this server. But I notice that, Jenkins never executes the Test stage since the server remains started(this is what i want), and does not exit from it.
How can I have the server started and also have the test stage run against this server?
You should run the tests before running the server. The test should not depend on the running server. Tests should require whatever is required and test, then you should run the server.
https://github.com/jenkinsci/pipeline-examples/tree/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify
I have resolved this by creating separated build jobs and then linking them together. In the run stage, I change the directory to the build folder using "cd" command and start the server. In the test stage, I do the same, but, execute the test cases on the server started in run stage.
Thank you everyone for your inputs.

Given the following file structure, how can you run a basic protractor test?

I have been trying to follow a tutorial given HERE. However, when I try to start the protractor test given, no tests seem to run at all. My webdriver-manager seems to run perfectly however. Basically nothing happens.
I have tried the following:
node protractor conf.js
node node_modules/protractor conf.js
node node_modules/protractor node_modules/protractor/conf.js
node node_modules/protractor node_modules/protractor/tests/conf.js
None of these work, and the first one throws an error. I've tried putting copies of the files in multiple directories, but none of those seem to work either. I'm no exactly sure what the issue is, but this is how much files are setup.
ui_directory/ <-- This is the overall directory for my web projects
ui_directory/conf.js
ui_directory/todo-spec.js
ui_directory/node_modules/
ui_directory/node_modules/protractor/
ui_directory/node_modules/protractor/conf.js
ui_directory/node_modules/protractor/todo-spec.js
ui_directory/node_modules.protractor/tests/
ui_directory/node_modules.protractor/tests/conf.js
ui_directory/node_modules.protractor/tests/todo-spec.js
What exactly is the proper command to run the tests from the tutorial? All todo-spec.js and conf.js files are the same.
My conf.js file contains the following:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['todo-spec.js']
};
Please follow the below steps to run basic protractor Test.
Create a Folder, Say example in Desktop (Folder name is protractor)
Create a config and Spec file with .js extension
Make sure both the files are in Same Location / Folder
Open Command Prompt, And Navigate to project Folder (protractor)
Once navigated to project folder, Type as in bracket:
[protractor config.js] // This will execute protractor Test
Example of Basic Protractor Config File is given as
exports.config = {
//The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
//Here we specify the name of the specs files.
framework: 'jasmine',
specs: ['Protractor_spec.js'],
jasmineNodeOpts: {
showColors: true,
includeStackTrace: true,
defaultTimeoutInterval: 1440000
},
}

How can I run grunt as a daemon?

I am running a packaged nodejs webserver that allows for reading of epub files (Readium-JS), and it is started with the grunt command.
However, if I run this on my VPS the server dies as soon as my terminal connection ends.
How can I run this task as a daemon?
I have looked at options like grunt-forever and grunt-daemon but the way the Gruntfile is written using load-grunt-config is messing with my mind and I can't piece together how to isolate the server code.
Here's the solution I found:
As was suggested above, using pm2
However, when I ran
pm2 start grunt
I got an error saying that the grunt module did not exist, which was weird.
So I ended up writing a script which worked:
-- start.js --
var pm2 = require('pm2');
pm2.connect(function() {
pm2.start({
script : '/usr/local/bin/grunt', // Script to be run
args: '--force',
}, function(err, apps) {
pm2.disconnect();
});
});
After running node start.js from the command line, everything sailed smoothly.

Resources