Skip to content

Commit 9187032

Browse files
committed
fix(cdk/testing): protractor element not extracting hidden text
We use Protractor's `getText` method to extract text, but the problem is that it's likely based on `HTMLElement.innerText` which excludes text from hidden elements. These changes switch to using `textContent` to bring the behavior in-line with the `UnitTestElement`.
1 parent ddc2e23 commit 9187032

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ export class ProtractorElement implements TestElement {
139139
if (options?.exclude) {
140140
return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
141141
}
142-
return this.element.getText();
142+
// We don't go through Protractor's `getText`, because it excludes text from hidden elements.
143+
return browser.executeScript(`return (arguments[0].textContent || '').trim()`, this.element);
143144
}
144145

145146
async getAttribute(name: string): Promise<string|null> {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ export function crossEnvironmentSpecs(
597597
await button.blur();
598598
expect(await button.isFocused()).toBe(false);
599599
});
600+
601+
it('should be able to get the text of a hidden element', async () => {
602+
const hiddenElement = await harness.hidden();
603+
expect(await hiddenElement.text()).toBe('Hello');
604+
});
605+
600606
});
601607
}
602608

src/cdk/testing/tests/harnesses/main-component-harness.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class MainComponentHarness extends ComponentHarness {
9595
readonly hoverTest = this.locatorFor('#hover-box');
9696
readonly customEventBasic = this.locatorFor('#custom-event-basic');
9797
readonly customEventObject = this.locatorFor('#custom-event-object');
98+
readonly hidden = this.locatorFor('.hidden-element');
9899

99100
private _testTools = this.locatorFor(SubComponentHarness);
100101

src/cdk/testing/tests/test-main-component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ <h1 style="height: 100px; width: 200px;">Main Component</h1>
6969
(mouseenter)="isHovering = true"
7070
(mouseleave)="isHovering = false"
7171
style="width: 50px; height: 50px; background: hotpink;"></div>
72+
<div class="hidden-element" style="display: none;">Hello</div>

src/material-experimental/mdc-card/testing/card-harness.e2e.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ describe('card harness', () => {
1414

1515
it('should get card text', async () => {
1616
const card = await loader.getHarness(MatCardHarness);
17-
expect(await card.getText()).toBe([
18-
'Shiba Inu',
19-
'Dog Breed',
20-
'The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from' +
21-
' Japan. A small, agile dog that copes very well with mountainous terrain, the Shiba Inu' +
22-
' was originally bred for hunting.',
23-
'LIKE',
24-
'SHARE'
25-
].join('\n'));
17+
expect(await card.getText()).toBe('Shiba InuDog Breed The Shiba Inu is the smallest of ' +
18+
'the six original and distinct spitz breeds of dog from ' +
19+
'Japan. A small, agile dog that copes very well with ' +
20+
'mountainous terrain, the Shiba Inu was originally bred ' +
21+
'for hunting. LIKESHARE');
2622
});
2723

2824
it('should get title text', async () => {

src/material/card/testing/card-harness.e2e.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ describe('card harness', () => {
1414

1515
it('should get card text', async () => {
1616
const card = await loader.getHarness(MatCardHarness);
17-
expect(await card.getText()).toBe([
18-
'Shiba Inu',
19-
'Dog Breed',
20-
'The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from' +
21-
' Japan. A small, agile dog that copes very well with mountainous terrain, the Shiba Inu' +
22-
' was originally bred for hunting.',
23-
'LIKE',
24-
'SHARE'
25-
].join('\n'));
17+
expect(await card.getText()).toBe('Shiba InuDog Breed The Shiba Inu is the smallest of ' +
18+
'the six original and distinct spitz breeds of dog from ' +
19+
'Japan. A small, agile dog that copes very well with ' +
20+
'mountainous terrain, the Shiba Inu was originally bred ' +
21+
'for hunting. LIKESHARE');
2622
});
2723

2824
it('should get title text', async () => {

src/material/toolbar/testing/toolbar-harness.e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('toolbar harness', () => {
1616
const toolbar = await loader.getHarness(MatToolbarHarness);
1717

1818
expect(await toolbar.getRowsAsText())
19-
.toEqual(['Custom Toolbar', 'Second Line\nverified_user', 'Third Line\nfavorite\ndelete']);
19+
.toEqual(['Custom Toolbar', 'Second Lineverified_user', 'Third Linefavoritedelete']);
2020
});
2121

2222
it('should have multiple rows', async () => {

0 commit comments

Comments
 (0)