Search code examples
javascriptangularjsunit-testingprotractorend-to-end

Add two parameters in function into "then" protractor


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...


Solution

  • 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);
    }