Fernanda Tenório
Fernanda Tenório

Reputation: 11

How do I expect the page loads to execute the next command in protractor?

I need to fix this problem. I have tests in protractor, but they fail because the commands execute before the page loads. I used sleep function to try to solve this problem, but this is not the better way and in some moments this fails too. I tried the following global function, but in some moments the error "Element is not clickable at point (39, 109). Other element would receive the click:" is displayed to me, even the button has been clicked:

global.waitForElementToBeLoaded = function(locator, identifier){
    var elementToFind = locator(identifier);
    browser.wait(function(){
        return        browser.driver.isElementPresent(elementToFind).then(function(found){
        return found;
});
}, 60000, "The test execution timed out waiting for " + elementToFind);
     expect(browser.driver.isElementPresent(elementToFind)).toBeTruthy();
     return browser.element(locator(identifier));
};

Upvotes: 1

Views: 51

Answers (2)

nilesh
nilesh

Reputation: 14289

I'm confused with your code snippet. Shouldn't wait timeout and msg be inside browser.wait

browser.wait(function(){
        return  browser.driver.isElementPresent(elementToFind).then(function(found){
           return found;
              }, 60000, "The test execution timed out waiting for " + identifier);

Also sometimes just isElementPresent is not enough, you may need to couple it up with isDisplayed(), like below. Unfortunately there is no once size fits all condition for waits, sometimes you may want to wait for visibility of element, sometimes invisibility, click-ability etc. selenium-webdriver provides many useful methods as part of until object you can leverage them on case by case basis.

browser.wait(function(){
            return  browser.driver.isElementPresent(elementToFind).then(function(found){
               if(found) {
                 return browser.driver.isDisplayed(elementToFind).then(function(displayed) {
                     return displayed;   
                  }
               } 
                return found;
    }, 60000, "The test execution timed out waiting for " + identifier);

Upvotes: 1

BarretV
BarretV

Reputation: 1197

I've found this to be useful in those kind of situations where I need something to happen after the page loads.

browser.wait(protractor.until.titleIs("Some Page"), 10000, "✗ Failed to wait for the page to load").then(function(){ // DO Stuff });

Upvotes: 0

Related Questions