Environment prerequisites
Make sure to install NodeJS before you continue.
Setting up the project in Visual Studio
Go into the web application directory:
cd .\MyWebApp
Install PhantomJS into the web application project:
npm install phantomjs-prebuilt --save-dev -g
Install casperjs (wrapper on top of PhantomJS to help with navigation, selectors and testing):
npm install casperjs --save-dev
Create tests.js
with this content:
function Cow() {
this.mowed = false;
this.moo = function moo() {
this.mowed = true; // mootable state: don't do that at home
return 'moo!';
};
}
casper.test.begin('Cow can moo', 2, function suite(test) {
var cow = new Cow();
test.assertEquals(cow.moo(), 'moo!');
test.assert(cow.mowed);
test.done();
});
Create run-tests.ps1
(it will run our tests). Paste this content into it:
$env:PHANTOMJS_EXECUTABLE=".\\node_modules\\phantomjs-prebuilt\\lib\\phantom\\bin\\phantomjs.exe"
node_modules/casperjs/bin/casperjs test tests.js
Run the file (from powershell.exe, not from Package Manager Console):
.\run-tests.ps1
To integrate with your gulp script, add this to your gulpfile.js
:
var gulp = require("gulp");
var exec = require('child_process').exec;
gulp.task("integration-tests", function (callback) {
exec("powershell.exe -executionpolicy remotesigned . .\\run-tests.ps1", function (err, stdout) {
console.log(stdout);
callback(err);
});
});
When running the gulp task you should see this output:
Test file: tests.js
# Cow can moo
PASS Subject equals the expected value
PASS Subject is strictly true
PASS Cow can moo (2 tests)
PASS 2 tests executed in 0.026s, 2 passed, 0 failed, 0 dubious, 0 skipped.
Process terminated with code 0.
To get started with writing tests, read casper docs.
If you prefer bat
files instead of ps1
, then create this guy:
set PHANTOMJS_EXECUTABLE="%~dp0..\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe"
%~dp0..\node_modules\casperjs\bin\casperjs test %~dp0tests.js --xunit=report.xml
To run the tests, just run the file by double-clicking on it in file explorer.