簡體   English   中英

量角器無需等待網頁更改

[英]Protractor no waiting for web page to change

將Angular 5與量角器配合使用。

在等待瀏覽器URL更改以查看用戶是否登錄然后登陸內部儀表板頁面時遇到麻煩。

規格:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    browser.wait(5000);
    const currentUrl = browser.getCurrentUrl();
    expect(currentUrl).toEqual('confidentialUrl/dashboard');
  });

執行“量角器conf.js”時出現錯誤:

失敗:等待條件必須是類似promise的對象,函數或Condition對象

browser.wait需要一個對象作為第一個參數,它是條件對象或類似Promise的對象。 例如,您可以從protractor創建預期條件

// Expected condition object
const EC = protractor.ExpectedConditions;
// wait 5 seconds for expected condition
// in this case it will wait for url contains a given value
browser.wait(EC.urlContains('foo'), 5000).then(() => {
   // then you can assert here
   const currentUrl = browser.getCurrentUrl();
   return expect(currentUrl).toEqual('confidentialUrl/dashboard');
});

我們通過使用ExpectedConditions解決了此類問題。

我編輯了您的代碼,因此在您的情況下:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    const EC = protractor.ExpectedConditions;
    browser.wait(EC.urlContains('confidentialUrl/dashboard'), 5000).then(() => {
      const currentUrl = browser.getCurrentUrl();
      return expect(currentUrl).toEqual('confidentialUrl/dashboard');
    })

  }); 

或者您可以:

fdescribe('Suite 1', function() {
      it('should login and land on the dashboard page', function() {
        // Arrange
        browser.ignoreSynchronization = true;

        const baseUrl = 'confidentialUrl';
        const email = 'email';
        const password = 'password';

        // Act
        browser.get(baseUrl);
        element(by.id('username')).sendKeys(email);
        element(by.id('password')).sendKeys(password);
        element(by.css('.submit-btn')).click();

        // Assert
        return browser.driver.wait(function() {
          return browser.driver.getCurrentUrl().then(function(url) {
            return expect(url).toEqual('confidentialUrl/dashboard');
          });
        }, 10000);

      }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM