Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Protractor e2e Testing

I am writing an end-to-end test using Protractor for my Angular application. I can mock httpBackend for unit test but I want to actually call the server and get the JSON response back and write tests again the data returned.
I have read a lot on stackoverflow but can not understand how this is done.

Do I use $http? How do I inject it into my Jasmine tests? How do I get the response JSON back into my Jasmine test?

any help or links to resources with instructions on doing this will be helpful.

Again I do NOT want to mock to server, I want to hit the server and get the JSON back.

Thanks!

like image 491
user2898797 Avatar asked Oct 19 '13 21:10

user2898797


People also ask

What is Protractor E2E testing?

Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, using Selenium. We can say in other words that Protractor is a tool that helps us to test Angular Apps using Selenium.

Is Protractor going to be deprecated?

Protractor Will Be Deprecated Some of the reasons behind these deprecation plans are: State of Protractor. Protractor is dependent on selenium-webdriver , and is not able to upgrade to the new version without introducing a huge breaking change, and forcing users to do a migration for all their tests.

Which is better Protractor or cypress?

Cypress is the easier and more reliable tool, whereas Protractor is the more powerful tool. Your choice of tool should depend on your specific testing needs.


2 Answers

I'm working through this myself at the moment. The short answer I think is that you set up your application exactly the same as if you were manually testing it yourself - so Protractor is really just a robot user, it has no (well, almost no) access to the internals of your application.

So, if your application needs a web server (and most do), then you start up that web server, then have protractor connect to your application via the browser and exercise it.

For my case, I'm aiming to use grunt to call a task that does basic database setup before it starts running my protractor e2e tests - this should give me a known database state.

For an example of this, I've been writing a tutorial for using Rails 4 with AngularJS, the section on using protractor for e2e testing is not rails-specific and might be useful: http://technpol.wordpress.com/2013/11/16/5-end-to-end-testing/

like image 188
PaulL Avatar answered Oct 21 '22 19:10

PaulL


Protractor should be used for end-to-end testing of your full stack.

In this scenario the test typically exercises the angular application (filling form, pressing buttons) which will trigger the angular application to call to the REST server, which returns data, which your Angular application transforms in DOM changes, which then your end-to-end test asserts on.

This means that you probably want to start your application server (which hosts the angular application and is the REST backend, I suppose) before starting Protractor

How to do that is out of scope for Protractor.

The difficulty in this is typically how to setup your database, so that the e2e test knows what to expect as returns to your JSON services.

like image 24
user2976698 Avatar answered Oct 21 '22 19:10

user2976698