Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multi page flow #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Module dependencies.
*/

//require('debug').enable('totoro*');
require('debug').enable('totoro*');
var debug = require('debug')('totoro-phantomjs-driver');
var TotoroDriver = require('totoro-driver-base');
var phantomjs = require('phantomjs');
Expand Down Expand Up @@ -55,8 +55,8 @@ proto.onAdd = function (data) {
path.join(__dirname, 'phantom-openurl.js'),
data.url,
this.includeScripts,
this.script,
this.ignoreLog
this.ignoreLog,
JSON.stringify(this.subTasks)
];
var child = childProcess.execFile(phantomjs.path, args);
child.stdout.on('data', function (out) {
Expand Down
80 changes: 70 additions & 10 deletions lib/phantom-openurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,119 @@ var async = require('async');

var url = system.args[1];
var includeScripts = system.args[2];
var script = system.args[3];
var ignoreLog = system.args[4];
var ignoreLog = system.args[3];
var subTasks = system.args[4];

if (subTasks) {
try {
subTasks = JSON.parse(subTasks);
} catch(e) {
subTasks = null;
}
}

if (includeScripts) {
includeScripts = includeScripts.split(',');
} else {
includeScripts = [];
}

var isInserted = false;
log('opening ' + url);
page.open(url, function (status) {
log('opened ' + url + ', status ' + status);
isInserted = false;
});

var script;
// To avoid triggering this method repeatedly.
page.onLoadFinished = function(status) {
if (status !== 'success') {
log('opened url error');
exit(1);
return;
}

if (!isInserted) {
if (script) {
insertScripts(page, includeScripts, function() {
executeScript(script);
script = null;
});
}
};

isInserted = true;
page.onUrlChanged = function(a, b) {
script = findMappingScript(page.url);
};

//include scripts;
function insertScripts(page, scripts, cb) {
async.eachSeries(scripts, function(script, callback) {
page.includeJs(script, function() {
log('loaded ' + script);
if (script.indexOf('http') < 0) {
page.injectJs(script);
callback();
});
} else {
page.includeJs(script, function() {
log('loaded ' + script);
callback();
// 有可能是缓存的原因, 第二次加载 js 的时候, 这个方法会触发两遍.
callback = function(){};
});
}
}, function() {
log('scripts loaded!');
cb();
});
}

page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];

if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}

console.error(msgStack.join('\n'));
};

function executeScript(script) {
if (script) {
page.evaluateJavaScript(eval(script));
}
}

page.onConsoleMessage = function(msg, lineNum, sourceId) {
log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

function findMappingScript(url) {
// 如果支持多页面, 需要提前传入 runner 和 测试脚本的对应关系, 也就是 subTasks
var script;
if (subTasks) {
Object.keys(subTasks).some(function(subUrl) {
if (isMatch(url, subUrl)) {
script = subTasks[subUrl];
delete subTasks[subUrl];
}
});
}
return script;
};

// 检查 url 是否匹配
function isMatch(url, subUrl) {
if (url.indexOf(subUrl) === 0) {
return true;
}

if (subUrl.indexOf('/') === 0) {
return url.indexOf(subUrl) > -1;
}
return false;
}

function log(str) {
return;
if (ignoreLog) {
return;
}
Expand Down