Looping elements in WebdriverIO
<p>I'm trying to loop through a list of links, and perform some actions with each one. I can iterate the elements using <a href="http://webdriver.io/api/protocol/elements.html" rel="nofollow">elements</a>, but using <a href="http://webdriver.io/api/action/click.html" rel="nofollow">click</a> inside the forEach doesn't block the next step in forEach, and Selenium goes nuts, as it tries to continue doing actions on elements not in the DOM anymore.</p>

<pre><code>var q = require("q");
var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'chrome'
    }
};

var clicks = [];

var runner = webdriverio.remote(options);

runner
    .init()
    .url('https://www.google.dk/search?q=burrito')
    .elements(".r").then(function(res){
        res.value.forEach(function(elem){
            console.log(elem);
            clicks.push(
                runner
                    .elementIdClick(elem.ELEMENT)
                    .pause(5000)
                    .back()
                    .pause(2000)
            );
        });

        return q.all(clicks);
    });
</code></pre>

<p>How do I make sure the next iteration in the forEach doesn't run before all the code is executed inside the forEach?</p>

<p>Edit: I should have mentioned that I already tried <a href="https://github.com/webdriverio/webdriverio/issues/941" rel="nofollow">https://github.com/webdriverio/webdriverio/issues/941</a> and <a href="https://github.com/webdriverio/webdriverio/issues/273" rel="nofollow">https://github.com/webdriverio/webdriverio/issues/273</a>. I updated my code sample with something more specific.</p>