I can get element with using the
fixture.debugElement.query(By.css('h1'));
But what I should to do when I want get element through class name. Something like this
fixture.debugElement.query(By.css('class="name"'))
Angular defines a number of decorators that attach specific kinds of metadata to classes, so that the system knows what those classes mean and how they should work.
We use the Jasmine-provided it function to define a spec. The first parameter of it is a text description of what the spec will be testing — in this case we have a defined component. The second parameter is a function that will run the test. We then use Jasmine's expect function to define our expectation.
DebugElement is an Angular class that contains all kinds of references and methods relevant to investigate an element as well as component fixture.debugElement.query(By.css('#shan'))
You use By.css
to pass a css selector. So any selector you can use with css, you can use with By.css
. And a selector for a class is simply .classname
(with period).
By.css('.classname') // get by class name By.css('input[type=radio]') // get input by type radio By.css('.parent .child') // get child who has a parent
These are just some example. If you know css, then you should know how to use selectors.
EDIT: To use By.css()
be sure to import { By } from '@angular/platform-browser';
Just to add some other usefull ways for selecting elements:
// get element with multiple classes fixture.debugElement.query(By.css('.className1.className2')); // get a certain element from a group of elements with the same class name fixture.debugElement.queryAll(By.css('.className'))[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With