1

It is quite clear. I have an array with some links and I want to build a loopto try all of them, but the problem is that link is always 3. It means that it read the last number in my array. Why? How can I fix it?

var categories = ['1','2','3'];
for( var i = 0; i < categories.length; i++ ) {
    var link = '/'+categories[i];
    browser.get(link);
    browser.sleep(2000);
    browser.driver.getCurrentUrl().then( function(url) {
        expect(url).toMatch(link);
    });
}

and I have list of divs and I want to read randomly infos from them. So I made the following

chosenOffer         = Math.floor( (Math.random() * count ) + 1);
offer               = element.all( by.className('offer')).get( chosenOffer );

But it shows always error message chosenOffer object...

dfr
  • 13
  • 5

1 Answers1

3

This is a classic closure problem that is described in detail in:

In your case, just let expect() resolve the promise:

var categories = ['1','2','3'];

for (var i = 0; i < categories.length; i++) {
    var link = '/' + categories[i];
    browser.get(link);
    browser.sleep(2000);

    expect(browser.driver.getCurrentUrl()).toMatch(link);
}
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • just another question chosenOffer = Math.floor( (Math.random() * count ) + 1); // choose random offer todo fix it in protractor syntax offer = element.all( by.className('offer')).get( offer ); why i get offer is object ...error – dfr Jan 14 '15 at 15:31
  • @dfr could you please make a separate question out of it (if it makes sense)? Thanks. – alecxe Jan 14 '15 at 15:36
  • I re-edit my questions bc I need to wait 90 minutes to post new question – dfr Jan 14 '15 at 15:57