Reputation: 69
I am trying to get a count of the arraylist & then trying to assert the presence of keyword in the values of the array. Below is my code which is having issues;
describe('My Test', function() {
it('Test starts', function() {
browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');
browser.sleep(5000).then(function(){});
var results = element.all(by.css(".sidesection>p>a"));
var results_count=results.count().then(function(counting){
console.log("There are total "+counting+" lines");
return counting;
})
results_count.then (function(count){
console.log("There are totalx "+count+" lines");
for (var iterate=1;iterate<count;iterate++){
results.get(iterate).getText().then(function(text){
console.log("The text in Relationship Type node line "+iterate+" is ---"+text);
expect(text.indexOf('Navigation')!=-1).toBeTruthy();
})
}
})
})
})
Output:
There are total 19 lines
There are totalx 19 lines
The text in Relationship Type node line 19 is ---Dropdowns
The text in Relationship Type node line 19 is ---Accordions
The text in Relationship Type node line 19 is ---Convert Weights
The text in Relationship Type node line 19 is ---Animated Buttons
The text in Relationship Type node line 19 is ---Side Navigation
The text in Relationship Type node line 19 is ---Top Navigation
The text in Relationship Type node line 19 is ---JS Animations
The text in Relationship Type node line 19 is ---Modal Boxes
The text in Relationship Type node line 19 is ---Progress Bars
The text in Relationship Type node line 19 is ---Parallax
The text in Relationship Type node line 19 is ---Login Form
The text in Relationship Type node line 19 is ---HTML Includes
The text in Relationship Type node line 19 is ---Google Maps
The text in Relationship Type node line 19 is ---Loaders
The text in Relationship Type node line 19 is ---Tooltips
The text in Relationship Type node line 19 is ---Slideshow
The text in Relationship Type node line 19 is ---Filter List
The text in Relationship Type node line 19 is ---Sort List
[31mF[0m
Failures:
1) My Test Test starts
Message:
[31m Expected false to be truthy.
I am having 2 queries here on which i am stuck:
1.) Why am i getting the number 19 hardcoded in all the list of values, i want the output count to be iterative like 1,2,3,4... so on
2.) Why is my expect statement failing although the keyword is present in some of the array values.
Could someone correct me in understanding & getting the above 2 problems solved ?
Upvotes: 1
Views: 49
Reputation: 3266
On (1) I'm not positive, but I can definitely answer (2) help improve your code a little
1) This seems like the classic for
loop scope problem, where the loop has been completed by the time it is called... See this question for reference. Not positive how this comes into play with Protractor and the control flow execution.
2) Your expect is failing because it checks each line, you are saying the condition of each line of text compared to 'Navigation' will evaluate as truthy. This will fail for quite a few of them (i.e. Slideshow, Tooltips, Loaders etc). You need a better assertion, for example you could just do the links 1 by 1: expect(results.get(i).getText()).toEqual('Help')
, or you could make an array of nav items and expect them to match etc... but you definitely need a better assertion. What exactly is this test trying to do?
Either way, here's some help with your code in general:
for
loops in protractor, unless you are doing something very specific. You can just use each
to iterate over an ElementArrayFinder.This is more semantics, but you can use a value returned from a promise instead of assigning it to a variable, some of your code is somewhat redundant. You can omit the part about results_count
if you implement it like this:
results.count().then(function(counting){
console.log("There are total "+counting+" lines"); // logs 19
return counting;
}).then(function (count) {
console.log(count); // logs 19
for(var i = 0; i<count; i++) {
}
})
But again, for
loops aren't really necessary in Protractor. Instead you can just use each
, which makes your code more concise and also removes that loop closure problem you are experiencing:
var results = element.all(by.css(".sidesection>p>a"));
results.each(function (elem, index) {
return elem.getText().then(function (text) {
console.log('Element at index ' + index + ' has text ' + text);
// this still fails because it's not a good assertion
expect(text.indexOf('Navigation')!=-1).toBeTruthy();
});
});
Upvotes: 1