2016-12-14 69 views
2

我的同事正在與角料和他使用的mdToast這樣:(在else語句)如何使用量角器測試mdToast的渲染文本?

$scope.login =() => { 
    $scope.loading = true; 
    authService.login($scope.username, $scope.password, (result) => { 
     if (result) { 
      if (result === true) { 
       $location.path('/'); 
      } else { 
       $mdToast.show($mdToast.simple().textContent($filter('capitalize')($filter('translate')('passwordNotValid'))).position('top right').hideDelay(3000)); 
       $scope.loading = false; 
       $("#input_username_login").focus(); 
      } 
     } else { 
      //error callback from server 
      $mdToast.show($mdToast.simple().textContent($filter('capitalize')($filter('translate')('passwordNotValid'))).position('top right').hideDelay(3000)); 
      $scope.loading = false; 
      $("#input_username_login").focus(); 
     } 
    }); 
}; 

我需要測試結果敬酒的文本,但它似乎像角材質的mdToast用途NG-IF。我的同事也沒有對敬酒任何HTML代碼(他只是使用控制器上面產生的呢)即便如此,當面包被觸發出現在DOM幾秒鐘下一個代碼:

screenshot of the generated md-toast

除了別的以外,我試過用browser.wait,waitForAngular等減慢敬酒的消失,但沒有奏效。我堅持用:

it("should show error toast in English when user fails login", function(){   
    login.email_address_field.sendKeys('[email protected]'); 
    login.password_field.sendKeys('hardest_pass_123'); 
    login.login_button.click(); 
    expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toBe('Incorrect username and/or password'); 
}); 

回答

1

我找到了一個解決方案,以這個answer爲例。我的規格是:

it("should show error toast in English when user fails login", function(){   
    login.email_address_field.sendKeys('[email protected]'); 
    login.password_field.sendKeys('hardest_pass_123'); 
    login.login_button.click(); 
    browser.ignoreSynchronization = true; 
    browser.sleep(1000); 
    expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toBe('Incorrect username and/or password'); 
    browser.ignoreSynchronization = false; 
}); 
1

您可以使用ExpectedConditions使你的腳本等待到烤麪包機消息是displayed.Once烤麪包機顯示,那麼你可以驗證消息也是如此。

var EC = protractor.ExpectedConditions; 
browser.wait(EC.visibilityOf(element(by.tagName('md-toast'))),5000); 
expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toEqual('Incorrect username and/or password'); 
+0

謝謝,但我試過了,由於超時不工作,我也調查了[這裏](https://github.com/angular/protractor/issues/169)和[這裏](https ://github.com/angular/material/issues/2704),似乎是一個尚未解決的問題(_我的意思是沒有解決方法,等等_¬¬) –

0

該規範適用於我,它不使用睡眠。這裏需要注意的重要一點是,瀏覽器在等待時必須設置browser.ignoreSynchronization標誌。由於browser.wait的異步性質,必須在browser.wait許諾解決後才能更改ignoreSynchronization,否則它不起作用。

// a close button that appears on the md-toast template 
const closeToastButton = $('[data-automation="toast-close"]') 
const cond = protractor.ExpectedConditions 
function waitToastShow() { 
    return browser.wait(cond.elementToBeClickable(closeToastButton), 5000) 
} 
function waitToastHide() { 
    return browser.wait(cond.invisibilityOf(closeToastButton), 5000) 
} 

screenshot = name => browser.takeScreenshot().then(/* save fn */) 

describe('a suite ... ',() => { 
    it('takes screenshots of an md-toast once shown and after hidden', function() { 
     // ... actions that launch an md-toast using $mdToast.show({ ... }) 
     browser.ignoreSynchronization = true 
     waitToastShow().then(() => { 
      screenshot('toast-showing.png') 
      waitToastHide().then(() => { 
       screenshot('toast-hidden.png') 
       browser.ignoreSynchronization = false; 
      }) 
     }) 
    }); 
}