如何从Angular e2e测试范围执行jQuery?
是否可以从Angular e2e场景执行jQuery命令?
例如,如果我想执行: $('.picker-col-id-id').attr('class');
我收到一个错误:
TypeError:对象[object Object]的属性’$’不是函数
这里的问题是AngularJs Scenario Test Runner在iframe中运行您的应用程序。 跑步者本身没有加载jQuery。
最好使用角度方案dsl。 从e2e测试文档 :
element(selector,label)。{method}(key,value)
执行在匹配给定jQuery选择器的元素上传入键和值的方法,其中method可以是以下任何jQuery方法:attr,prop,css。 标签用于测试输出。
虽然从文档中不清楚,但您也可以使用’attr’方法只使用1个参数来获取属性的值。
element('.picker-col-id-id').attr('class');
如果你需要其他jQueryfunction,比如focus(),你可以这样做:
element('.picker-col-id-id').query(function(elements, done) { elements.focus(); done(); });
或者扩展角度dsl
angular.scenario.dsl('jQueryFunction', function() { return function(selector, functionName /*, args */) { var args = Array.prototype.slice.call(arguments, 2); return this.addFutureAction(functionName, function($window, $document, done) { var $ = $window.$; // jQuery inside the iframe var elem = $(selector); if (!elem.length) { return done('Selector ' + selector + ' did not match any elements.'); } done(null, elem[functionName].apply(elem, args)); }); }; });
并以这种方式使用它:
jQueryFunction('.picker-col-id-id', 'focus');
或者一般来说:
jQueryFunction(selector, jQueryFunctionName, arg1, arg2, ...);
正如评论中所提到的,jQuery似乎缺失了。 您需要将其添加到您的karma配置文件或直接添加到runner.html。
如果你想将它添加到你的karma配置文件中,请增加files
条目以添加对jQuery的引用,例如,
files = [ ANGULAR_SCENARIO, ANGULAR_SCENARIO_ADAPTER, 'app/components/jquery/jquery.js', // <== This should point to jQuery 'test/e2e/**/*.js' ];
或者,如果您从runner.html
运行e2e测试, runner.html
可以在角度之后和脚本之前加载它,例如:
End2end Test Runner