3

I am wishing to conditionally execute some commands/assertions depending on whether previous assertions succeeded or not.

I can't seem to find a way to nest then calls or conditionally execute things using the chaining syntax of leadfoot.

Here is my test (the returns at the end of each function is due to this being compiled coffeescript):

tdd.test(tableUrl, function() {
    this.timeout = 60000;
    return this.remote.get(require.toUrl("tests/pages/" + tableUrl))
    .setPageLoadTimeout(60000)
    .setFindTimeout(20000)
    .findByCssSelector('.dataTables_wrapper table.dataTable')
    .then(null, function(err) {
      return assert.fail('', '', 'Table should exist');
    }).then(pollUntil(function() {
      var table;
      table = document.querySelector('.dataTables_wrapper table.dataTable');
      if (table.querySelectorAll('tbody td').length > 1) {
        return true;
      } else {
        return null;
      }
    }, null, 20000, 500)).then(function() {
      console.log('Assertion succeeded: Table should have data');
      return assert.ok(true, 'Table should have data');
    }, function(err) {
      return assert.fail('', '', 'Table should have data');
    });
  });

When a table does not exist, both 'Table should exist' and 'Table should have data' assertions should be reported as failing. However, only the last assertion is failing. When I comment out the 'Table should have data' error callback, the 'Table should exist' assertion is reported as failing correctly.

What I would like to do is

  1. test if a table exists (via findByCssSelector).

    a) If it exists, report that it exists and test to see that it has more than one td element. This is currently done via pollUntil to ensure that the command is complete/promise is resolved as soon as more than one td is found instead of waiting for the whole implicit wait time.

    b) If it does not exist, report that it does not exist.

I can't seem to find a way of not executing the error callback of the second 'Table should have data' poll if the first findByCssSelector fails, due to lack of conditionals and only the last assertion failure being reported in a test.

Michael
  • 5,994
  • 7
  • 44
  • 56

1 Answers1

3

So conditional branching can happen with then calls, this answer addresses how to do conditional branching with intern.

The problem I was having was the pollUntil function not behaving the same way as a normal leadfoot Command method due to the function returning a callback function that returns a promise rather than a promise directly.

This can be circumvented by wrapping the pollUntil in an anonymous function, immediately calling the pollUntil function using the call method, passing in the current this value due to then setting the context of the callback to the Command object, and then finally chaining another Command.

This is what the above code turned into using correct conditional branching with pollUntil.

tdd.test(tableUrl, function() {
  this.timeout = 60000;
  return this.remote.get(require.toUrl("tests/pages/" + tableUrl))
  .setPageLoadTimeout(60000)
  .setFindTimeout(5000)
  .findByCssSelector('.dataTables_wrapper table.dataTable')
  .then((function() {
    return pollUntil(function() {
      var table;
      table = document.querySelector('.dataTables_wrapper table.dataTable');
      if (table.querySelectorAll('tbody td').length > 1) {
        return true;
      } else {
        return null;
      }
    }, null, 20000, 500).call(this).then(function() {
      console.log('Assertion succeeded: Table should have data');
      return assert.ok(true, 'Table should have data');
    }, function(err) {
      return assert.fail('', '', 'Table should have data');
    });
  }), function(err) {
    return assert.fail('', '', "Table should exist. Error: " + err.name);
  });
});
Community
  • 1
  • 1
Michael
  • 5,994
  • 7
  • 44
  • 56