3

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 is not a good way of testing and hence want to remove it from the code.

I tried browser.wait but I get an error and when I add broswer.manage.timeouts.implicitwait() the wait doesn't happen. I'm stuck on this issue for a long time please help me out :(

  var co = require('co');
    var path = require('path');

    describe("Portal: Partner Admin ", function () {
        beforeEach(function () {
            browser.ignoreSynchronization = true;

        });

        it("test", co.wrap(function* () {

            yield browser.get(browser.params.baseUrl);

            var elmOK = browser.driver.findElement(by.css('a[href="#login"]'));
            yield elmOK.click();
            expect(browser.getCurrentUrl()).toContain("login");
            yield browser.switchTo().frame('here-account-sdk').then(function () {
                browser.driver.findElement(by.id('sign-in-email')).sendKeys("Userid");
                browser.driver.findElement(by.id('sign-in-password-encrypted')).sendKeys("password");
                browser.driver.findElement(by.xpath(' //*[@id="sign-in-form"]/div[2]/div[6]/form/fieldset[3]/button')).click();

            });
            browser.sleep(5000);
            var elmOK = browser.driver.findElement(by.xpath('//*[@id="lnav"]/li[3]/a'));
            yield elmOK.click();
            browser.sleep(1500);
            browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[1]/select/option[2]')).click();
            browser.sleep(5000);

            browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[2]/table/tbody/tr[1]/td[10]/span')).click();
            browser.sleep(5000);

            browser.driver.findElement(by.xpath('//*[@id="content"]/div/div[2]/div/div/div/div[3]/button[1]')).click();//Delete the file
            browser.sleep(5000);
        }));
Jatin
  • 660
  • 3
  • 8
  • 28
  • 1
    Typically selenium is instructed to wait until something is visible in the page or when the document readyState is complete. Check http://stackoverflow.com/questions/5868439/wait-for-page-load-in-selenium – apokryfos Nov 14 '16 at 07:25
  • Hi, I am using protractor for testing – Jatin Nov 14 '16 at 07:28
  • 1
    @Jatin Protractor uses Selenium... – Akshat Mahajan Nov 14 '16 at 07:30
  • 1
    Maybe you shouldn't use protractor when testing a non-angular website. It's like using a knife to eat soup. Also, protractor is backed by selenium which you can still use. – apokryfos Nov 14 '16 at 07:31
  • Agreed but we have some requirement and have to go through it :(... My code runs fine when I use sleep but i have to remove the sleep statements :( – Jatin Nov 14 '16 at 08:11

2 Answers2

3

You can use protractor expected conditions.

var EC = 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.

3

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.

function waitForElement(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();
mvndaai
  • 3,453
  • 3
  • 30
  • 34