Skip to content

Commit 1328420

Browse files
authored
feat(cdk/testing): add getNativeElement to harness environments (#20538)
* feat(cdk/testing): add getNativeElement to harness environments * fixup! feat(cdk/testing): add getNativeElement to harness environments * fixup! fixup! feat(cdk/testing): add getNativeElement to harness environments * fixup! fixup! feat(cdk/testing): add getNativeElement to harness environments * fixup! fixup! feat(cdk/testing): add getNativeElement to harness environments * fixup! feat(cdk/testing): add getNativeElement to harness environments
1 parent 0e61769 commit 1328420

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

src/cdk/testing/protractor/protractor-harness-environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFind
3737
return new ProtractorHarnessEnvironment(protractorElement(by.css('body')), options);
3838
}
3939

40+
/** Gets the ElementFinder corresponding to the given TestElement. */
41+
static getNativeElement(el: TestElement): ElementFinder {
42+
if (el instanceof ProtractorElement) {
43+
return el.element;
44+
}
45+
throw Error('This TestElement was not created by the ProtractorHarnessEnvironment');
46+
}
47+
4048
async forceStabilize(): Promise<void> {}
4149

4250
async waitForTasksOutsideAngular(): Promise<void> {

src/cdk/testing/testbed/testbed-harness-environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
6464
return new TestbedHarnessEnvironment(document.body, fixture, options);
6565
}
6666

67+
/** Gets the native DOM element corresponding to the given TestElement. */
68+
static getNativeElement(el: TestElement): Element {
69+
if (el instanceof UnitTestElement) {
70+
return el.element;
71+
}
72+
throw Error('This TestElement was not created by the TestbedHarnessEnvironment');
73+
}
74+
6775
/**
6876
* Creates an instance of the given harness type, using the fixture's root element as the
6977
* harness's host element. This method should be used when creating a harness for the root element

src/cdk/testing/tests/protractor.e2e.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ describe('ProtractorHarnessEnvironment', () => {
7373
.getHarness(MainComponentHarness);
7474
expect(await (await harness.deepShadow()).text()).toBe('Shadow 2');
7575
});
76+
77+
it('should be able to retrieve the ElementFinder from a ProtractorElement', async () => {
78+
const harness = await ProtractorHarnessEnvironment.loader({queryFn: piercingQueryFn})
79+
.getHarness(MainComponentHarness);
80+
const element = ProtractorHarnessEnvironment.getNativeElement(await harness.host());
81+
expect(await element.getTagName()).toBe('test-main');
82+
});
7683
});
7784
});
7885

src/cdk/testing/tests/testbed.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ describe('TestbedHarnessEnvironment', () => {
102102
);
103103
expect(await (await harness.deepShadow()).text()).toBe('Shadow 2');
104104
});
105+
106+
it('should be able to retrieve the native DOM element from a UnitTestElement', async () => {
107+
const harness = await TestbedHarnessEnvironment
108+
.harnessForFixture(fixture, MainComponentHarness);
109+
const element = TestbedHarnessEnvironment.getNativeElement(await harness.host());
110+
expect(element.id).toContain('root');
111+
});
105112
});
106113
}
107114
});

tools/public_api_guard/cdk/testing/protractor.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<Ele
2929
protected getAllRawElements(selector: string): Promise<ElementFinder[]>;
3030
protected getDocumentRoot(): ElementFinder;
3131
waitForTasksOutsideAngular(): Promise<void>;
32+
static getNativeElement(el: TestElement): ElementFinder;
3233
static loader(options?: ProtractorHarnessEnvironmentOptions): HarnessLoader;
3334
}
3435

tools/public_api_guard/cdk/testing/testbed.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export declare class TestbedHarnessEnvironment extends HarnessEnvironment<Elemen
77
protected getDocumentRoot(): Element;
88
waitForTasksOutsideAngular(): Promise<void>;
99
static documentRootLoader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
10+
static getNativeElement(el: TestElement): Element;
1011
static harnessForFixture<T extends ComponentHarness>(fixture: ComponentFixture<unknown>, harnessType: ComponentHarnessConstructor<T>, options?: TestbedHarnessEnvironmentOptions): Promise<T>;
1112
static loader(fixture: ComponentFixture<unknown>, options?: TestbedHarnessEnvironmentOptions): HarnessLoader;
1213
}

0 commit comments

Comments
 (0)