Skip to content

Commit 8eb6754

Browse files
authored
feat(cdk/testing): add optional excludes to TestElement text method (#20145)
* add optional excludes to TestElement text method * add implementation for UnitTestElement and ProtractorElement * add test, refactor * add e2e test * move shared function to separate file * rename file, udpate golden * add jsdoc, export TextOptions interface * fix jsdoc and import * export text filtering from cdk/testing
1 parent 99ad347 commit 8eb6754

File tree

10 files changed

+83
-9
lines changed

10 files changed

+83
-9
lines changed

src/cdk/testing/protractor/protractor-element.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
9+
import {
10+
_getTextWithExcludedElements,
11+
ElementDimensions,
12+
ModifierKeys,
13+
TestElement,
14+
TestKey,
15+
TextOptions
16+
} from '@angular/cdk/testing';
1017
import {browser, ElementFinder, Key} from 'protractor';
1118

1219
/** Maps the `TestKey` constants to Protractor's `Key` constants. */
@@ -129,7 +136,10 @@ export class ProtractorElement implements TestElement {
129136
return this.element.sendKeys(...keys);
130137
}
131138

132-
async text(): Promise<string> {
139+
async text(options?: TextOptions): Promise<string> {
140+
if (options?.exclude) {
141+
return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
142+
}
133143
return this.element.getText();
134144
}
135145

src/cdk/testing/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './component-harness';
1010
export * from './harness-environment';
1111
export * from './test-element';
1212
export * from './element-dimensions';
13+
export * from './text-filtering';

src/cdk/testing/test-element.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ export interface TestElement {
101101
*/
102102
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
103103

104-
/** Gets the text from the element. */
105-
text(): Promise<string>;
104+
/**
105+
* Gets the text from the element.
106+
* @param options Options that affect what text is included.
107+
*/
108+
text(options?: TextOptions): Promise<string>;
106109

107110
/** Gets the value for the given attribute from the element. */
108111
getAttribute(name: string): Promise<string | null>;
@@ -128,3 +131,8 @@ export interface TestElement {
128131
/** Checks whether the element is focused. */
129132
isFocused(): Promise<boolean>;
130133
}
134+
135+
export interface TextOptions {
136+
/** Optional selector for elements to exclude. */
137+
exclude?: string;
138+
}

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
*/
88

99
import * as keyCodes from '@angular/cdk/keycodes';
10-
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
10+
import {
11+
_getTextWithExcludedElements,
12+
ElementDimensions,
13+
ModifierKeys,
14+
TestElement,
15+
TestKey,
16+
TextOptions
17+
} from '@angular/cdk/testing';
1118
import {
1219
clearElement,
1320
dispatchMouseEvent,
@@ -126,8 +133,11 @@ export class UnitTestElement implements TestElement {
126133
await this._stabilize();
127134
}
128135

129-
async text(): Promise<string> {
136+
async text(options?: TextOptions): Promise<string> {
130137
await this._stabilize();
138+
if (options?.exclude) {
139+
return _getTextWithExcludedElements(this.element, options.exclude);
140+
}
131141
return (this.element.textContent || '').trim();
132142
}
133143

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ export function crossEnvironmentSpecs(
412412
expect(await button.matchesSelector('button:disabled')).toBe(false);
413413
});
414414

415+
416+
it('should get correct text excluding certain selectors', async () => {
417+
const results = await harness.subcomponentAndSpecialHarnesses();
418+
const subHarnessHost = await results[0].host();
419+
420+
expect(await subHarnessHost.text({exclude: 'h2'})).toBe('ProtractorTestBedOther');
421+
expect(await subHarnessHost.text({exclude: 'li'})).toBe('List of test tools');
422+
});
423+
415424
it('should get TestElements and ComponentHarnesses', async () => {
416425
const results = await harness.subcomponentHarnessesAndElements();
417426
expect(results.length).toBe(5);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ describe('ProtractorHarnessEnvironment', () => {
4444
const globalEl = await harness.globalEl();
4545
expect(await globalEl.text()).toBe('I am a sibling!');
4646
});
47+
48+
it('should get correct text excluding certain selectors', async () => {
49+
const results = await harness.subcomponentAndSpecialHarnesses();
50+
const subHarnessHost = await results[0].host();
51+
52+
expect(await subHarnessHost.text({exclude: 'h2'})).toBe('ProtractorTestBedOther');
53+
expect(await subHarnessHost.text({exclude: 'li'})).toBe('List of test tools');
54+
});
4755
});
4856

4957
describe('shadow DOM interaction', () => {

src/cdk/testing/text-filtering.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* Gets text of element excluding certain selectors within the element.
11+
* @param element Element to get text from,
12+
* @param excludeSelector Selector identifying which elements to exclude,
13+
*/
14+
export function _getTextWithExcludedElements(element: Element, excludeSelector: string) {
15+
const clone = element.cloneNode(true) as Element;
16+
const exclusions = clone.querySelectorAll(excludeSelector);
17+
for (let i = 0; i < exclusions.length; i++) {
18+
let child = exclusions[i];
19+
child.parentNode?.removeChild(child);
20+
}
21+
return (clone.textContent || '').trim();
22+
}

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export declare function _getTextWithExcludedElements(element: Element, excludeSelector: string): string;
2+
13
export declare type AsyncFactoryFn<T> = () => Promise<T>;
24

35
export declare type AsyncOptionPredicate<T, O> = (item: T, option: O) => Promise<boolean>;
@@ -131,7 +133,7 @@ export interface TestElement {
131133
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
132134
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
133135
setInputValue?(value: string): Promise<void>;
134-
text(): Promise<string>;
136+
text(options?: TextOptions): Promise<string>;
135137
}
136138

137139
export declare enum TestKey {
@@ -166,3 +168,7 @@ export declare enum TestKey {
166168
F12 = 28,
167169
META = 29
168170
}
171+
172+
export interface TextOptions {
173+
exclude?: string;
174+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export declare class ProtractorElement implements TestElement {
1717
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
1818
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
1919
setInputValue(value: string): Promise<void>;
20-
text(): Promise<string>;
20+
text(options?: TextOptions): Promise<string>;
2121
}
2222

2323
export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFinder> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ export declare class UnitTestElement implements TestElement {
3434
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
3535
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
3636
setInputValue(value: string): Promise<void>;
37-
text(): Promise<string>;
37+
text(options?: TextOptions): Promise<string>;
3838
}

0 commit comments

Comments
 (0)