6

我正在使用 Rails + AngularJS,并已切换到使用 Protractor 进行所有端到端测试。我已经使用protractor-rails gem设置它,它可以帮助我使用测试数据库而不是开发数据库进行测试。

问题是在我运行测试后,例如:'create_client_spec.js.coffee' 然后我的表中留下了一个新客户端,在我的测试后没有清理。

helper = require('../../helper.js.coffee')

describe('create a new client', ->

  beforeEach ->
    helper.login()

  afterEach ->
    helper.logout()

  it 'shows the client after creation', ->
    browser.get('/api#/clients')
    element(By.id("new_btn")).click()

    element(By.id("name")).sendKeys("John Smith")
    element(By.id("create_btn")).click()

    expect(element(By.id("heading")).getText()).toContain("John Smith")

)

我如何很好地清理这些测试?

我的一个想法是在 afterEach 中添加一个方法,以在此文件中的每次测试后删除新客户端。

更新:

我在 helper.js.coffee 中加入了以下内容

  delete_client: ->
    last=element.all(By.id("listing")).last()
    last.element(By.id("delete")).click()
    this.accept_dialog()

  accept_dialog: ->
    # Accept the dialog which is displayed
    ptor = protractor.getInstance()
    alertDialog = ptor.switchTo().alert()
    alertDialog.accept()

然后我在注销之前在我的 afterEach 块中调用 helper.delete_client() 。它有效,但有更好的方法吗?

4

1 回答 1

1

我如何很好地清理这些测试?

似乎您的定义cleanup是重新开始,即从新listing元素开始,而不是打开对话框。由于您delete是最后一个元素,因此您的每个测试都以空列表开始。

即你想重新开始。

遵循 hack 可以帮助并确保它们非常干净,但可能会减慢您的测试速度。

browser.get('<your root URL>');

如果这对您来说“清洁太多”,那么您的afterEach选择实际上还不错,但是您最终会以您编码的方式测试您的“删除”用例。

//note I have not run this snipped this so it is not syntax free
listing=element.all(By.id("listing"))
listing.innerHTML = '';//or whatever should be the default
OR
listing.removeChild(listing.last()); 

关于打开的对话框。

没有关闭对话框似乎很奇怪,element(By.id("create_btn")).click()但我对用例了解多少。

要删除对话框,您可以遵循类似的 DOM 操作技术,只需删除该 DOM,否则您最终也会测试其他用例。

于 2014-12-04T17:20:25.360 回答