Skip to content Skip to sidebar Skip to footer

Want To Remove Browser.sleep()

Hi all I am working on a protractor to test a nonangular website. Initially while testing I had browser.sleep() so that page gets load completely. I know that adding browser.sleep

Solution 1:

Rather than waiting for the page itself, wait for an element on the page.

The trick is to wait first for the element to be present, then wait for it to be displayed. Just calling "isDisplayed" causes errors if you do not wait for "isPresent" first. Here is a good function to use.

functionwaitForElement(el, waitTimeoutMilliseconds){
    return browser.wait(function() { return el.isPresent(); }, waitTimeoutMilliseconds)
    .then(function(){
       return browser.wait(function() { return el.isDisplayed(); }, waitTimeoutMilliseconds);
    });
}

Instead of

browser.sleep(5000);
var elmOK = browser.driver.findElement(by.xpath('//*[@id="lnav"]/li[3]/a'));
yield elmOK.click();

do

var elmOk = element(by.xpath('//*[@id="lnav"]/li[3]/a'));
waitForElement(elmOk, 5000);
elmOk.click();

Solution 2:

You can use protractor expected conditions.

varEC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be clickable.
browser.wait(EC.elementToBeClickable($('#abc')), 5000);

like

var webElement = browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[1]/select/option[2]'));
var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be clickable.
browser.wait(EC.elementToBeClickable(webElement )), 5000, 'element is not clickable with 5 seconds');

so that you don't need to put explicit waits. If element is clickable then browser immediately clicks on it, otherwise it waits for 5 seconds before it timeouts. you can increase that wait time as well.

Post a Comment for "Want To Remove Browser.sleep()"