wait for complete page load does not work as expec

2019-08-22 04:36发布

问题:

I am writing a test for login into non-angular application.

describe('login Test for App', function () {

    beforeEach(function () {
        browser.ignoreSynchronization=true;
        browser.get(loginPageURL,2000);
    });


    it('It should Login a User', function () {
        element(by.id('username')).sendKeys(constants.userName);
        element(by.id('password')).sendKeys(constants.password);
        element(by.id('Login')).click().then(function () {
            // Waiting to open modal
            browser.wait(function () {
                return browser.getCurrentUrl().then(function (url) {
                    return url==dashboardUrl;
                });
            });
        });
    });
});

After login, I want to check currentUrl. But After login button click, It waits till dashboard url appeared. But when after loginbutton click, It go to diffrent url, Then it also wait for dashboard url infinite. I want to check current url after login event, if it is not dashboard url, then it should fail, and do not run next test suite cases, because login Test is failed.

Like-

When clickOn login button.

  1. Wait for complete page load.

2.Then check current url, If it is not dashboard url, then test should failed, and not proceed for any test cases.

回答1:

It is not best practice to wait for the pages url to load in order to know if the page is loaded, because there could be redirects or other things.

It is best practice to wait for a specific element on the next page(after log-in). Here is your code refactored to use a custom wait() function that will wait for an element to appear before continuing to retrieve the current url:

    describe('login Test for App', function () {
    browser.ignoreSynchronization = true;

    it('should load the log-in page', function(done) {
        browser.driver.get(loginPageURL).then(function() {
            browser.driver.sleep(2000);
            wait('#username', 10000);
            done();
        });
    });

    it('It should Login a User', function (done) {
        element(by.id('username')).sendKeys(constants.userName);
        element(by.id('password')).sendKeys(constants.password);
        element(by.id('Login')).click().then(function () {
            // Waiting to open modal
            wait('#element_on_the_next_page', 10000);

            browser.getCurrentUrl().then(function (url) {
              // do anything with the url
              done();
            });
        });
    });

    function wait(selector, timeout) {
      browser.driver.wait(function() {
        return browser.driver.isElementPresent(by.css(selector)).then(function(present) {
          return present;
        });
      }, timeout);
      browser.driver.wait(function() {
        return browser.driver.findElement(by.css(selector)).isDisplayed().then(function(displayed) {
          return displayed;
        });
      }, timeout).then(function() {
        return;
      });
    }
});

Hope this helps!



标签: protractor