4

When writing E2E tests for Angular Scenario Runner I came across a query() method:

element(selector, label).query(fn)

The documentation says:

Executes the function fn(selectedElements, done), where selectedElements are the elements that match the given jQuery selector and done is a function that is called at the end of the fn function. The label is used for test output.

So I wrote an it-case for a set of buttons on my html-page:

it('should display all buttons on page load', function () {
    var buttonText = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
    element('button', 'UI elements').query(function (selectedElements, done) {
        selectedElements.each(function (idx, elm) {
            expect(elm.innerText in buttonText).toEqual(true);
        });
        done();
    });
});

As a result of test execution I received a list of 10 failed expect-clauses with text:

expected true but was undefined

Intermediate debugging showed that elm.innerText in buttonText condition is truthy.

So my question is, what went wrong? Is it the incorrect usage of done() method?

John Doe
  • 4,574
  • 2
  • 26
  • 30
  • I'm not sure, but it looks like you should be doing `elm.text()`, not `elm.innerText`, as you would get back angular-wrapped elements and not raw ones. – Andrew Joslin Nov 19 '12 at 13:56
  • @AndyJoslin I thought so too, but it is wrong. I receive `TypeError: Object [object HTMLButtonElement] has no method 'text'` error in case of using the `.text()` method. – John Doe Nov 19 '12 at 15:00
  • Try adding some `dump(elm.innerText)` and check and see what comes out. – Andrew Joslin Nov 19 '12 at 22:30
  • @AndyJoslin I get only one error `Uncaught TypeError: Cannot read property 'name' of null` when using `dump()`... – John Doe Nov 20 '12 at 11:00
  • @JohnDoe try another type of `expect` for `elm`, for example `elm.nodeType`. Do you use jQuery? Note: for `toEqual(true)` there is `toBeTruthly()` method. – Maxim Grach Feb 15 '13 at 18:56
  • Take a look at the [answer](http://stackoverflow.com/a/13473545/2037203) to the "[AngulareJS e2e check each link in a list](http://stackoverflow.com/questions/13442386/angularejs-e2e-check-each-link-in-a-list)" question. – Maxim Grach Feb 15 '13 at 19:06

2 Answers2

2

You can not call expect() inside the element().query() function.

But you can do the following

var promise = element('SELECTOR').query(function(selectedElements, done) {
    // We are in JQuery land.
    if (selectedElements < 1) {
      done('ERROR MESSAGE')
    } else {
      done(null, selectedElements[0].innerText)
    }
    // For documentation only.
    // Don't return anything... just continue.
    done()
})
expect(promise).toEqual('i am waiting for you')

You can combine promise and expect into a single call.

Adi Roiban
  • 1,293
  • 2
  • 13
  • 28
0

The code looks correct except for the 'expect' line:

expect(elm.innerText in buttonText).toEqual(true);

The parameter to 'expect' is a future. From the documentation, since All API statements return a future object, elm.innerText returns a future, but elm.innerText in buttonText does not, resulting in undefined. Perhaps, the way to code this would be:

expect(elm.innerText).toMatch(/\d/);
TsenYing
  • 280
  • 4
  • 10