首页 > 解决方案 > 等待元素出现在dom中然后执行代码angular 6单元测试

问题描述

我需要等待元素被加载到 dom 中,所以我需要检查它包含的文本字符串。

我试过到目前为止,fixture.whenStable().then但没有奏效。我如何等待元素直到它加载到 dom 中。

it( 'should change the text value for the element', function() {
    let testDropdown : HTMLElement;
    const debugElement : DebugElement = fixture.debugElement;
    fixture.detectChanges();
    testDropdown = debugElement.queryAll( By.css( 'mat-select' ) )[ 0 ].nativeElement;
    testDropdown.click();
    fixture.detectChanges();
    debugElement.queryAll( By.css( 'mat-option' ) )[ 1 ].nativeElement.click();
    fixture.detectChanges();
    expect( testDropdown.getAttribute( 'ng-reflect-value' ) ).toEqual( 'Text 2' );
    fixture.detectChanges();
    const elementTitle = debugElement.queryAll( By.css( '.legend' ) );
    console.log( elementTitle );
} );

一旦我更改了选择选项,相同的选择选项文本,我将设置为图例元素。我需要等待那个图例项被加载。该元素将异步加载。

有什么建议么?我该如何进行?

标签: angularunit-testingkarma-jasminetypescript2.0

解决方案


推荐阅读