Skip to content

Commit 90f6726

Browse files
crisbetommalerba
authored andcommitted
fix(cdk/testing): protractor element not extracting hidden text (#21540)
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`. (cherry picked from commit 26366e7)
1 parent 53ba5a7 commit 90f6726

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
@@ -183,7 +183,8 @@ export class ProtractorElement implements TestElement {
183183
if (options?.exclude) {
184184
return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
185185
}
186-
return this.element.getText();
186+
// We don't go through Protractor's `getText`, because it excludes text from hidden elements.
187+
return browser.executeScript(`return (arguments[0].textContent || '').trim()`, this.element);
187188
}
188189

189190
/** Gets the value for the given attribute from the element. */

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ export function crossEnvironmentSpecs(
617617
await button.blur();
618618
expect(await button.isFocused()).toBe(false);
619619
});
620+
621+
it('should be able to get the text of a hidden element', async () => {
622+
const hiddenElement = await harness.hidden();
623+
expect(await hiddenElement.text()).toBe('Hello');
624+
});
625+
620626
});
621627
}
622628

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class MainComponentHarness extends ComponentHarness {
103103
readonly hoverTest = this.locatorFor('#hover-box');
104104
readonly customEventBasic = this.locatorFor('#custom-event-basic');
105105
readonly customEventObject = this.locatorFor('#custom-event-object');
106+
readonly hidden = this.locatorFor('.hidden-element');
106107

107108
private _testTools = this.locatorFor(SubComponentHarness);
108109

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,4 @@ <h1 style="height: 100px; width: 200px;">Main Component</h1>
8181
(mouseenter)="isHovering = true"
8282
(mouseleave)="isHovering = false"
8383
style="width: 50px; height: 50px; background: hotpink;"></div>
84+
<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)