首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular By.css中使用多选css的选择模式不会给出多个元素

在Angular By.css中使用多选css的选择模式不会给出多个元素
EN

Stack Overflow用户
提问于 2019-02-02 17:30:07
回答 1查看 361关注 0票数 1

css中,我可以执行a[href^="https"],它会选择<a>属性值以"https“开头的每个href元素。如何在AngularBy.css中指定相同的规则

https://angular.io/api/platform-browser/By中,By.css定义为

static css(selector: string): Predicate<DebugElement>

我的Angular代码有几个div,它们的id属性等于thumbnail-1thumbnail-2。我想把它们都放到我的Jasmine单元测试中。

目前,我正在逐一收集它们

let imageThumbnailDiv1 = fixture.debugElement.query(By.css("#thumbnail-1")); let imageThumbnailDiv2 = fixture.debugElement.query(By.css("#thumbnail-2"));

如果我使用多选模式,我只得到第一个元素。有没有办法获得多个元素。

代码语言:javascript
复制
  fit('select all thumbnails',(done)=>{
      let newPracticeQuestionComponent = component;
      let imageThumbnailDivs = fixture.debugElement.query(By.css("div[id^=\"thumbnail\"]"));
      console.log("thumbnail divs before file upload ",imageThumbnailDivs);
      expect(imageThumbnailDivs).toBeFalsy();

      let file1 = new File(["foo1"], "foo1.txt");
      let file2 = new File(["foo2"], "foo2.txt");
      let file3 = new File(["foo3"], "foo3.txt");
      //let file4 = new File(["foo4"], "foo4.txt");
      let reader1 = newPracticeQuestionComponent.handleFileSelect([file1]);
      let reader2 = newPracticeQuestionComponent.handleFileSelect([file2]);
      let reader3 = newPracticeQuestionComponent.handleFileSelect([file3]);

      setTimeout(function() {
        console.log("in timeout");

        fixture.detectChanges();//without this, the view will not be updated with model
        let imageThumbnailDivsAfter = fixture.debugElement.query(By.css("div[id^=\"thumbnail\""));
        console.log("thumbnail divs after file upload ",imageThumbnailDivsAfter); //<-- this shows only the 1st element
        expect(imageThumbnailDivsAfter).toBeTruthy();
        //console.log("before done call")
        done();//without done, jasmine will finish this test spec without checking the assertions in the timeout
        //console.log("after timeout call")
      }, 2000);

  });
EN

回答 1

Stack Overflow用户

发布于 2021-03-01 18:20:20

您可以使用debugElement.queryAll而不是debugElement.query来获得一个可以迭代的数组。

代码语言:javascript
复制
let thumbnails = fixture.debugElement.queryAll(By.css(".thumbnail"));

thumbnails.forEach((debugElement)=>{
    expect(debugElement)//Whatever you want to test
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54491713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档