使用Keycloak服务器作为Angular中的令牌提供者进行量角器e2e测试

问题描述 投票:0回答:1

我是量角器的新手。无法测试组件,因为无法从启动测试文件e2e/app.e2e-spec.ts文件登录到keycloak服务器。

我正在使用keycloak服务器进行身份验证

在我的app.e2e-spec.ts中>

import { AppPage } from './app.po';
var protractor = require('protractor');
require('../node_modules/protractor/jasminewd');
//var config = require('../settings.js').getConfig();
require('../src/assets/keycloak.json')
const config = {
  url:'http://localhost:5000/',
  user:'test',
  password:'test'
}
describe('login', function() {
  it('should login', function() {
    console.log(config);
    var ptor = protractor.getInstance();
    var driver = ptor.driver;

    var findByName = function(name) {
      return driver.findElement(protractor.By.name(name));
    };

    driver.get(config.url);
    findByName('username').sendKeys(config.user);
    findByName('password').sendKeys(config.password);
    findByName('login').click();

  });
});

在我的keycloak.service.ts中>

@Injectable()
export class KeycloakService {
  private instance: Keycloak.KeycloakInstance;
  private userProfile: Keycloak.KeycloakProfile;
  private bearerExcludedUrls: string[];
  private bearerPrefix: string;
  static auth: any = {};

  constructor(private settings:AppSettings){

  }
  init(): Promise<any> {
    let keycloakAuth = Keycloak('keycloak.json');//referring setting in json file
    this.instance = keycloakAuth;  
    KeycloakService.auth.loggedIn = false;
      return new Promise((resolve, reject) => {
        keycloakAuth.init({ onLoad: 'login-required' })
          .success(() => {
            KeycloakService.auth.loggedIn = true;
            resolve();
          })
          .error((e) => {
            reject();
          });
      });
    }

}

在我的component.ts中,我需要先登录才能打开路线,所以我实现了APP_INITIALIZER获取组件实例,如下所示:

    describe('AppDashboardComponent', () => {
    let component: AppDashboardComponent;
    let fixture: ComponentFixture<AppDashboardComponent>;
    let de: DebugElement;
    let el: HTMLElement;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                AppDashboardComponent,
              ],
              imports: [
                  SharedModule,
                  ReactiveFormsModule,
                  HttpClientModule,
                  NGMaterialImportsModule,
                  CommonModule,
                  GrowlModule,
                  portfolioRouting
              ],
              providers: [KeycloakService,
                {
                  provide: _INITIALIZER,
                  useFactory: (config: KeycloakService) => 
                  () => config.init(),//This calls keycloak Init to goto login page
                  deps: [KeycloakService],
                  multi: true
                },
                ConfigService               
              ]
        })
    }))

    beforeEach(() => {
        fixture = TestBed.createComponent(AppDashboardComponent);
        component = fixture.componentInstance;
        de = fixture.debugElement.query(By.css('img'));
        el = de.nativeElement;
        // fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
})

[运行ng test时,出现错误[http://localhost:9876/keycloak.json 404(未找到)”,如下图所示。如何解决此问题,以便可以登录到外部密钥库登录页面。

Karma error

我是量角器的新手。无法测试组件,因为无法从启动测试文件e2e / app.e2e-spec.ts文件登录到keycloak服务器。我正在使用密钥斗篷服务器进行身份验证。...

angular protractor karma-runner keycloak
1个回答
0
投票

您是否已经找到解决方案?我有同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.