Commit dccce079 authored by Wang Qiu's avatar Wang Qiu

add waitForPageLoad and refactor a little.

parent de4b6323
......@@ -15,8 +15,8 @@ module.exports = function(grunt) {
},
watch: {
test: {
files: ['./testSpec.js'],
tasks: ['connect', 'karma:e2e']
files: ['./testSpec.js', './karma-e2e-dsl.js'],
tasks: ['connect', 'uglify:build', 'karma:e2e']
},
},
karma: {
......
/*! karma-e2e-dsl - v0.1.0 - 2013-11-05 */!function(a,b,c){"use strict";function d(a,c){var e=b.Deferred(),f=c||0,g=a[f],h=[].slice.call(arguments,2);return g?g.apply(g,h).then(function(){var b=[].slice.call(arguments);d.apply(this,[a,++f].concat(b))}):e.resolve(),e.promise()}function e(a){var b=a.split("/");return b[b.length-1]}function f(a){var b=e(a);return a.substring(0,a.length-b.length)}function g(){for(var a="",b=!1,c=arguments.length-1;c>=-1&&!b;c--){var d=c>=0?arguments[c]:"/";"string"==typeof d&&d&&(a=d+"/"+a,b="/"===d.charAt(0))}return a=a.replace(/\/\//g,"/"),"/"===a.charAt(a.length-1)&&(a=a.substring(0,a.length-1)),a}function h(a){return function(){var c=b.Deferred();return c.then(a),c.resolve(),c.promise()}}function i(a,b){return"undefined"!=typeof b&&(a.loadedDone&&a.pop(),a.push(h(b)),a.loadedDone=!0),a}function j(a){return function(b){S=[],a(),i(S,b),0==T&&d(S)}}function k(){return b(R.prop("contentDocument"))}function l(){return b(R.prop("contentWindow"))}function m(a){return function(){var c=b.Deferred();return R.load(function(){c.resolve()}),R.attr("src",a),c.promise()}}function n(){return function(){var a=b.Deferred();return R.one("load",function(){a.resolve()}),l().prop("location").reload(),a.promise()}}function o(a,c){return function(){var d=b.Deferred();return d.done(c),d.resolve(l().prop("location")[a]),d.promise()}}function p(a,b){T++,setTimeout(function(){var b=S.pop();a(),S.push(b),0==--T&&d(S)},b)}function q(a){return function(){var c=b.Deferred();return setTimeout(function(){c.resolve()},a),c.promise()}}function r(a){return function(){var c=b.Deferred();return c.resolve(k().find(a)),c.promise()}}function s(a){return this===c?new s(a):(S.push(r(a)),void 0)}function t(a,c,d){return function(e){var f=b.Deferred(),g=typeof d;return"undefined"!==g&&"function"!==g?(a(e),f.resolve(e)):(f.then(d),f.resolve(c(e))),f.promise()}}function u(a,b,c){return t(function(d){d[a](b,c)},function(c){return c[a](b)},c)}function v(a,b){return u("attr",a,b)}function w(a,b){return u("prop",a,b)}function x(a,b){return t(function(c){c[a](b)},function(b){return b[a]()},b)}function y(a){return t(function(d){a.length!==c?a:[a];d.find("option").each(function(){-1!=a.indexOf(b(this).val())&&b(this).prop("selected",!0)})},function(a){var c=[];return a.find("option:selected").each(function(){c.push(b(this).val())}),c.length>1?c:c[0]},a)}function z(a){return function(c){var d=b.Deferred();return d.then(a),d.resolve(c),d.promise()}}function A(a){return"undefined"==typeof a?w("checked",!0):function(c){var d=b.Deferred();return c.each(function(c,d){b(d).val()==a&&b(d).prop("checked",!0)}),d.resolve(),d.promise()}}function B(a){return function(c){if("A"===c.prop("tagName")&&"click"===a){var d=l().prop("location").pathname,e=g(f(d),c.attr("href"));return m(e)()}var h=b.Deferred();return c.trigger(a),h.resolve(),h.promise()}}function C(a){return function(){var c=b.Deferred();return a&&a.apply(this,[c].concat([].slice.call(arguments))),c.promise()}}function D(a){return this===c?new D(a):(S.push(C(function(a,b){a.resolve(b,!0)})),void 0)}function E(){return C(function(a,b,c){a.resolve(b,!c)})}function F(a,b,c){return C(function(d,e,f){d.resolve(a,e,b,f,f===c(e,b))})}function G(a){if(M(a))return"'"+a+"'";if(b.isArray(a)){var c="";return b(a).each(function(a,b){c+=G(b)+", "}),c=c.substring(0,c.length-2),"["+c+"]"}return a}function H(a){return a?"":" not"}function I(a,b,c,d){var e="AssertError: ";switch(a){case"toEqual":e+="expect "+G(b)+H(d)+" to equal "+G(c);break;case"toBe":e+="expect "+G(b)+H(d)+" to be "+G(c);break;case"toBeDefined":e+="expect "+G(b)+H(d)+" to be defined.";break;case"toContain":e+="expect "+G(b)+H(d)+" to contain "+G(c);break;case"toMatch":e+="expect "+G(b)+H(d)+" to match "+G(c);break;case"toBeLessThan":e+="expect "+G(b)+H(d)+" to be less than "+G(c);break;case"toBeGreaterThan":e+="expect "+G(b)+H(d)+" to be greater than "+G(c)}return e}function J(){return C(function(a,b,c,d,e,f){if(f===!1)throw I(b,c,d,e);a.resolve()})}function K(a,c){if(a===c)return!0;if(b.isArray(a)&&b.isArray(c)){if(a.length!=c.length)return!1;for(var d=0;d<a.length;d++)if(!K(a[d],c[d]))return!1;return!0}return N(a)&&N(c)?a.getTime()===c.getTime():O(a)&&O(c)?a.source===c.source&&a.global===c.global&&a.multiline===c.multiline&&a.lastIndex===c.lastIndex&&a.ignoreCase===c.ignoreCase:P(a)||P(c)?!1:a==c}function L(a,b,c){S.push(F(a,b,c)),S.push(J())}function M(a){return"[object String]"==={}.toString.call(a)}function N(a){return"[object Date]"==={}.toString.call(a)}function O(a){return"[object RegExp]"==={}.toString.call(a)}function P(a){return"[object Object]"==={}.toString.call(a)}function Q(a,c){return M(a)?a.indexOf(c)>=0:b.isArray(a)?b.inArray(c,a)>=0:!1}document.write('<iframe id="context" width="100%" height="500px" src="about:blank"></iframe>');var R=b("#context"),S=[],T=0,U=null,V={navigateTo:function(a){S.push(m(a))},reload:function(){S.push(n())},delay:function(a,b){p(a,b)},sleep:function(a){S.push(q(a))},pause:function(){S.push(function(){return U=b.Deferred(),U.promise()})},resume:function(){U.resolve(),U=null},window:{path:function(a){S.push(o("pathname",a))},href:function(a){S.push(o("href",a))},hash:function(a){S.push(o("hash",a))},search:function(a){S.push(o("search",a))}}},W={constructor:s,enter:function(a){S.push(v("value",a))},check:function(){S.push(w("checked",!0))},uncheck:function(){S.push(w("checked",!1))},select:function(a){S.push(A(a))},click:function(){S.push(B("click"))},isChecked:function(a){S.push(w("checked",a))},isSelected:function(a){S.push(w("checked",a))},isDisabled:function(a){S.push(w("disabled",a))},option:function(a){S.push(y(a))},options:function(){var a=[].slice.call(arguments);S.push(y(a.length>1?a:a[0]))},query:function(a){S.push(z(a))}};b(["attr","prop","css"]).each(function(a,b){W[b]=function(a,c){S.push(u(b,a,c))}}),b(["val","text","count#size","html","height","innerHeight","outerHeight","width","innerWidth","outerWidth","position","scrollLeft","scrollTop","offset"]).each(function(a,b){var c=b.split("#");W[c[0]]=function(a){S.push(x(c[1]||c[0],a))}}),s.prototype=W;var X={constructor:D,toEqual:function(a){L("toEqual",a,K)},toBe:function(a){L("toBe",a,function(a,b){return a===b})},toBeTruthy:function(){this.toBe(!0)},toBeFalsy:function(){this.toBe(!1)},toBeNull:function(){this.toBe(null)},toBeDefined:function(){L("toBeDefined",c,function(a){return"undefined"!=typeof a})},toContain:function(a){L("toContain",a,function(a,b){return Q(a,b)})},toMatch:function(a){L("toMatch",a,function(a,b){return new RegExp(b).test(a)})},toBeLessThan:function(a){L("toBeLessThan",a,function(a,b){return b>a})},toBeGreaterThan:function(a){L("toBeGreaterThan",a,function(a,b){return a>b})},not:function(){return S.push(E()),this}};D.prototype=X,a.browser=V,a.dsl=j,a.dropdownlist=a.input=a.element=s,a.expect=D}(this,jQuery);
\ No newline at end of file
/*! karma-e2e-dsl - v0.1.0 - 2013-11-07 */!function(a,b,c){"use strict";function d(a,c){var e=b.Deferred(),f=c||0,g=a[f],h=[].slice.call(arguments,2);return g?g.apply(g,h).then(function(){var b=[].slice.call(arguments);d.apply(this,[a,++f].concat(b))}):e.resolve(),e.promise()}function e(a){var b=a.split("/");return b[b.length-1]}function f(a){var b=e(a);return a.substring(0,a.length-b.length)}function g(){for(var a="",b=!1,c=arguments.length-1;c>=-1&&!b;c--){var d=c>=0?arguments[c]:"/";"string"==typeof d&&d&&(a=d+"/"+a,b="/"===d.charAt(0))}return a=a.replace(/\/\//g,"/"),"/"===a.charAt(a.length-1)&&(a=a.substring(0,a.length-1)),a}function h(a){return function(){var c=b.Deferred();return c.then(a),c.resolve(),c.promise()}}function i(a,b){return"undefined"!=typeof b&&(a.loadedDone&&a.pop(),a.push(h(b)),a.loadedDone=!0),a}function j(a){return function(b){U=[],a(),i(U,b),0==V&&d(U)}}function k(){return b(T.prop("contentDocument"))}function l(){return b(T.prop("contentWindow"))}function m(a){return E(function(b){T.load(function(){b.resolve()}),T.attr("src",a)})}function n(){return E(function(a){T.one("load",function(){a.resolve()}),l().prop("location").reload()})}function o(a,b){return E(function(c){c.done(b),c.resolve(l().prop("location")[a])})}function p(a,b){V++,setTimeout(function(){var b=U.pop();a(),U.push(b),0==--V&&d(U)},b)}function q(a){return E(function(b){var c=r(arguments);setTimeout(function(){b.resolve.apply(b,c)},a)})}function r(a){return[].slice.call(a,1)}function s(){return E(function(a){var b=r(arguments);T.one("load",function(){a.resolve.apply(a,b)})})}function t(a){return function(){var c=b.Deferred();return c.resolve(k().find(a)),c.promise()}}function u(a){return this===c?new u(a):(U.push(t(a)),void 0)}function v(a,c,d){return function(e){var f=b.Deferred(),g=typeof d;return"undefined"!==g&&"function"!==g?(a(e),f.resolve(e)):(f.then(d),f.resolve(c(e))),f.promise()}}function w(a,b,c){return v(function(d){d[a](b,c)},function(c){return c[a](b)},c)}function x(a,b){return w("attr",a,b)}function y(a,b){return w("prop",a,b)}function z(a,b){return v(function(c){c[a](b)},function(b){return b[a]()},b)}function A(a){return v(function(d){a.length!==c?a:[a];d.find("option").each(function(){-1!=a.indexOf(b(this).val())&&b(this).prop("selected",!0)})},function(a){var c=[];return a.find("option:selected").each(function(){c.push(b(this).val())}),c.length>1?c:c[0]},a)}function B(a){return E(function(b,c){b.then(a),b.resolve(c)})}function C(a){return"undefined"==typeof a?y("checked",!0):E(function(c,d){d.each(function(c,d){b(d).val()==a&&b(d).prop("checked",!0)}),c.resolve()})}function D(a){return function(c){if("A"===c.prop("tagName")&&"click"===a){var d=l().prop("location").pathname,e=g(f(d),c.attr("href"));return m(e)()}var h=b.Deferred();return c.trigger(a),h.resolve(),h.promise()}}function E(a){return function(){var c=b.Deferred();return a&&a.apply(this,[c].concat([].slice.call(arguments))),c.promise()}}function F(a){return this===c?new F(a):(U.push(E(function(a,b){a.resolve(b,!0)})),void 0)}function G(){return E(function(a,b,c){a.resolve(b,!c)})}function H(a,b,c){return E(function(d,e,f){d.resolve(a,e,b,f,f===c(e,b))})}function I(a){if(O(a))return"'"+a+"'";if(b.isArray(a)){var c="";return b(a).each(function(a,b){c+=I(b)+", "}),c=c.substring(0,c.length-2),"["+c+"]"}return a}function J(a){return a?"":" not"}function K(a,b,c,d){var e="AssertError: ";switch(a){case"toEqual":e+="expect "+I(b)+J(d)+" to equal "+I(c);break;case"toBe":e+="expect "+I(b)+J(d)+" to be "+I(c);break;case"toBeDefined":e+="expect "+I(b)+J(d)+" to be defined.";break;case"toContain":e+="expect "+I(b)+J(d)+" to contain "+I(c);break;case"toMatch":e+="expect "+I(b)+J(d)+" to match "+I(c);break;case"toBeLessThan":e+="expect "+I(b)+J(d)+" to be less than "+I(c);break;case"toBeGreaterThan":e+="expect "+I(b)+J(d)+" to be greater than "+I(c)}return e}function L(){return E(function(a,b,c,d,e,f){if(f===!1)throw K(b,c,d,e);a.resolve()})}function M(a,c){if(a===c)return!0;if(b.isArray(a)&&b.isArray(c)){if(a.length!=c.length)return!1;for(var d=0;d<a.length;d++)if(!M(a[d],c[d]))return!1;return!0}return P(a)&&P(c)?a.getTime()===c.getTime():Q(a)&&Q(c)?a.source===c.source&&a.global===c.global&&a.multiline===c.multiline&&a.lastIndex===c.lastIndex&&a.ignoreCase===c.ignoreCase:R(a)||R(c)?!1:a==c}function N(a,b,c){U.push(H(a,b,c)),U.push(L())}function O(a){return"[object String]"==={}.toString.call(a)}function P(a){return"[object Date]"==={}.toString.call(a)}function Q(a){return"[object RegExp]"==={}.toString.call(a)}function R(a){return"[object Object]"==={}.toString.call(a)}function S(a,c){return O(a)?a.indexOf(c)>=0:b.isArray(a)?b.inArray(c,a)>=0:!1}document.write('<iframe id="context" width="100%" height="500px" src="about:blank"></iframe>');var T=b("#context"),U=[],V=0,W=null,X={navigateTo:function(a){U.push(m(a))},reload:function(){U.push(n())},delay:function(a,b){p(a,b)},sleep:function(a){U.push(q(a))},pause:function(){U.push(function(){return W=b.Deferred(),W.promise()})},resume:function(){W.resolve(),W=null},waitForPageLoad:function(){U.push(s())},window:{path:function(a){U.push(o("pathname",a))},href:function(a){U.push(o("href",a))},hash:function(a){U.push(o("hash",a))},search:function(a){U.push(o("search",a))}}},Y={constructor:u,enter:function(a){U.push(x("value",a))},check:function(){U.push(y("checked",!0))},uncheck:function(){U.push(y("checked",!1))},select:function(a){U.push(C(a))},click:function(){U.push(D("click"))},isChecked:function(a){U.push(y("checked",a))},isSelected:function(a){U.push(y("checked",a))},isDisabled:function(a){U.push(y("disabled",a))},option:function(a){U.push(A(a))},options:function(){var a=[].slice.call(arguments);U.push(A(a.length>1?a:a[0]))},query:function(a){U.push(B(a))}};b(["attr","prop","css"]).each(function(a,b){Y[b]=function(a,c){U.push(w(b,a,c))}}),b(["val","text","count#size","html","height","innerHeight","outerHeight","width","innerWidth","outerWidth","position","scrollLeft","scrollTop","offset"]).each(function(a,b){var c=b.split("#");Y[c[0]]=function(a){U.push(z(c[1]||c[0],a))}}),u.prototype=Y;var Z={constructor:F,toEqual:function(a){N("toEqual",a,M)},toBe:function(a){N("toBe",a,function(a,b){return a===b})},toBeTruthy:function(){this.toBe(!0)},toBeFalsy:function(){this.toBe(!1)},toBeNull:function(){this.toBe(null)},toBeDefined:function(){N("toBeDefined",c,function(a){return"undefined"!=typeof a})},toContain:function(a){N("toContain",a,function(a,b){return S(a,b)})},toMatch:function(a){N("toMatch",a,function(a,b){return new RegExp(b).test(a)})},toBeLessThan:function(a){N("toBeLessThan",a,function(a,b){return b>a})},toBeGreaterThan:function(a){N("toBeGreaterThan",a,function(a,b){return a>b})},not:function(){return U.push(G()),this}};F.prototype=Z,a.browser=X,a.dsl=j,a.dropdownlist=a.input=a.element=u,a.expect=F}(this,jQuery);
\ No newline at end of file
......@@ -96,34 +96,28 @@
}
function navigateTo(path) {
return function () {
var defer = $.Deferred();
return deferred(function (defer) {
ifr.load(function () {
defer.resolve();
});
ifr.attr('src', path);
return defer.promise();
};
});
}
function reload() {
return function () {
var defer = $.Deferred();
return deferred(function (defer) {
ifr.one('load', function () {
defer.resolve();
});
win().prop('location').reload();
return defer.promise();
};
});
}
function locationProp(propName, handler) {
return function () {
var defer = $.Deferred();
return deferred(function (defer) {
defer.done(handler);
defer.resolve(win().prop('location')[propName]);
return defer.promise();
};
});
}
function delay(callback, duration) {
......@@ -139,13 +133,25 @@
}
function sleep(duration) {
return function() {
var defer = $.Deferred();
return deferred(function (defer) {
var args = argsPassingThrough(arguments);
setTimeout(function () {
defer.resolve();
defer.resolve.apply(defer, args);
}, duration);
return defer.promise();
}
});
}
function argsPassingThrough(_arguments) {
return [].slice.call(_arguments, 1);
}
function waitForPageLoad() {
return deferred(function (defer) {
var args = argsPassingThrough(arguments);
ifr.one('load', function () {
defer.resolve.apply(defer, args);
});
});
}
var pauseDefer = null;
......@@ -172,6 +178,9 @@
pauseDefer.resolve();
pauseDefer = null;
},
waitForPageLoad: function () {
dslList.push(waitForPageLoad());
},
window: {
path: function (pathHandler) {
dslList.push(locationProp('pathname', pathHandler));
......@@ -260,28 +269,24 @@
}
function query(selectedElementsHandler) {
return function ($elems) {
var defer = $.Deferred();
return deferred(function (defer, $elems) {
defer.then(selectedElementsHandler);
defer.resolve($elems);
return defer.promise();
};
});
}
function select(value) {
if(typeof(value) === 'undefined') {
return prop('checked', true);
} else {
return function ($elems) {
var defer = $.Deferred();
return deferred(function (defer, $elems) {
$elems.each(function (idx, element) {
if($(element).val() == value) {
$(element).prop('checked', true);
}
});
defer.resolve();
return defer.promise();
};
});
}
}
......
......@@ -63,6 +63,7 @@ module.exports = function(config) {
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['Chrome'],
urlRoot: '/__karma/',
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
......
......@@ -196,10 +196,8 @@ describe('karma e2e dsl', function () {
it('should not be enabled', dsl(function() {
input('[type="submit"]').click();
browser.sleep(500);
browser.window.href(function (href) {
href.should.contain('dropdownlist=1');
});
browser.waitForPageLoad();
expect(browser.window.href()).toContain('dropdownlist=1');
}));
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment