Search code examples
javascriptpromisefunctional-testinginternleadfoot

Leadfoot session object returns promises


I am attempting to use the leadfoot module for functional testing with the intern and selenium.

For this test, I'm trying to click a button in one place, then check the display property of a element elsewhere on the page.

I couldn't find a way to expand the search of the findById call, so I tried using the session property, which seems to work, but results in everything returning a promise.

The only way I've found that will make it work is by chaining then functions. What makes the session (and the elements its functions return) different?

return this.remote
    .findById('buttonContainer')
    .findByClassName('buttonClass')
    .click()
    .session 
    .findById('stagePanel')
    .then(function(element) {
        element.findByClassName('itemList')
        .then(function(element) {
            element.getComputedStyle('display')
            .then(function (display) {
                // check display property
            });
        });

    });

I'm sure I'm doing many things wrong, so any and all advice is appreciated.


Solution

  • The this.remote object is a Command object, not a Session or Element object. If you want a Session you can get it from this.remote.session but it’s not normally necessary, and the Session object is not chainable.

    The reason why your second findById is not working is because you aren’t ending filtering you added with your previous findBy calls. When you don’t call end after a find operation, any subsequent find operation will use the element(s) from the previous find as the root elements to search in.

    In other words, when you run this.remote.findById('a').findById('b'), it will search for element 'b' inside of element 'a', not inside the whole document this.remote.findById('a').end().findById('b') would search for both 'a' and 'b' inside the whole document.

    Also, any time you perform an asynchronous operation from within a callback you need to return the result of the operation. If you don’t, the test won’t know that it needs to wait for more operations to complete. return chaining also prevents callback pyramids:

    return this.remote
        .findById('buttonContainer')
          .findByClassName('buttonClass')
            .click()
            .end(2)
        .findById('stagePanel')
        .then(function(stagePanel) {
            return stagePanel.findByClassName('itemList');
        }).then(function(itemList) {
            return itemList.getComputedStyle('display');
        }).then(function (display) {
            // check display property
        });