doup
doup

Reputation: 911

Protractor wait() for debounced event + loading

I'm trying to make my protractor tests to wait a condition, but I get inconsistent behaviour. The scenario is this one:

  1. The user writtes some terms in a search box
  2. A debounced event is fired. So, this depends on when the user stops typing.
  3. We make an API request, meanwhile we show an is loading feedback and hide the close button.
  4. Feedback dissapears, close button reapears, continue.

So, to achieve this I'm observing the close button. Between user stops writing and debounce event fires close button is visible. After debounced event fires (we're making request), is loading element shows and close button hides [1]. When is finished close button shows again.

Any idea is really appreciated, thanks!

1: To ease testing I've make close button dissapear at least 200ms, so that in localhost doesn't dissapear ultra-fast. This should be ok since webdriver polls every 100ms for the wait condition.


So, this is my test:

it('should show "no results" feedback when there are no results', function () {
    app.getHomepage();
    app.topMenu.searchLink.click();
    app.search.termsInput.sendKeys('chipijanders');
    app.search.waitForSearch();

    expect(app.search.noResults.isDisplayed()).toBe(true);

    // Checking the waitForSearch consistency
    for (var i = 0; i < 10; i++) {
        app.search.closeLink.click();
        app.topMenu.searchLink.click();
        app.search.termsInput.sendKeys('chipijanders');
        app.search.waitForSearch();
        expect(app.search.noResults.isDisplayed()).toBe(true);
    }
});

And here waitForSearch:

waitForSearch: function () {
    var self = this;

    browser.driver.wait(function() {
        return self.closeLink.isDisplayed().then(function (isDisplayed) {
            return !isDisplayed;
        });
    });

    browser.driver.wait(function() {
        return self.closeLink.isDisplayed().then(function (isDisplayed) {
            return isDisplayed;
        });
    });
}

Upvotes: 2

Views: 1018

Answers (1)

el-davo
el-davo

Reputation: 449

Had an issue recently waiting for a decounced method in underscore. The solution I used was to override _.debounced in protractor.conf like so

onPrepare: function () {

    var debounceRightNow = function () {
        angular.module('debounceRightNow', []).run(
            function () {
                _.debounce = function (callback) {
                    return callback;
                };
            });
    };

    browser.addMockModule('debounceRightNow', debounceRightNow);
}

Not sure if that answers your question but it is a nifty solution

Upvotes: 1

Related Questions