6

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?

map7
  • 5,096
  • 6
  • 65
  • 128
  • Do you know about `onCleanUp` optional function in [protractor config](https://github.com/angular/protractor/blob/master/docs/referenceConf.js#L195)? – glepretre Oct 20 '14 at 16:01
  • No I didn't know about this onCleanUp option, could I call a rake task in the onCleanUp? – map7 Oct 21 '14 at 21:59
  • I'm not sure about that, it's automatically called at the end of all your tests, that's all I know. – glepretre Oct 22 '14 at 06:56

1 Answers1

1

How do I cleanup these tests nicely?

Seems like your definition of cleanup is that you start over i.e. start with with fresh listing element and that not dialog is open. Since you delete the last element your every test starts with empty listing.

i.e. you want to start over again.

Following hack can help and ensure they are very clean but may slow down your tests.

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

If this is "too much cleaning" for you then your afterEach option is actually not bad however you end up testing your 'delete' use case the way you have coded.

//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()); 

About the open dialog.

It seems weird that element(By.id("create_btn")).click() is not closing the dialog but what do I know of the use case.

For removing the dialog you can do follow similar DOM manipulation techniques and just remove that DOM otherwise you end up testing the other use case also.

bhantol
  • 9,368
  • 7
  • 44
  • 81
  • 1
    By cleanup I do mean start again with my seeded data and not the new items I've entered. It would be good to return to a snapshot of the database in time quickly. – map7 Dec 08 '14 at 10:49
  • 1
    With SPAs the Architecture is REST & that the backend maintains the state by virtue of using database. It moves to a new state. So this is the backend problem domain. So if possible you may want to avoid having to rely on the backend state. All these problems become moot points when we focus on testing the "UI" code instead of what comes from backend. So instead of testing/verifying "I must have 5 listings on the page" you may want to test/verify that "multiple listings are displayed on the page". This takes away the database/backend testing which is a separate area of testing REST services. – bhantol Dec 08 '14 at 17:42