DOM interaction
Now we are going to cover the DOM interaction, based on the previous code snippets we will find out how we can test a specific section of the DOM.
DebugElement
The Angular fixture provides the component's element directly through the fixture.nativeElement
.
This is actually a convenience method, implemented as fixture.debugElement.nativeElement
.
There's a good reason for this circuitous path to the element.
The properties of the nativeElement
depend upon the runtime environment. You could be running these tests on a non-browser platform that doesn't have a DOM or whose DOM-emulation doesn't support the full HTMLElement
API.
Because the sample tests for this guide are designed to run only in a browser, a nativeElement
in these tests is always an HTMLElement
whose familiar methods and properties you can explore within a test.
By.css()
Although the tests in this guide all run in the browser, some apps might run on a different platform at least some of the time.
For example, the component might render first on the server as part of a strategy to make the application launch faster on poorly connected devices. The server-side renderer might not support the full HTML element API. If it doesn't support querySelector
, the previous test could fail.
The following example re-implements the previous test with DebugElement.query()
and the browser's By.css
method.
Some noteworthy observations:
You must unwrap that result to get the paragraph element.
Last updated
Was this helpful?