如何从角E2E的测试范围执行jQuery的? [英] How to execute jQuery from Angular e2e test scope?

查看:139
本文介绍了如何从角E2E的测试范围执行jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能执行从角E2E方案jQuery的命令?

例如,如果我想执行: $('。选择器-COL-ID-ID)ATTR('类');
我得到一个错误:


  

类型错误:对象[对象的对象]的属性$不是一个函数



解决方案

这里的问题是,AngularJs情景测试运行器运行在一个iframe应用程序。亚军本身没有加载jQuery的。

这是最好的使用角度场景DSL。从端到端测试文档


  

元素(选择器,标签)。{}方法(键,值)


  
  

执行传入键和值的元件匹配方法
  给定jQuery选择,其中方法可以是以下任意的
  jQuery方法:ATTR,道具,CSS。标签是用于测试输出


虽然不清楚从文档,还可以使用ATTR的方法只有1参数来获得属性的值。

 元素('选择器-COL-ID-ID。')ATTR(类)。

如果你需要其他的jQuery的功能,如对焦(),你可以这样来做:

 元素('。选择器-COL-ID-ID)。查询(函数(元素,完成){
    elements.focus();
    完成();
});

或延长角DSL

  angular.scenario.dsl('jQueryFunction',函数(){
    返回功能(选择,functionName / *,* ARGS /){
        变参= Array.prototype.slice.call(参数,2);
        返回this.addFutureAction(functionName,而功能($窗口,$文件,​​完成){
            变量$ = $ $窗口。在iframe内// jQuery的
            VAR ELEM = $(选择);
            如果(!elem.length){
                返回完成('选择'+选择+'没有匹配任何元素。');
            }
            DONE(NULL,ELEM [functionName]。适用(ELEM,参数));
        });
    };
});

和使用这种方式:

  jQueryFunction('选择器-COL-ID-ID。','焦点');

或一般的:

  jQueryFunction(选择,jQueryFunctionName,ARG1,ARG2,...);

Is it possible to execute jQuery commands from Angular e2e scenario ?

for example, if I would like to execute : $('.picker-col-id-id').attr('class'); I'm getting an error:

TypeError: Property '$' of object [object Object] is not a function

解决方案

The problem here is that the AngularJs Scenario Test Runner runs your application in an iframe. The runner itself hasn't loaded jQuery.

It's best to use the angular scenario dsl. From the e2e testing docs:

element(selector, label).{method}(key, value)

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.

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');

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();
});

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));
        });
    };
});

And use it this way:

jQueryFunction('.picker-col-id-id', 'focus');

Or in general:

jQueryFunction(selector, jQueryFunctionName, arg1, arg2, ...);

这篇关于如何从角E2E的测试范围执行jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆