title: Jasmine - Testing links via Webdriver I/O
body: <p>I have been working on a end-to-end test using Webdriver I/O from Jasmine. One specific scenario has been giving me significant challenges.</p>
  
  <p>I have a page with 5 links on it. The number of links actually challenges as the page is dynamic. I want to test the links to see if each links' <code>title</code> matches the <code>title</code> of the page that it links to. Due to the fact that the links are dynamically generated, I cannot just hard code tests for each link. So, I'm trying the following:</p>
  
  <pre><code>it('should match link titles to page titles', function(done) {
    client = webdriverio.remote(settings.capabilities).init()
      .url('http://www.example.com')
      .elements('a').then(function(links) {
        var mappings = [];
  
        // For every link store the link title and corresponding page title
        var results = [];
        for (var i=0; i&lt;links.value.length; i++) {
          mappings.push({ linkTitle: links.value[0].title, pageTitle: '' });
          results.push(client.click(links.value[i])
            .getTitle().then(function(title, i) {
              mappings[i].pageTitle = title;
            });
          );
        }
  
        // Once all promises have resolved, compared each link title to each corresponding page title
        Promise.all(results).then(function() {
          for (var i=0; i&lt;mappings.length; i++) {
            var mapping = mappings[i];
            expect(mapping.linkTitle).toBe(mapping.pageTitle);
          }
          done();          
        });                  
      });
    ;
  });
  </code></pre>
  
  <p>I'm unable to even confirm if I'm getting the link title properly. I believe there is something I entirely misunderstand. I am not even getting each links <code>title</code> property. I'm definately not getting the corresponding page title. I think I'm lost in closure world here. Yet, I'm not sure.</p>
  
  <p><strong>UPDATE - NOV 24</strong>
  I still have not figured this out. However, i believe it has something to do with the fact that Webdriver I/O <a href="https://github.com/webdriverio/webdriverio/blob/24e30dc6cb0c2429ac4e8863ef73eb874ef865dd/lib/utils/RequestHandler.js">uses</a> the Q promise library. I came to this conclusion because the following test works:</p>
  
  <pre><code>it('should match link titles to page titles', function(done) {
    var promise = new Promise(function(resolve, reject) {
      setTimeout(function() { resolve(); }, 1000);
    });
  
    promise.then(function() {
      var promises = [];
      for (var i=0; i&lt;3; i++) {
        promises.push(
          new Promise(function(resolve, reject) {
            setTimeout(function() {
              resolve();
            }, 500);
          })
        );
      }
  
      Promise.all(promises).then(function() {
        expect(true).toBe(true)
        done();
      });
  }); 
  </code></pre>
  
  <p>However, the following does <strong><em>NOT</em></strong> work:</p>
  
  <pre><code>it('should match link titles to page titles', function(done) {
    client = webdriverio.remote(settings.capabilities).init()
      .url('http://www.example.com')
      .elements('a').then(function(links) {
        var mappings = [];
  
        // For every link store the link title and corresponding page title
        var results = [];
        for (var i=0; i&lt;links.value.length; i++) {
          mappings.push({ linkTitle: links.value[0].title, pageTitle: '' });
          results.push(client.click(links.value[i])
            .getTitle().then(function(title, i) {
              mappings[i].pageTitle = title;
            });
          );
        }
  
        // Once all promises have resolved, compared each link title to each corresponding page title
        Q.all(results).then(function() {
          for (var i=0; i&lt;mappings.length; i++) {
            var mapping = mappings[i];
            expect(mapping.linkTitle).toBe(mapping.pageTitle);
          }
          done();          
        });                  
      })
    ;
  });
  </code></pre>
  
  <p>I'm not getting any exceptions. Yet, the code inside of <code>Q.all</code> does not seem to get executed. I'm not sure what to do here.</p>