首页 > 解决方案 > 按属性名称获取元素 - Angular 测试

问题描述

说,我在我的 Angular 组件中有以下 HTML

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

在我的测试用例中,如何name='my-name'使用属性获取上述元素ComponentFixture

标签: angularangular8

解决方案


尝试

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';


推荐阅读