angular/components · error

This TestElement was not created by the WebDriverHarnessEnvi

Error message

This TestElement was not created by the WebDriverHarnessEnvironment

What it means

Thrown by the static WebDriverHarnessEnvironment.getNativeElement when the TestElement is not a SeleniumWebDriverElement. Only elements created by this environment wrap a webdriver.WebElement and can be unwrapped.

Source

Thrown at src/cdk/testing/selenium-webdriver/selenium-web-driver-harness-environment.ts:95

  /** Environment stabilization callback passed to the created test elements. */
  private _stabilizeCallback: () => Promise<void>;

  protected constructor(
    rawRootElement: () => webdriver.WebElement,
    options?: WebDriverHarnessEnvironmentOptions,
  ) {
    super(rawRootElement);
    this._options = {...defaultEnvironmentOptions, ...options};
    this._stabilizeCallback = () => this.forceStabilize();
  }

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

  /** Creates a `HarnessLoader` rooted at the document root. */
  static loader(
    driver: webdriver.WebDriver,
    options?: WebDriverHarnessEnvironmentOptions,
  ): HarnessLoader {
    return new SeleniumWebDriverHarnessEnvironment(
      () => driver.findElement(webdriver.By.css('body')),
      options,
    );
  }

  /**
   * 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.
   */

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Create the element through WebDriverHarnessEnvironment (rootEl/driver based loaders) before unwrapping.
  2. Check el instanceof SeleniumWebDriverElement before calling getNativeElement.
  3. Use the environment-specific unwrap helper that matches the element's origin.

Example fix

// before
const webEl = WebDriverHarnessEnvironment.getNativeElement(el);
// after
if (el instanceof SeleniumWebDriverElement) {
  const webEl = WebDriverHarnessEnvironment.getNativeElement(el);
}
Defensive patterns

Strategy: type-guard

Validate before calling

import {SeleniumWebDriverElement} from '@angular/cdk/testing/selenium-webdriver';
if (!(el instanceof SeleniumWebDriverElement)) throw new TypeError('el must come from WebDriverHarnessEnvironment');

Type guard

const isSeleniumElement = (el: TestElement): el is SeleniumWebDriverElement => el instanceof SeleniumWebDriverElement;

Try / catch

let webEl: webdriver.WebElement | undefined;
try { webEl = WebDriverHarnessEnvironment.getNativeElement(el); } catch { webEl = undefined; }

Prevention

When it happens

Trigger: Calling WebDriverHarnessEnvironment.getNativeElement(testElement) with a TestbedHarnessElement or ProtractorElement, i.e. an element created by a different harness environment.

Common situations: Shared helpers passing TestElements between unit-test (TestBed) and Selenium e2e code paths; mixing harness environments in one test suite.

Related errors


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