0.3.1 • Published 9 years ago
wtester v0.3.1
The simple integration testing tools for website
Features
- auto inject the code to web page and run it code on browser(not nodejs).
- transfter running context between two page.
- load browser by configure.
- multi spec supported
- custom nodejs command supported(having some inner command, see reference)
Install
Install webdriver-manager
npm install -g webdriver-manager
webdriver-manager updatemore is on https://github.com/angular/webdriver-manager
Install wtester
npm install -g wtesterStart
Create test spec(testBing.js)
wtester("bing", "http://www.bing.com", null, function (flow, command) {
flow("^.*\\.bing\\.com/(\\?.*)?$", true, function (env, done) {
document.getElementById("sb_form_q").value = "github centny";
document.getElementById("sb_form_go").click();
done();
});
flow("^.*\\.bing\\.com/search.*$", false, function (env, done) {
var as = document.getElementsByTagName("a");
for (var i = 0; i < as.length; i++) {
if (as[i].href == 'https://github.com/Centny') {
done();
return;
}
}
throw "fail";
});
});Create configure file(wtester-conf.js)
exports.config = {
port: 8880,//proxy port
selenium: 'http://127.0.0.1:4444/wd/hub',
specs: [
'testBing.js',
],
capabilities: {
'browserName': 'chrome',
"loggingPrefs": {
"driver": "INFO",
"browser": "ALL"
},
},
};Run
wtester wtester-conf.jsRun the more exampe
- download code
git clone https://github.com/Centny/WebTester.git- start static server
cd WebTester/test
npm install connect serve-static
./run.js- start webdriver
webdriver-manager start- run test
cd WebTester/test
wtester wtester-conf.jsReference
wtester(name,starter,opts,exec)
namerequired,string the case namestartrequired,string the start urloptsoptional,object the options for tester, deafult nullctxobject, the initial context to run test.
execrequired,function the flow executor, the argument isflow,commandflowfuction, adding test case stepcommandfunction, adding custom command
flow(murl,open,worker,pre)
murlrequired,string the url regex pattern to match page for run the workeropenrequired,bool not used nowworkderrequired,function the test code which running on browser, the argument isenv,doneenvobject, the current env transfter from prefix workderdonefunction, completed current worker, not arguments.
preoptional,function the workder to initial something for the test workder
command(name,worker)
namerequired,string the command name.workderrequired,function the command executor, the argument isenv,args,doneenvobject, the enviroment for running workderenv.browserobject, the webdriver object from selenium-webdriverenv.Byobject, the By tools from selenium-webdriverenv.untilobject, the until tool from selenium-webdriverargsobject, the command arguments from callerdonefunction, completed the current worker and return the data or error, the argument isdata,err
tester
tester object contain some event handler and util function.
tester.initthe initial function before call flowtester.readfilethe util to read file sync.
Custom Command
wtester("case1", "http://localhost:8080/web/page1.html", {
//the intitial test case env.
ctx: {
ws: __dirname,
},
}, function(flow, command) {
command("title", function(env, args, done) {
//the custom command on nodejs
env.browser.getTitle().then(function(title) {
done(title, null);
});
});
flow("^http://localhost:8080/web/page1\\.html(\\?.*)?$", true, function(env, done) {
//the test code on page1.html
env.ctx.testing = "login";
env.exec("title", {}, function(data, err) {//exec custom command
if (err) {
throw err;
}
document.getElementById("login").click();
console.log("testing click login done...");
done();
});
}, function(env, done) {
done();
}).debug({});
});Inner Command
sendkeys
calling webdriver sendkey
byrequired, the selector type in id/xpath/css, see more for webdriver document.selectorrequired, the selector value, like element idfileoptional, the file pathvalueoptional, the value.
wtester("case1", "http://localhost:8080/web/page1.html", {
//the intitial test case env.
ctx: {
ws: __dirname,
},
}, function(flow, command) {
flow("http://localhost:8080/web/page2\\.html(\\?.*)?", false, function(env, done) {
//the test code on page2.html
if (env.ctx.testing != "login") {
throw "fail";
}
document.getElementById("account").value = "abc";
env.exec("sendkeys", {//exec inner command, eg: set file path for input
by: "id",
selector: "file",
file: env.ctx.ws + "/../data/test.txt",
}, function(data, err) {
if (err) {
throw err;
}
var file = document.getElementById("file");
if (!file.value) {
throw "not fild";
}
console.log(file.value);
console.log("testing login done...");
done();
});
}).debug({//debug the test code on page2.
ctx: {
testing: "login",
},
});
});readfile
read file
namerequired, the file name
wtester("cmd", "http://localhost:8080/web/page1.html", {
//the intitial test case env.
ctx: {
ws: __dirname,
},
}, function (flow, command, tester) {
flow("^http://localhost:8080/web/page1\\.html(\\?.*)?$", true, function (env, done) {
//the test code on page1.html
env.exec("readfile", {
name: env.ctx.ws + "/../data/test.txt",
}, function (data, err) {
if (data !== "abc") {
throw "error";
}
done();
});
});
});conf.settings Reference
fullscreenif do fullscreen when spec start or not
Context Reference
the example config.js
exports.config = {
port: 8880,//proxy port
selenium: 'http://127.0.0.1:4444/wd/hub',
specs: [
'e2e/testSpec.js',
'e2e/testSpec2.js',
{
specs: [
'e2e/testCtx01.js',
'e2e/testCtx02.js'
],
settings: {
"context": 1,
},
},
{
specs: [
'e2e/testSpec.js',
'e2e/testSpec2.js',
],
},
"e2e/testCmd.js",
"e2e/testMulti.js",
"e2e/testTester.js",
],
capabilities: {
'browserName': 'chrome',
"loggingPrefs": {
"driver": "INFO",
"browser": "ALL"
},
"chromeOptions": {
"args": ['--start-maximized']
}
},
settings: {
"fullscreen": 1,
},
};conf.specsis not in the same context.e2e/testCtx01.js,e2e/testCtx02.jsis the same context control bycontext:1
Debug Test Case
For debug test case on browser, you cant adding <script type="text/javascript" src="e2e/testSpec.js" /> on your page and adding blow code to simple start flow by matchi url
if (typeof wtester === 'undefined') {
if (typeof module !== 'undefined' && module.exports) {//on nodejs debug
wt = require("wtester");
wtester = wt.wtester;
} else {//on browser debug
wtester = function(name, starter, opts, exec) {
exec(function(murl, open, worker, pre) {
return {
debug: function(env) {
if (!env.ctx) {
env.ctx = {};
}
if (pre) {
pre(env, function() {
if (window.location.href.match(murl)) {
worker(env, function() { });
}
});
} else {
if (window.location.href.match(murl)) {
worker(env, function() { });
}
}
},
};
}, function(env, args, done) {
});
};
}
}