Skip to content

feat(cdk/testing): add getNativeElement to harness environments #20538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cdk/testing/protractor/protractor-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFind
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');
}

async forceStabilize(): Promise<void> {}

async waitForTasksOutsideAngular(): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions src/cdk/testing/testbed/testbed-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
return new TestbedHarnessEnvironment(document.body, fixture, options);
}

/** Gets the native DOM element corresponding to the given TestElement. */
static getNativeElement(el: TestElement): Element {
if (el instanceof UnitTestElement) {
return el.element;
}
throw Error('This TestElement was not created by the TestbedHarnessEnvironment');
}

/**
* Creates an instance of the given harness type, using the fixture's root element as the
* harness's host element. This method should be used when creating a harness for the root element
Expand Down
7 changes: 7 additions & 0 deletions src/cdk/testing/tests/protractor.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ describe('ProtractorHarnessEnvironment', () => {
.getHarness(MainComponentHarness);
expect(await (await harness.deepShadow()).text()).toBe('Shadow 2');
});

it('should be able to retrieve the ElementFinder from a ProtractorElement', async () => {
const harness = await ProtractorHarnessEnvironment.loader({queryFn: piercingQueryFn})
.getHarness(MainComponentHarness);
const element = ProtractorHarnessEnvironment.getNativeElement(await harness.host());
expect(await element.getTagName()).toBe('test-main');
});
});
});

Expand Down
7 changes: 7 additions & 0 deletions src/cdk/testing/tests/testbed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ describe('TestbedHarnessEnvironment', () => {
);
expect(await (await harness.deepShadow()).text()).toBe('Shadow 2');
});

it('should be able to retrieve the native DOM element from a UnitTestElement', async () => {
const harness = await TestbedHarnessEnvironment
.harnessForFixture(fixture, MainComponentHarness);
const element = TestbedHarnessEnvironment.getNativeElement(await harness.host());
expect(element.id).toContain('root');
});
});
}
});
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/cdk/testing/protractor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<Ele
protected getAllRawElements(selector: string): Promise<ElementFinder[]>;
protected getDocumentRoot(): ElementFinder;
waitForTasksOutsideAngular(): Promise<void>;
static getNativeElement(el: TestElement): ElementFinder;
static loader(options?: ProtractorHarnessEnvironmentOptions): HarnessLoader;
}

Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/cdk/testing/testbed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export declare class TestbedHarnessEnvironment extends HarnessEnvironment<Elemen
protected getDocumentRoot(): Element;
waitForTasksOutsideAngular(): Promise<void>;
static documentRootLoader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
static getNativeElement(el: TestElement): Element;
static harnessForFixture<T extends ComponentHarness>(fixture: ComponentFixture<unknown>, harnessType: ComponentHarnessConstructor<T>, options?: TestbedHarnessEnvironmentOptions): Promise<T>;
static loader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
}
Expand Down