angular/components · error

This TestElement was not created by the ProtractorHarnessEnv

Error message

This TestElement was not created by the ProtractorHarnessEnvironment

What it means

Thrown by the static ProtractorHarnessEnvironment.getNativeElement when the TestElement passed in is not a ProtractorElement. Only elements created by the Protractor harness environment wrap a Protractor ElementFinder; anything else cannot be unwrapped.

Source

Thrown at src/cdk/testing/protractor/protractor-harness-environment.ts:56

  protected constructor(
    rawRootElement: ElementFinder,
    options?: ProtractorHarnessEnvironmentOptions,
  ) {
    super(rawRootElement);
    this._options = {...defaultEnvironmentOptions, ...options};
  }

  /** Creates a `HarnessLoader` rooted at the document root. */
  static loader(options?: ProtractorHarnessEnvironmentOptions): HarnessLoader {
    return new ProtractorHarnessEnvironment(protractorElement(by.css('body')), options);
  }

  /** Gets the ElementFinder corresponding to the given TestElement. */
  static getNativeElement(el: TestElement): ElementFinder {
    if (el instanceof ProtractorElement) {
      return el.element;
    }
    throw Error('This TestElement was not created by the ProtractorHarnessEnvironment');
  }

  /**
   * Flushes change detection and async tasks captured in the Angular zone.
   * In most cases it should not be necessary to call this manually. However, there may be some edge
   * cases where it is needed to fully flush animation events.
   */
  async forceStabilize(): Promise<void> {}

  /** @docs-private */
  async waitForTasksOutsideAngular(): Promise<void> {
    // TODO: figure out how we can do this for the protractor environment.
    // https://github.com/angular/components/issues/17412
  }

  /** Gets the root element for the document. */
  protected getDocumentRoot(): ElementFinder {
    return protractorElement(by.css('body'));

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Obtain the TestElement from the Protractor harness environment (e.g. via ProtractorHarnessEnvironment.loader() based harnesses) before unwrapping.
  2. Check el instanceof ProtractorElement before calling getNativeElement.
  3. Switch the helper to the correct environment's getNativeElement (WebDriverHarnessEnvironment or TestbedHarnessElement equivalents).

Example fix

// before
const finder = ProtractorHarnessEnvironment.getNativeElement(el);
// after
if (el instanceof ProtractorElement) {
  const finder = ProtractorHarnessEnvironment.getNativeElement(el);
}
Defensive patterns

Strategy: type-guard

Validate before calling

import {ProtractorElement} from '@angular/cdk/testing/protractor';
if (!(el instanceof ProtractorElement)) throw new TypeError('el must come from ProtractorHarnessEnvironment');

Type guard

const isProtractorElement = (el: TestElement): el is ProtractorElement => el instanceof ProtractorElement;

Try / catch

let finder: ElementFinder | undefined;
try { finder = ProtractorHarnessEnvironment.getNativeElement(el); } catch { finder = undefined; }

Prevention

When it happens

Trigger: Calling ProtractorHarnessEnvironment.getNativeElement(testElement) where testElement came from TestbedHarnessEnvironment (unit-test TestElement), SeleniumWebDriverElement, or a mock/custom TestElement implementation.

Common situations: Shared test utilities that pass TestElements across environments (TestBed harnesses in one project, Protractor e2e in another); copy-pasting a helper from unit tests into Protractor specs.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/97fc84bb3d3106d5. Report an issue: GitHub.