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

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

 element(selector, label).query(fn) 

The documentation says:

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

So, I wrote it 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 the test, I got a list of 10 unsuccessful pending offers with the text:

Expected true but undefined

Interim debugging showed that the elm.innerText in buttonText condition is true.

So my question is: what went wrong? Is this a misuse of the done() method?

+4
source share
2 answers

You cannot call expect () inside the element () function. query ().

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 a promise and count on one challenge.

+2
source

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

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

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

 expect(elm.innerText).toMatch(/\d/); 
0
source

Source: https://habr.com/ru/post/1446913/


All Articles