通过属性名称获取元素-角度测试

时间:2020-01-10 06:08:45

标签: angular angular8

说,我在Angular组件中使用了HTML

<div name='my-name'>
  something
</div>

在测试案例中,如何使用name='my-name'通过属性ComponentFixture获取以上元素?

2 个答案:

答案 0 :(得分:1)

尝试

const fixture = TestBed.createComponent(AppComponent);
const debugElement = fixture.debugElement;
const attributeEl = debugElement.query(By.css('div[name="my-name"]'));

并确保从import { By } from '@angular/platform-browser';

导入通过

答案 1 :(得分:0)

尝试这样

fixture = TestBed.createComponent(AppComponent);
debugElement = fixture.debugElement;

const value = debugElement.query(By.css('name='my-name''));

如果这不起作用,请告诉我。

相关问题