等待完整的页面加载在量角器中无法按预期工作

等待完整的页面加载在量角器中无法按预期工作

问题描述:

我正在编写一个登录非角度应用程序的测试.

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;
                });
            });
        });
    });
});

登录后,我想查看currentUrl.但是点击登录按钮后,它会等到仪表板网址出现.但是当登录按钮点击后,它转到不同的网址,然后它也等待仪表板网址无限.我想在登录事件后检查当前 url,如果它不是仪表板 url,那么它应该失败,并且不运行下一个测试套件案例,因为登录测试失败.

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.

喜欢-

当点击登录按钮时.

  1. 等待页面加载完成.

2.然后检查当前的url,如果不是dashboard url,那么测试应该失败,并且不进行任何测试用例.

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

为了知道页面是否加载而等待页面 url 加载不是最佳做法,因为可能存在重定向或其他事情.

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.

最好等待下一页的特定元素(登录后).以下是重构为使用自定义 wait() 函数的代码,该函数将在继续检索当前 url 之前等待元素出现:

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;
      });
    }
});

希望这会有所帮助!