Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cleaning up after Protractor tests

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?

like image 698
map7 Avatar asked Oct 14 '14 23:10

map7


1 Answers

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.

like image 52
bhantol Avatar answered Sep 27 '22 22:09

bhantol