我正在使用 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() 。它有效,但有更好的方法吗?