Here's my issue, steb by step :)) I thought it reads better this way, unlike a wall of text pitifully trying to explain my very domain-specific problem.
1) We have a Angular.js
app with PHP
back-end backed with MongoDB
storage.
2) Protractor
for End-to-end tests.
3) Need to test pages which alter the database, i.e. registration scenario - I'm going through all registration steps in a test, so the db gets a new user record.
4) Predictably, test will fail after it was run, since the db has a record for the test user and no registration is needed - user is redirected to homepage instead.
I was thinking to get a mongodb
package for node.js
, and interact with a DB in tests.
But it just doesn't seem right: config files for DB connection are in a php
files on the backend, while I'm trying to write tests for purely front-end part of our app.
Any ideas?
There is an easy way to do it. If you have an angular service in your app that talks with your back-end you can call it from protractor.
Here is an example:
https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js
function createObject(data) {
return browser.executeAsyncScript(function(data, callback) {
var api = angular.injector(['ProtractorMeetupApp']).get('apiService');
api.member.save(data, function(newItem) {
callback(newItem._id);
});
}, data);
}
This code will be serialized and it will be executed on the browser. I have a service called apiService in the ProtractorMeetupApp module. The api service can create, update, etc.
Your test would look like this:
https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/member3-spec.js
it('should call api', function() {
// Create a new member.
createObject({name: 'test member'}).then(function(id) {
console.log(id)
});
});
I would mock out the interactions with your PHP app. This will allow you to isolate your tests to the Angular code and test for edge cases in your data (or issues on the server side) more explicitly. http://docs.angularjs.org/api/ngMockE2E.$httpBackend
Here is another answer that might be helpful for you: mock $httpBackend in angular e2e tests