2

我正在使用量角器茉莉花进行角度单页应用程序的 e2e 测试。考虑以下代码片段。

describe('Search', function(){
  it('Should Open the search modal popup', function() {
     //code
     expect(modalOpened).toBe(true)
  })

  it('Search should return results', function(){

  })
  it('Search should not return results', function() {

  })
})

在上面的例子中,如果失败,那么下面的Should Open the search modal popup规范也会失败,因为模态本身没有打开。因此,运行以下规格毫无意义。我可以有条件地运行最后两个规范吗?就像只有当第一个规范通过时,下面的规范才应该运行。Should Open the search modal popupShould Open the search modal popup

4

1 回答 1

1

看起来您正在混淆单元测试和 E2E 测试。Protractor 是 Selenium 的一个子集,仅用于运行您的集成或端到端测试。每个 E2E 规范都应该将您的代码单元集成在一起(因此,集成测试),并测试您选择的浏览器是否允许每个功能发生(例如单击搜索和带有结果的模式显示)。此外,您不会检查 modalOpened 的属性是否为真,您会检查以确保模态在 css 中,通过 className 或其他,以及您的结果。

幸运的是,Protractor 提供了基于 Promise 的异步事件。当您单击搜索图标时,您可以执行以下操作:

it('Should Open the search modal popup and show results', function() {

    element(by.id('search')).click().then(
        function() {
             // now check for modal to be displayed

             // now check results are displayed
        }
    );
});

资料来源:

https://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.click

https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.then

于 2016-04-27T07:46:25.177 回答