Skip to content

feat(cdk/testing): add optional excludes to TestElement text method #20145

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 9 commits into from
Aug 19, 2020
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
14 changes: 12 additions & 2 deletions src/cdk/testing/protractor/protractor-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
import {
_getTextWithExcludedElements,
ElementDimensions,
ModifierKeys,
TestElement,
TestKey,
TextOptions
} from '@angular/cdk/testing';
import {browser, ElementFinder, Key} from 'protractor';

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

async text(): Promise<string> {
async text(options?: TextOptions): Promise<string> {
if (options?.exclude) {
return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
}
return this.element.getText();
}

Expand Down
1 change: 1 addition & 0 deletions src/cdk/testing/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './component-harness';
export * from './harness-environment';
export * from './test-element';
export * from './element-dimensions';
export * from './text-filtering';
12 changes: 10 additions & 2 deletions src/cdk/testing/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ export interface TestElement {
*/
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;

/** Gets the text from the element. */
text(): Promise<string>;
/**
* Gets the text from the element.
* @param options Options that affect what text is included.
*/
text(options?: TextOptions): Promise<string>;

/** Gets the value for the given attribute from the element. */
getAttribute(name: string): Promise<string | null>;
Expand All @@ -128,3 +131,8 @@ export interface TestElement {
/** Checks whether the element is focused. */
isFocused(): Promise<boolean>;
}

export interface TextOptions {
/** Optional selector for elements to exclude. */
exclude?: string;
}
14 changes: 12 additions & 2 deletions src/cdk/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
*/

import * as keyCodes from '@angular/cdk/keycodes';
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
import {
_getTextWithExcludedElements,
ElementDimensions,
ModifierKeys,
TestElement,
TestKey,
TextOptions
} from '@angular/cdk/testing';
import {
clearElement,
dispatchMouseEvent,
Expand Down Expand Up @@ -126,8 +133,11 @@ export class UnitTestElement implements TestElement {
await this._stabilize();
}

async text(): Promise<string> {
async text(options?: TextOptions): Promise<string> {
await this._stabilize();
if (options?.exclude) {
return _getTextWithExcludedElements(this.element, options.exclude);
}
return (this.element.textContent || '').trim();
}

Expand Down
9 changes: 9 additions & 0 deletions src/cdk/testing/tests/cross-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ export function crossEnvironmentSpecs(
expect(await button.matchesSelector('button:disabled')).toBe(false);
});


it('should get correct text excluding certain selectors', async () => {
const results = await harness.subcomponentAndSpecialHarnesses();
const subHarnessHost = await results[0].host();

expect(await subHarnessHost.text({exclude: 'h2'})).toBe('ProtractorTestBedOther');
expect(await subHarnessHost.text({exclude: 'li'})).toBe('List of test tools');
});

it('should get TestElements and ComponentHarnesses', async () => {
const results = await harness.subcomponentHarnessesAndElements();
expect(results.length).toBe(5);
Expand Down
8 changes: 8 additions & 0 deletions src/cdk/testing/tests/protractor.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ describe('ProtractorHarnessEnvironment', () => {
const globalEl = await harness.globalEl();
expect(await globalEl.text()).toBe('I am a sibling!');
});

it('should get correct text excluding certain selectors', async () => {
const results = await harness.subcomponentAndSpecialHarnesses();
const subHarnessHost = await results[0].host();

expect(await subHarnessHost.text({exclude: 'h2'})).toBe('ProtractorTestBedOther');
expect(await subHarnessHost.text({exclude: 'li'})).toBe('List of test tools');
});
});

describe('shadow DOM interaction', () => {
Expand Down
22 changes: 22 additions & 0 deletions src/cdk/testing/text-filtering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

/**
* Gets text of element excluding certain selectors within the element.
* @param element Element to get text from,
* @param excludeSelector Selector identifying which elements to exclude,
*/
export function _getTextWithExcludedElements(element: Element, excludeSelector: string) {
const clone = element.cloneNode(true) as Element;
const exclusions = clone.querySelectorAll(excludeSelector);
for (let i = 0; i < exclusions.length; i++) {
let child = exclusions[i];
child.parentNode?.removeChild(child);
}
return (clone.textContent || '').trim();
}
8 changes: 7 additions & 1 deletion tools/public_api_guard/cdk/testing.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export declare function _getTextWithExcludedElements(element: Element, excludeSelector: string): string;

export declare type AsyncFactoryFn<T> = () => Promise<T>;

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

export declare enum TestKey {
Expand Down Expand Up @@ -166,3 +168,7 @@ export declare enum TestKey {
F12 = 28,
META = 29
}

export interface TextOptions {
exclude?: string;
}
2 changes: 1 addition & 1 deletion tools/public_api_guard/cdk/testing/protractor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export declare class ProtractorElement implements TestElement {
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
setInputValue(value: string): Promise<void>;
text(): Promise<string>;
text(options?: TextOptions): Promise<string>;
}

export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFinder> {
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/cdk/testing/testbed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export declare class UnitTestElement implements TestElement {
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
setInputValue(value: string): Promise<void>;
text(): Promise<string>;
text(options?: TextOptions): Promise<string>;
}