Access JS Scope Elements / Variables Using Protractor

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

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

$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 to use Expractor expect () function for $ scope.authentication.user value?

+5
source share
2 answers

There is a function called evaluate that allows you to evaluate the angular expressions given by an element.

Select the DOM element for which you want to explore the scope, and then call the score. The documentation has an example:

http://angular.imtqy.com/protractor/#/api?view=ElementArrayFinder.prototype.evaluate

+6
source

Using the protractor, you perform end-to-end tests (for example, if you clicked on the interface — buttons, anchors — and read what was displayed — what you decided to finally expose ) the view), you do not have access to the JavaScript variables encapsulated inside your controllers.

The type of test you're talking about is unit test, where you test only your doLogin function by entering a $ scope object filled with relevant data, and then claiming that it does exactly what you expected by testing js variables.

Although, if $scope.authentication.user changes the view when set to true (for example, if you show "Authentication successfully" or even route to another view), you can claim with protractor that this behavior has passed (by checking the message "Authentication successfully "in the DOM after running loggin), but keep in mind that this is still e2e testing.

+4
source

Source: https://habr.com/ru/post/1209797/


All Articles