量角器测试后清理

map*_*ap7 6 testing end-to-end jasmine angularjs protractor

我正在使用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")

)
Run Code Online (Sandbox Code Playgroud)

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

我的一个想法是在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()
Run Code Online (Sandbox Code Playgroud)

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

bha*_*tol 1

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

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

即你想重新开始。

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

browser.get('<your root URL>');
Run Code Online (Sandbox Code Playgroud)

如果这对您来说“太多的清理”,那么您的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()); 
Run Code Online (Sandbox Code Playgroud)

关于打开的对话框。

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

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