Need to access my angular service in protractor test cases - angularjs

I am writing e2e test cases using protractor for my angular application. I have a service in my application called UserService which holds user information. I have an API getUser in this service to get the user data. I need to access UserService in my protractor test cases.
I looked at some of the articles and found that I could use browser.executeAsyncScript to access my services. But If I use this method , I am gettiing the injection issue.
In my application, the module is defined as:
var angApp = angular.module('myApp', ['ngCookies','ngResource']);
and all directives, services and controllers are created using angApp module.
Now I need to access my UserService so that I can call getUser API in my protractor testcases.
Could someone help me in accessing userService in my protractor testcases?

The problem that you are facing is that protractor tests are run in a separate process from the browser. Any script that runs in the browser is not directly accessible to your protractor tests. There is no way to directly transfer data between the two (the best you can do is interact through the DOM).
I'm guessing that your UserService is some kind of CRUD API for manipulating Users on the server. My recommendation would be to make REST requests directly from the protractor tests to the server in order to get the users. It will not be easy to share code between the tests and the app.

Related

Playwright e2e testing. What to do with oauth2 or email-passwordless authentication

I am starting to do e2e tests with playwright.
Most of the application requires to be authenticated. Automating this with a username/password mechanism is quite easy. The problem is that the app I want to test has only two authentication mechanisms: Github OAuth and email authentication links. Specifically I am using next-auth in a NextJS project.
I don't know how should I e2e test this with Playwright, the options I have considered are:
Completly mocking the authentication using a mock user and a mock session and then append the session token in the test context (as it is described here in the official docs).
Creating a Github user and/or an email account for the tests and somehow truly use them in playwrigth.
The first option is much easier to implement but then it's not e2e testing anymore. The second option looks difficult to implement and I don't know if it is recommended. I don't know if there are smarter or more standarized ways to proceed with this.
I disagree with #mostafa as an e2e test should be as closely to production as possible. Even if you're using a well tested library, you could still use it wrongly. Cypress has the mentioned issue recognized and developed a solution with a new feature: https://cypress.io/blog/2022/04/25/cypress-9-6-0-easily-test-multi-domain-workflows-with-cy-origin/
It's especially useful for login workflows provided by external services.
The keynote here is that you don't need to write e2e test to test the authentication flow because you're using next-auth ( which I think is a well-tested library).
So I think you should go for the first option .

Acceptance Test with Identity Server and Specflow .Net

I Need to test with specflow an api endpoint that has been protected with identinty server but I don't know how to do that, I've created an a WebApplication in my specflow project but when the test post a request, the response return status 500 because the API can't access .well-known/openid-configuration.
This is happening because the Auth server is not accessible during the unit test run. The API endpoint needs to validate the token against the Auth server.
What you need to do is have an auth server running separately before the unit tests (integration tests) are run.
I suspect you are attempting to run an instance of your AuthServer using something like WebApplicationFactory. This won't work because the service isn't available for the follow up call to the .well-known/openid-configuration endpoint.

Test my AngularJS service against a real web api

I am tasked with writing an AngularJS app that connects to a RESTful API provided by our client. I want to wrap that API in an angular service and write tests against it using the real API. I am confused on how and where to do this.
Should I be using Protractor for this test? It doesn't involve any UX elements at all, so it doesn't feel like it should use Protractor.
Should I be using Karma? I tried Karma with $httpBackend, ngMockE2E and .passThrough() for all whenGET and whenPOST calls, but I'm having issues with Unexpected request: POST errors.
You should use Karma. Protractor is for testing interaction with web pages.
Your Unexpected request: POST errors should be resolvable if you use $httpBackend.expectPOST() for each POST request you issue.

Developing and testing AngularJS apps (and apps in general)

I have an app I am building, like most I have built in the last few years, with a clean separation: REST API on the back-end, Angular (before that Backbone, before that just jQuery) on the front-end.
When first testing, it is easy to stub the REST API; when developing, I can do things like a static file server (like angular-seed's ./scripts/web-server) and put in place files. But at a certain point, I really need to develop and test against the real REST API.
How do people build against and launch in a case of a separated app like this? In the past, I had a ./dev node script which would set all sorts of test/dev database variables, then require('../../server-dev-dir/app.js') but it seems sort of heavy. Is there a cleaner way to do it?
Same issue for testing: the app really needs to be tested against a true REST API, at a certain point, stubs and mocks only help so much.
In AngularJS, you make all of your HTTP requests using the $http service. It's not your job to test $http. That's the job of AngularJS team. All you care about is this:
does your back-end or 3rd party (API) back-end receive the requests that $http is sending as configured by your AngularJS code?
To that end, AngularJS provides the $httpBackend mock. When angular-mocks.js is loaded in your tests, $http will send HTTP requests to it. You can train $httpBackend on how to respond to any URL.
You can also make assertions on what requests $httpBackend received and what data it received with those requests.

Anyway to get my Jasmine tests to run against the local gae server

I'm trying to test my backbone front-end using Jasmine against our back-end API.
Our application is built on google app engine, and our build system uses maven, so I'm using the jasmine-maven-plugin to facilitate testing the front-end integration with the API.
But, when I try to run the tests, none of the REST API calls are available (which makes sense since the server jasmine spins up is solely for jasmine testing).
Does any one know of a way to get the Jasmine plug in to use the jetty server that the "gae:run" target spins up?
Because there's a ton of authenticated calls, the server that is doing the front-end testing and the API need to be the same, including port (or it would be all cross-domain requests).
Looking at the Jasmine source code for the server here: https://github.com/searls/jasmine-maven-plugin/blob/master/src/main/java/com/github/searls/jasmine/ServerMojo.java it looks like jasmine is explicitly using the jetty server directly. I don't believe that appengine gives you access to its custom jetty driver directly but you might want to look at the gae-maven-plugin source to see how they do it. You would need to write your own implementation of the ServerMojo for jasmine and plug it in there though.

Resources