Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor E2E - How Do You Manage Database?

I'm currently leaning on the Node + Angular stack and utilising Karma and Protractor for testing.

I'm currently having a hard time figuring out how to handle E2E tests that create and edit data, as well the need to have a expected data loaded.

Searching google turns up lots of various custom methods. I often read 'you should set-up your data' or 'just create a mock' without going into a bit more detail on a common process. Others put in too much overhead in creating a whole new mock module from scratch.

I would just to know how are people currently doing it, and is there a standard for this? Or do people tend to just mock the back-end? Mocking the back-end doesn't seem to be simple either like in Karma since your in the browser scope.

I am as expected using MongoDB, so would be nice to get some direction on what other doing in this scenario. Particularly automating loading of fixtures and database clean-up through Protractor would be good.

like image 516
iQ. Avatar asked Aug 12 '14 23:08

iQ.


People also ask

What is E2E testing protractor?

Protractor is an open-source testing framework you can use to perform end-to-end testing on Angular apps. This framework acts like a combination of different solutions. It can integrate several pieces of technology, such as NodeJS, Jasmine, Selenium, Mocha, and many more.

How do you write a test case using a protractor?

Files required by ProtractorThe test code is written by using the syntax of our testing framework. For example, if we are using Jasmine framework, then the test code will be written by using the syntax of Jasmine. This file will contain all the functional flows and assertions of the test.


1 Answers

There are three ways you can achieve this:

1) The first way is to write easy custom APIs for testing that you can use before your protractor tests starts (protractor config file onPrepare). On the onPrepare you can run a script that creates data in your database that you will later use for testing purposes. That script should contain the logic to push the database entries you require to the database. You can also make this script to run on CI before your protractor tests CI phase starts. This way you will have all the database entries you require loaded onto the database before the tests start. The cons of this is that your tests will take a bit longer to start. Another con would be that if there is setup in between tests, you'd have to run the setup between tests.

2) Second way of doing this would be by using the npm package ng-apimock. I would recommend against using this because mocking is not a good idea for e2e tests. Even so, using ng-apimock will allow you to define API values that you want to mock and push them to a mock server. Then you can use that value later on in your tests using selectScenario. There is a protractor ngapimock plugin that you can use to achieve this. Also a functionality called pass through will allow your API response to not use the mock but use the actual API backend. This tool is used for faster frontend development in case backend is not ready yet. If you are using ngapimock, make sure you maintain your mocks periodically else the tests will fail.

3) Third way of doing this would be by initialising a database that is going to be used only for testing. This means that when you initialise your database, just make sure that only the data you need for testing is present. Run the tests and then destroy the database. This way you will need to maintain your database startup script to add different values into different tables.

Hope this helps.

like image 127
Sankalan Parajuli Avatar answered Oct 27 '22 21:10

Sankalan Parajuli