Cleaning after Protractor tests

I am using Rails + AngularJS and have switched to using Protractor for all my end tests. I installed it using the protractor-rails gem , which helps me use the test database instead of the development database for my tests.

The problem is that after running the test, for example: 'create_client_spec.js.coffee', I remain with a new client in my table that was not cleared 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 to clear these tests?

One of my ideas was to add an afterEach method to remove a new client after each test in this file.

Update:

I 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 helper.delete_client () in my afterEach block before logging out. It works, but is there a better way?

+5
source share
1 answer

How to clear these tests?

It looks like your definition of cleanup is that you start by starting with a fresh listing element, and this dialog does not open. Since you delete last item, each of your tests starts with an empty list.

i.e. you want to start over.

After hacking, it can help and make sure that they are very clean, but can slow down your tests.

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

If this is β€œtoo much cleanup” for you, then your afterEach option is actually not bad, but you end up checking your use case for β€œdelete” as you encoded.

 //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 dialog box that opens.

It seems strange that element(By.id("create_btn")).click() does not close the dialog, but what I know about the use case.

To remove a dialog box, you can follow similar DOM manipulation methods and simply delete that DOM, otherwise you will also test another use case.

+1
source

Source: https://habr.com/ru/post/1204684/


All Articles