Testing a websockets server with protractor - angularjs

I have a custom WebSockets server that triggers route changes in a AngularJS client. I would like to carry out e2e integration tests using protractor. As the route change events are every 30 seconds and involve multiuser interactions, it seems like creating a mock of the service is the best approach. Is there a recommended way of doing this in protractor?

I am not sure if this is exactly what you are looking for, but you can define mock services in Protractor and then upload them to the browser using Protractors browser.addMockModule function. The uploaded module will replace the real one that the application would normally use. See the following blog post for more details.
http://eitanp461.blogspot.com/2014/01/advanced-protractor-features.html

I recently had a similar problem and solved it using addMockModule as stated above. I wrote a post about it describing my solution.
http://www.functionalimperative.com/2015/04/22/protractor-socket-mocks.html
I found organizing my code a certain way helped when mocking the socket modules.

Related

What can I use for mock fetch-requests while e2e testing in protractor similar to addMockModule without Angular

I am writing e2e test with protractor for a typescript module. I want to use protractor for the additional features in comparison to selenium webdriver. I am using typescript standalone so I am building a non-angular application.
I searched a lot for mocking services or fetchmock etc. But I think I am searching at the wrong place. Maybe I just didn't understood what do I really need for my problem.
At the moment I have an application where I want to test the frontend. My problem is, that the application only works with a server which is setting up many data and make it available for rest. Without this server my javascript file wont work and I wont start the server for my tests, it must be server independent.
For example: my server provide the data on a specific address 192.168.1.230 and I can fetch the data with fetch api over: 192.168.1.230/users/1.
In unit tests I have mocked my fetches, but in e2e tests I need to mock the address (maybe with selenium webdriver) and want to get a dummy response if my api is fetching this data.
How can I realise that? I use protractor with npm (nodejs) and could need a npm plugin for this.
I saw another post here with the addMockModule, but this is only for angular modules and I don't use angular.

Protractor browser.driver.getCurrentUrl vs browser.getCurrentUrl

I'm running an Angular app and I'm trying to get the current URL when testing on protractor. Which one to use?
browser.driver.getCurrentUrl() or browser.getCurrentUrl() ?
If this is an Angular application under test - use browser, otherwise - browser.driver.
To quote #jmr from a relevant github issue:
If you need to interact with a non-Angular page, you may access the wrapped webdriver instance directly with browser.driver.
Though, note that both are gonna work if this is Angular application under test. Some people even said that found browser.driver more reliable if it the sync time is longer than usual:
Just to add to this - I find that browser.driver works better on AngularJS apps that take time to sync. I have tried both and for some reason browser.driver is more reliable.
Though, I've personally used browser.getCurrentUrl() and cannot recall any problems.

How to performance test an AngularJS Web Application?

I used to do performance testing on websites mostly with JMeter scripts.
However, more and more projects are build with frontend MVC's, such as AngularJS and a current project is loading all of it's content via angular view files, REST calls etc.
Unfortunately, JMeter doesn't execute any javascript thus my load test return me the homepage in just (400ms).
In real, it actually takes several seconds to load in a browser. When I check the response data, it does not contain any data yet due to Angular.
Instead of investigating the network traffic and individually loading each component (e.g. profile.html, notification.html, REST calls etc. ). Is there a product on the market or some best case I could follow that is similar to executing JMeter scripts, but considering javascript execution and loading of external resources due to javascript?
(I am not planning to profile javascript execution times. This is still to test if the infrastructure behind is capable serving xyz simultaneous users)
Although JMeter isn't capable of executing client-side JavaScript it can record relevant requests via HTTP(S) Test Script Recorder. Once recorded you should be able to combine all the standalone requests into one "aggregate" using JMeter's Transaction Controller
If this easy approach for some reasons doesn't play for you check out How to Load Test AJAX/XHR Enabled Sites With JMeter for more options and clues.
I use Chrome dev tools to do this kind of performance tests in web apps.
I suggest you to read the Chrome Profiling docs (https://developer.chrome.com/devtools/docs/javascript-memory-profiling). All the section of Performance and Profiling in goolge documentation is really good!
You can try to use the option 'Use as Monitor' for the requests you fire up from your test.
http://jmeter.apache.org/usermanual/build-monitor-test-plan.html
They are performance killers, though. Another option is to use the listener 'Save Responses to a File' to see if the end HTML is delivered. It should not give you the ideal result but it might help.
If you want to track down performance of XHRs for a single user, you can try to play with Selenium and BrowserMob Proxy, but it is not under the stress testing, but functional testing.
You can try https://github.com/kidk/felt it is build for this specific purpose.
It uses PhantomJS/SlimerJS to generate load to a website, so you get all the API/JS/CSS and image calls you would get like in a normal browser. It is still a young project, but it might be the solution you are looking for.
(This is my personal project)

What's the best way to test an application built in Sinatra and Backbone.js?

I'm new to Sinatra and Backbone. I've done testing for Rails using Rspec and Cucumber. I'm wondering the smartest way to approach testing an application built in Sinatra and Backbone.js.
I was thinking of using Rspec to test some of the logic in Sinatra and then using Jasmine to test the Backbone portion. Then I was also going to use Cucumber to do integration testing.
Is this overkill? Could one cancel the other out and render it not useful? Could Jasmine replace Rspec altogether?
I should also mention that the Sinatra app communicates with an API exposed by a separate Rails app. Maybe there is a smart way to mock/model that data to run tests on as well.
Thanks!
I'm in a similar position, in that I use Sinatra mainly for writing data API's and now write web apps using Backbone (or whatever). Since you have a data API, I'd test the API using RSpec and Rack Test, which will probably the nicer part of your day. The rest of the week you could use any of the javascript testing frameworks (Jasmine, Sinon etc…) to test the Backbone portion, and hope to get back to using Ruby as soon as possible! ;) It's up to you whether you mock the calls to the data API, or just run them against it, I think the main factor in making that choice would be speed.
I don't think it's overkill to use both, since you have a separate API it should have it's own specs, as other clients may start connecting to it too. It's also a hell of a lot easier to spec an HTTP call than whatever else the Backbone app is doing + an HTTP call.
I don't use Cucumber personally, it seems superfluous if you have the other specs, but YMMV.

extending angular e2e testing

I have been using the Angular Scenario Runner for performing End To End tests on our codebase.
I am following:
http://docs.angularjs.org/guide/dev_guide.e2e-testing
Compared to frameworks such as capybara etc, the supplied matchers seem quite limited.
What options are available to help in End To End testing Angular Applications?
The next tool for AngularJS will be Protractor
you can use Selenium, of course.
One good thing that E2E does is to record all outgoing requests, so that it continues only after all of them are back with responses. This way you dont need to set a timer to check when your page is fully loaded. Not sure if Selenium supports that.

Resources