From b77d03d192380d33995177cced2fbec4d40547b4 Mon Sep 17 00:00:00 2001 From: leoner Date: Sun, 4 Jan 2015 16:48:04 +0800 Subject: [PATCH 1/3] support multi page flow --- lib/driver.js | 5 +++-- lib/phantom-openurl.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/driver.js b/lib/driver.js index 03cb54e..f2b9be8 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -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'); @@ -56,7 +56,8 @@ proto.onAdd = function (data) { 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) { diff --git a/lib/phantom-openurl.js b/lib/phantom-openurl.js index 48e9924..0a7c64f 100644 --- a/lib/phantom-openurl.js +++ b/lib/phantom-openurl.js @@ -23,6 +23,15 @@ var url = system.args[1]; var includeScripts = system.args[2]; var script = system.args[3]; var ignoreLog = system.args[4]; +var subTasks = system.args[5]; + +if (subTasks) { + try { + subTasks = JSON.parse(subTasks); + } catch(e) { + subTasks = null; + } +} if (includeScripts) { includeScripts = includeScripts.split(','); @@ -50,16 +59,26 @@ page.onLoadFinished = function(status) { executeScript(script); }); } +}; - isInserted = true; +page.onUrlChanged = function(a, b) { + if (url === page.url) { + return; + } + + isInserted = false; + script = findMappingScript(page.url); }; //include scripts; function insertScripts(page, scripts, cb) { + isInserted = true; async.eachSeries(scripts, function(script, callback) { page.includeJs(script, function() { log('loaded ' + script); callback(); + // 有可能是缓存的原因, 第二次加载 js 的时候, 这个方法会触发两遍. + callback = function(){}; }); }, function() { log('scripts loaded!'); @@ -73,6 +92,19 @@ function executeScript(script) { } } +function findMappingScript(url) { + // 如果支持多页面, 需要提前传入 runner 和 测试脚本的对应关系, 也就是 subTasks + var script; + if (subTasks) { + Object.keys(subTasks).some(function(subUrl) { + if (url.indexOf(subUrl) > -1) { + script = subTasks[subUrl]; + } + }); + } + return script; +}; + function log(str) { if (ignoreLog) { return; From 4556e45e64eab77841a90f2c077dc2039b271fab Mon Sep 17 00:00:00 2001 From: leoner Date: Mon, 5 Jan 2015 23:25:34 +0800 Subject: [PATCH 2/3] refactor support inject local js --- lib/phantom-openurl.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/phantom-openurl.js b/lib/phantom-openurl.js index 0a7c64f..52bc69e 100644 --- a/lib/phantom-openurl.js +++ b/lib/phantom-openurl.js @@ -74,24 +74,46 @@ page.onUrlChanged = function(a, b) { function insertScripts(page, scripts, cb) { isInserted = true; async.eachSeries(scripts, function(script, callback) { - page.includeJs(script, function() { - log('loaded ' + script); + if (script.indexOf('http') < 0) { + page.injectJs(script); callback(); - // 有可能是缓存的原因, 第二次加载 js 的时候, 这个方法会触发两遍. - callback = function(){}; - }); + } 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; From e9897b6697a09828fbc8bb42fc0817e49f666748 Mon Sep 17 00:00:00 2001 From: leoner Date: Tue, 6 Jan 2015 16:21:52 +0800 Subject: [PATCH 3/3] refactor url match --- lib/driver.js | 1 - lib/phantom-openurl.js | 34 ++++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/driver.js b/lib/driver.js index f2b9be8..03692a0 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -55,7 +55,6 @@ proto.onAdd = function (data) { path.join(__dirname, 'phantom-openurl.js'), data.url, this.includeScripts, - this.script, this.ignoreLog, JSON.stringify(this.subTasks) ]; diff --git a/lib/phantom-openurl.js b/lib/phantom-openurl.js index 52bc69e..42f50ed 100644 --- a/lib/phantom-openurl.js +++ b/lib/phantom-openurl.js @@ -21,9 +21,8 @@ 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 subTasks = system.args[5]; +var ignoreLog = system.args[3]; +var subTasks = system.args[4]; if (subTasks) { try { @@ -39,13 +38,12 @@ if (includeScripts) { 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') { @@ -53,26 +51,20 @@ page.onLoadFinished = function(status) { exit(1); return; } - - if (!isInserted) { + if (script) { insertScripts(page, includeScripts, function() { executeScript(script); + script = null; }); } }; page.onUrlChanged = function(a, b) { - if (url === page.url) { - return; - } - - isInserted = false; script = findMappingScript(page.url); }; //include scripts; function insertScripts(page, scripts, cb) { - isInserted = true; async.eachSeries(scripts, function(script, callback) { if (script.indexOf('http') < 0) { page.injectJs(script); @@ -119,15 +111,29 @@ function findMappingScript(url) { var script; if (subTasks) { Object.keys(subTasks).some(function(subUrl) { - if (url.indexOf(subUrl) > -1) { + 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; }