如何从 Angular e2e 测试范围执行 jQuery? [英] How to execute jQuery from Angular e2e test scope?
问题描述
是否可以从 Angular e2e 场景中执行 jQuery 命令?
Is it possible to execute jQuery commands from Angular e2e scenario ?
例如,如果我想执行:$('.picker-col-id-id').attr('class');
我收到一个错误:
for example, if I would like to execute : $('.picker-col-id-id').attr('class');
I'm getting an error:
类型错误:对象 [object Object] 的属性$"不是函数
推荐答案
这里的问题是 AngularJs Scenario Test Runner 在 iframe 中运行您的应用程序.运行程序本身还没有加载 jQuery.
The problem here is that the AngularJs Scenario Test Runner runs your application in an iframe. The runner itself hasn't loaded jQuery.
最好使用angular场景dsl.来自 e2e 测试文档:
It's best to use the angular scenario dsl. From the e2e testing docs:
element(selector, label).{method}(key, value)
在匹配的元素上执行传入key和value的方法给定的 jQuery 选择器,其中方法可以是以下任何一种jQuery 方法:attr、prop、css.标签用于测试输出.
Executes the method passing in key and value on the element matching the given jQuery selector, where method can be any of the following jQuery methods: attr, prop, css. The label is used for test output.
虽然文档中没有明确说明,但您也可以使用只有 1 个参数的 'attr' 方法来获取属性的值.
Although not clear from the docs, you can also use the 'attr' method with only 1 argument to get the value of the attribute.
element('.picker-col-id-id').attr('class');
如果您需要其他 jQuery 功能,例如 focus(),您可以这样做:
If you need other jQuery functionality, like focus(), you can do it this way:
element('.picker-col-id-id').query(function(elements, done) {
elements.focus();
done();
});
或者延长angular dsl
Or extend the angular 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, ...);
这篇关于如何从 Angular e2e 测试范围执行 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!