Keycloak-angular: Keycloak breaks Protractor WaitForAngularEnabled()

Created on 18 Sep 2019  路  6Comments  路  Source: mauriciovigolo/keycloak-angular

Bug Report or Feature Request (mark with an x)

- [ x] bug report -> please search for issues before submitting
- [ ] feature request

Versions.

  • keycloak-angular: 7.0.1,
  • keycloak-js: 6.0.1,
  • angular 7.2.15,
  • protractor: 5.4.2

Repro steps.

Keycloak is initialized normally in app.module by using app_initializer where keycloak.init(initOptions) is being called as expected.

providers: [ { provide: APP_INITIALIZER, useFactory: appInitializer, multi: true, deps: [ConfigurationService, KeycloakService, UserService] } ],

Everything works as expected but when I now run tests that use protractor like serenity-js no interactions are being done on the website when browser.waitForAngularEnabled() is true.

The log given by the failure.

Logging all current zone.js macroTasks shows the following in the console:

ngZone

Note that this scheduled task is not going away and will continue to block all my protractor tests
as if the app is currently not available / loaded. I only looked slightly at the current implementation but it seems to be that the _keycloakEvents$ subject has something to do with it.

The problem persists and logging the current macroTasks from zone.js from time to time will result in this task always being scheduled and therefore blocking protractor as it tries to wait for $http and $timeout (my guess).

The second NgZone macroTask that you can see has something to do with scheduleCheckIframe() but as it is notScheduled it should not be a problem.

Desired functionality.

Running the tests without having to use explicit waits for all the specs would be a real time-saver for me. It could be that I am missing something but I would love to see a solution to my problem.

Most helpful comment

Really? This works for me

import {KeycloakService} from 'keycloak-angular';
import {NgZone} from '@angular/core';

export function keycloakIntitaliser(keycloak: KeycloakService, ngZone: NgZone): () => Promise<any> {
  return (): Promise<any> => {
    return ngZone.runOutsideAngular(() =>
      keycloak.init(/* my settings*/})
    );
  };
}

But I've fallen a bit behind on keycloak-angular version, so things might have changed in the newer versions.

All 6 comments

I don't really want to bump this issue but did anyone come across this problem in particular as it has been a while since I created it.

Or maybe I am doing something wrong on my end and protractor shouldn't have a problem with this package.

Might not be the same issue you have but I did the same as the user here https://github.com/mauriciovigolo/keycloak-angular/issues/73#issuecomment-394713211 I ran keycloak.init outside of angular. No idea if it's still necessary for later versions though.

The thing is that we are initializing keycloak in APP_INITIALIZER. That means I can't run it outside of ngzone as it has not been yet initialized or it is just not usable at that point in the app.

Initializing it at any other point during the app creates an annoying redirect where you see the app for a split second and then the keycloak login page.

Really? This works for me

import {KeycloakService} from 'keycloak-angular';
import {NgZone} from '@angular/core';

export function keycloakIntitaliser(keycloak: KeycloakService, ngZone: NgZone): () => Promise<any> {
  return (): Promise<any> => {
    return ngZone.runOutsideAngular(() =>
      keycloak.init(/* my settings*/})
    );
  };
}

But I've fallen a bit behind on keycloak-angular version, so things might have changed in the newer versions.

I only tried it for a while before I created this ticket. I will try it again on monday and update the ticket accordingly. Thank you for your help!

Sorry, I didn't really have the time to try it again until now but it does seem to be working!

Thank you for your input on this issue. I'll close it as it is no longer needed.

Was this page helpful?
0 / 5 - 0 ratings