加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

检查元素是否存在 – Jasmine&Angular

发布时间:2020-12-17 07:16:40 所属栏目:安全 来源:网络整理
导读:我想编写一个测试来检查点击后页面上是否存在元素.因此,当我单击带有“addItem”类的元素时,使用* ngIf隐藏此元素.我试过这样的: it('Should handle click on .addItem button',() = { spyOn(component,'addItem'); addItemDebugElement = componentFixture
我想编写一个测试来检查点击后页面上是否存在元素.因此,当我单击带有“addItem”类的元素时,使用* ngIf隐藏此元素.我试过这样的:

it('Should handle click on .addItem button',() => {
    spyOn(component,'addItem');
    addItemDebugElement = componentFixture.debugElement.query(By.css('.addItem'));
    addItemDebugElement.nativeElement.click();  //  click on the button
    expect(addItemDebugElement).toExist();
});

但它说:’Matchers< DebugElement>‘类型中不存在属性’toExist’.
你能告诉我怎么做吗?非常感谢!

解决方法

您可以尝试这个来检查Dom上是否存在该元素

expect($(document)).toContain(addItemDebugElement)

要么

expect(addItemDebugElement).toBeInDom();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读