I'm using Rails + AngularJS and have switched to using Protractor for all my end to end tests. I've set it up using the protractor-rails gem which helps me use the test database instead of development database for my tests.
The problem is after I run a test eg: 'create_client_spec.js.coffee' then I'm left with a new client in my table which is not cleaned up after my test.
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")
)
How do I cleanup these tests nicely?
One idea I had was to add a method in afterEach to remove the new client after each test in this file.
Update:
I've put the following in my 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()
Then I call the helper.delete_client() in my afterEach block before logging out. It works, but is there a better way?