You can write your own custom function that will verify the counter value. This custom function could be passed to browser.wait function.
Assuming following are the files:
HTML file
<html ng-app="myApp" ng-controller="MainController">
<head>
<title>{{title}}</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="src/app.js"></script>
</head>
<body>
<div ng-controller="MyController">
<div class="main">{{data}}</div>
<div class="counter">{{counterVal}}</div>
<button class="counter-btn" ng-click="increaseCounter()">Increase Counter</button>
</div>
</body>
</html>
JS(Angular) file
angular.module('myApp', [])
.controller('MainController', MainController)
.controller('MyController', MyController);
function MainController($scope) {
$scope.title = 'Protractor test';
}
function MyController($scope, $timeout) {
$scope.data = 'Hi!';
$scope.counterVal = 0;
$scope.increaseCounter = function () {
// Async call, can be a network call.
$timeout(function () {
$scope.counterVal++;
}, 1000);
}
}
Protractor test file
describe('localhost test page', function () {
it('should open webpage', function () {
browser.get('http://localhost/angularjs/index.html');
expect(element(by.css('.main')).getText()).toEqual('Hi!');
expect(element(by.css('.counter')).getText()).toEqual('0');
// Click the button.
element(by.css('.counter-btn')).click();
browser.wait(waitForCounterIncrease('1'));
// Click again
element(by.css('.counter-btn')).click();
browser.wait(waitForCounterIncrease('2'));
});
// This is the custom funtion.
// Note: If condition is not met, this function will timeout based on the settings.
function waitForCounterIncrease(val) {
return function () {
var deferred = protractor.promise.defer();
element(by.css('.counter')).getText()
.then(function (counterVal) {
console.log(counterVal)
if (counterVal === val) {
deferred.fulfill(counterVal);
}
});
return deferred.promise;
}
}
});
If you run the above test, it should pass verifying the counter values.
browser.waitis not a blocking call. You do have to write your own function that performs the necessary checks. For more info read protractortest.org/#/… - Gaurav