admin管理员组

文章数量:1220945

Here is my html code

<span class="boxText">
            {{model.number}}
    <i *ngIf="model.canText" class="fa fa-comment" placement="right"></i>
</span>

There is only one span tag in my code. I want to test if span tag contains the specific value, i.e model.number.

Here I am able to get the span element.

let el = fixture.debugElement.query(By.css('.boxText'));
let spanEl = el.nativeElement; 

But not able check if the value exist. How do i do this using jasmine?

Here is my html code

<span class="boxText">
            {{model.number}}
    <i *ngIf="model.canText" class="fa fa-comment" placement="right"></i>
</span>

There is only one span tag in my code. I want to test if span tag contains the specific value, i.e model.number.

Here I am able to get the span element.

let el = fixture.debugElement.query(By.css('.boxText'));
let spanEl = el.nativeElement; 

But not able check if the value exist. How do i do this using jasmine?

Share Improve this question edited Apr 5, 2017 at 13:39 Amit Chigadani asked Apr 5, 2017 at 13:26 Amit ChigadaniAmit Chigadani 29.7k14 gold badges87 silver badges101 bronze badges 4
  • Have you tried -> fixture.debugElement.query(By.css('.boxText'))[0] and then -> el.value ? – Alejandro Lora Commented Apr 5, 2017 at 13:33
  • I tried now, but that does not seem to work. – Amit Chigadani Commented Apr 5, 2017 at 13:41
  • can you upload a pic of this console.log(spanEl) ? Have you tried this spanEl.innerHTML ? it has to be like that, it's just accessing the element value, shouldn't be complex but without seeing what you're getting it's impossible – Alejandro Lora Commented Apr 5, 2017 at 14:23
  • @Alejandro Lora You are right here, spanEl.innerHTML will give the text within it. And then I could do expect(spanEl.innerHTML).toContain(component.model.number); Thanks much!! – Amit Chigadani Commented Apr 5, 2017 at 14:43
Add a comment  | 

1 Answer 1

Reset to default 19

I leave here the answer for someone else, but it was just accessing the element as on plain js.

let el = fixture.debugElement.query(By.css('.boxText'));
let spanEl = el.nativeElement;
// spanEl.innerHTML => give you the value

and test like normally

expect(spanEl.innerHTML).toContain(component.model.number);

本文标签: javascriptGet value of span tag in jasmine angular2 testingStack Overflow