我正在使用 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 block 中调用 helper.delete_client()。它有效,但有更好的方法吗?
最佳答案
How do I cleanup these tests nicely?
似乎您对 cleanup
的定义是您重新开始,即从新的 listing
元素开始,而不是打开对话框。由于您删除
最后一个元素,因此您的每个测试都以空列表开始。
即你想重新开始。
以下 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,否则您最终也会测试其他用例。
关于angularjs - Protractor 测试后清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26372158/