What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Angular component harnesses let tests interact with components through supported, user-oriented APIs instead of depending on private DOM structure. They are most useful for shared, interactive components whose markup may change independently of the tests that use them.
What a component harness does
A component harness is a class that gives tests a stable API for operating a component in ways that resemble user interaction. Rather than selecting internal elements and dispatching DOM events directly, a test can call methods such as toggle() or read state through isOpen(). That keeps assertions focused on behavior and reduces reliance on markup and CSS details.
Angular describes harnesses as reusable across unit and end-to-end tests. The harness API separates tests that consume a component from the component’s internal implementation.
Use a harness in a TestBed test
The harness infrastructure is part of Angular CDK. If the project does not already include it, add the package with ng add @angular/cdk. In a TestBed test, create the fixture, make a loader scoped to it, and ask that loader for the component’s harness:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitches#1 Best Overall
const fixture = TestBed.createComponent(MyComponent);
const loader = TestbedHarnessEnvironment.loader(fixture);
const component = await loader.getHarness(MyComponentHarness);
Harness APIs are generally asynchronous, so await calls that query or interact with harnesses. The current Angular guide documents TestBed unit tests and Selenium WebDriver end-to-end tests as built-in CDK environments; support can vary by Angular/CDK version, so check the documentation matching the versions in your project. See Angular’s guide to using component harnesses.
Choose the loader that contains the element
| Loader | Search scope | Use it for |
|---|---|---|
TestbedHarnessEnvironment.loader(fixture) |
The fixture root | Elements rendered within the tested component fixture. |
TestbedHarnessEnvironment.documentRootLoader(fixture) |
The document root | Content rendered outside the fixture, such as dialogs or CDK overlays attached under document.body. |
If a harness query cannot find an overlay, the issue may be its search scope rather than the component: use the document-root loader when the target is outside the fixture. Angular also provides harnessForFixture as an option for loading one harness directly for a fixture root. The API details are in the TestbedHarnessEnvironment reference.
Query one or more harnesses
A HarnessLoader can retrieve one matching harness, enumerate matches, or test for their presence. Use the query that matches the assertion rather than fetching elements and inspecting private markup.
getHarnessretrieves one matching harness.getAllHarnessesretrieves all matching harnesses.getHarnessAtIndexretrieves a match by index.countHarnessescounts matches.hasHarnesschecks whether a match exists.
Many harness classes provide a static with() helper that creates a HarnessPredicate. Predicates support meaningful filters, such as a selector or component-specific text, so a test can identify the intended instance without relying on its private DOM structure. See the HarnessLoader API and HarnessPredicate API.
Handle promises and change detection
Await harness calls consistently. TestBed harnesses run change detection before reading element state and after interactions, which is generally suitable for ordinary tests. When a test needs to examine an intermediate state while asynchronous work is still pending, use manualChangeDetection to take control of change detection for that block. Angular documents these behaviors in its harness testing guide.
Write a custom harness around user-visible behavior
Extend ComponentHarness and define a static hostSelector that usually matches the component or directive selector. Expose useful user actions and observable state—not every internal element. For example, a menu harness might offer an action to open the menu and a method to report whether it is open.
Rank #4
Use locatorFor, locatorForOptional, and locatorForAll for element queries. These locators resolve against the current DOM, which helps when conditional content is removed and later recreated. Interact through TestElement rather than direct DOM access; it is designed to work across environments. The Angular guide to creating component harnesses covers the authoring API.
For components with multiple instances, add a static with() method that builds a HarnessPredicate for common selectors or component-specific filters. Tests can then select the instance they mean through the harness API.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Best Value
Decide whether a component needs a harness
Harnesses add an abstraction, so they are not necessary for every component. Angular recommends them particularly for shared components used in many places that also have user interaction. A one-off page often gains less: its implementation and tests tend to change together. A custom harness can still be worthwhile when the same component needs a consistent test API in unit and end-to-end tests.
Support for other testing environments
Beyond the built-in TestBed and Selenium WebDriver environments named in Angular’s guide, using harnesses requires environment-specific work. A custom environment needs a TestElement implementation for its raw element type and a concrete HarnessEnvironment subclass. It must locate matching raw elements, create test elements and child environments, identify the document root, stabilize Angular work, wait for tasks outside Angular, and expose a loader factory to test authors. Consult the environment-authoring guidance and verify the support available in the Angular/CDK version your project uses.
The Angular documentation checked on October 5, 2026, reported version v22.2.1+sha-ef03596. APIs and supported environments may change; use documentation aligned with your installed Angular and CDK versions.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




