How does the “element(selector, label).query(fn)” method work in Angular E2E tests?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-13 03:42:26

问题


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?


回答1:


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.




回答2:


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/);


来源:https://stackoverflow.com/questions/13451594/how-does-the-elementselector-label-queryfn-method-work-in-angular-e2e-tes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!