javascript 使用量角器访问 JS 范围元素/变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27627781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:44:02  来源:igfitidea点击:

Accessing JS scope elements/variables with Protractor

javascriptangularjsprotractor

提问by Tahsis Claus

I have a Protractor test that enters login data and clicks the login button and I wish to check the value of an Angular variable.

我有一个输入登录数据并单击登录按钮的量角器测试,我希望检查 Angular 变量的值。

The ng-click for the clicked element is doLogin() which is defined in the controller file as:

单击元素的 ng-click 是 doLogin(),它在控制器文件中定义为:

$scope.doLogin  = function(){
        console.log('login -- todo');
        // remember email used
        localStorageService.add('lastKeyEmail', $scope.data.login.key.email);
        // todo - make dynamic
        $scope.authentication.user = true;
        // set email of logged in user
        // todo: would need to be done in user service (set user details received from server)
        Authentication.setEmail($scope.data.login.key.email);
        // overwrite password in memory
        $scope.data.login.password = "thispasswordisdeletedsoyoucantreadit";
    };

How do I use Protractor's expect() on the value of $scope.authentication.user?

如何对 $scope.authentication.user 的值使用量角器的 expect()?

回答by Andres D

There is a function called evaluatethat allows you to evaluate an angular expressions given an element.

有一个被调用的函数evaluate,它允许您评估给定元素的角度表达式。

Select a DOM element for which you want to examine the scope, then call evaluate. There is an example in the documentation:

选择要检查其范围的 DOM 元素,然后调用评估。文档中有一个例子:

http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.evaluate

http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.evaluate

回答by topheman

With protractor, you execute end-to-end tests (like if you were a user clicking on the interface - buttons, anchors - and reading what's rendered on - what you decide to finally exposeto the view), you don't have access to the JavaScript variables encapsulated inside your controllers.

使用量角器,您可以执行端到端测试(例如,如果您是用户单击界面 - 按钮、锚点 - 并阅读呈现的内容 - 您决定最终向视图公开的内容),您无权访问封装在控制器中的 JavaScript 变量。

The kind of test you're talking about is a unit test, where you only test your doLoginfunction, by injecting a $scope object populated with the proper data and then you assert that it does exactly what you expected by testing js variables.

您正在谈论的测试类型是单元测试,您只测试您的doLogin函数,方法是注入一个填充有正确数据的 $scope 对象,然后通过测试 js 变量断言它完全符合您的预期。

Although, if $scope.authentication.usermodifies the view when set to true(like if you display "Authentication successfull", or even route to an other view), you can assert with protractor that this behavior went through (by checking the "Authentication successfull" message in the DOM after you triggered the loggin) - but keep in mind this is still e2e testing.

虽然,如果$scope.authentication.user在设置为时修改视图true(例如,如果您显示“身份验证成功”,甚至路由到其他视图),您可以使用量角器断言此行为已通过(通过检查 DOM 中的“身份验证成功”消息触发登录后) - 但请记住,这仍然是 e2e 测试。