且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS:在E2E测试访问范围

更新时间:2021-08-24 03:11:08

下面是一个基于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')
})