我是量角器的新手。无法测试组件,因为无法从启动测试文件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(未找到)”,如下图所示。如何解决此问题,以便可以登录到外部密钥库登录页面。
我是量角器的新手。无法测试组件,因为无法从启动测试文件e2e / app.e2e-spec.ts文件登录到keycloak服务器。我正在使用密钥斗篷服务器进行身份验证。...
您是否已经找到解决方案?我有同样的问题。