AngularJS:在E2E测试访问范围 [英] AngularJS: Accessing scope in E2E test

查看:284
本文介绍了AngularJS:在E2E测试访问范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问$范围的端到端测试中都没有成功...

I'm trying to access $scope's within an E2E test without success...

作为一个测试,我尝试这样做:(我的网站不使用JQuery ..)

As a test I tried this: (My site does not use JQuery..)

转轮有我在嵌套iframe的网站,所以我直接访问它,然后让​​所有NG-范围,并试图对他们.scope(),如<一个href=\"http://stackoverflow.com/questions/10514147/in-angularjs-how-do-you-find-all-the-scopes-on-a-page\">this职位和code低于...

The runner has my site in a nested iframe, so I'm accessing it directly, then getting all ng-scopes and trying .scope() on them as in this post and code below...

var frameDocument = document.getElementById('test-frames').children[0].contentDocument;
var scopeElements = frameDocument.getElementsByClassName('ng-scope');
var scopes = [].map.call(scopeElements, function (e) {
return angular.element(e).scope();
});

以上code发现合适的元素,但呼吁他们的范围()每个返回undefined ....

The above code finds the proper elements, but calling scope() on them returns undefined for each....

有人可以证实或否认,我们可以在E2E访问范围有多大?我假设有一种方法?

Can someone confirm or deny that we can access the scope in E2E? I'd assume there is a way?

感谢,你

推荐答案

下面是一个基于previous回答我的绝招。

Here is my trick based on previous answer.

您可以将其扩展到动态范围。的主要部分是从addFutureAction得到参考APPWINDOW

You can extend it to dynamic scopes. The main part is getting the reference to appWindow from addFutureAction.

//HTML CODE

<body id="main-controller" ng-controller="mainCtrl" ng-init="__init__()">


//Scenario helper.

/*
Run `callback` with scope from `selector`.
*/
angular.scenario.dsl('scope', function() {
    return function(selector, callback) {
        return this.addFutureAction(
            'Executing scope at ' + selector,
            function(appWindow, $document, done) {
                var body = appWindow.document.getElementById(selector)
                var scope = appWindow.angular.element(body).scope()
                callback(scope)
                scope.$apply()
                done(null, 'OK');
            })
    }
})

//Actual test.

it(
'When alerts are defined, they are displayed.',
function() {
    scope('main-controller', function(scope) {
        scope.alerts.push({'type': 'error', 'msg': 'Some error.'})
    })

    expect(element('#alerts').css('display')).toEqual('block')
})

这篇关于AngularJS:在E2E测试访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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