Skip to content

Commit 6df1268

Browse files
committed
build: fix webdriver tests (#28915)
Reworks the Webdriver tests to avoid the frequent timouts that we've been seeing. Working theory is that refreshing the page on each test and then waiting for Angular to stabizilize causes Webdriver to eventually time out. These changes switch to only loading the page once and resetting its state within the same session. (cherry picked from commit 971f83f)
1 parent 0c017a2 commit 6df1268

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ declare const kagekiri: {
4040
describe('WebDriverHarnessEnvironment', () => {
4141
let wd: webdriver.WebDriver;
4242

43-
async function getUrl(path: string) {
44-
await wd.get(`http://localhost:${port}${path}`);
45-
await waitForAngularReady(wd);
46-
}
47-
4843
async function piercingQueryFn(selector: string, root: () => webdriver.WebElement) {
4944
return wd.findElements(
5045
webdriver.By.js((s: string, r: Element) => kagekiri.querySelectorAll(s, r), selector, root()),
@@ -60,15 +55,21 @@ describe('WebDriverHarnessEnvironment', () => {
6055
.usingServer(process.env['WEB_TEST_WEBDRIVER_SERVER']!)
6156
.withCapabilities(webTestMetadata.capabilities)
6257
.build();
63-
await wd.manage().timeouts().implicitlyWait(0);
64-
});
6558

66-
afterAll(async () => {
67-
await wd.quit();
59+
// Ideally we would refresh the page and wait for Angular to stabilize on each test.
60+
// We don't do it, because it causes Webdriver to eventually time out. Instead we go to
61+
// the page once and reset the component state before the test (see `beforeEach` below).
62+
await wd.get(`http://localhost:${port}/component-harness`);
63+
await waitForAngularReady(wd);
6864
});
6965

7066
beforeEach(async () => {
71-
await getUrl('/component-harness');
67+
await wd.findElement({id: 'reset-state'}).click();
68+
await wd.sleep(500);
69+
});
70+
71+
afterAll(async () => {
72+
await wd.quit();
7273
});
7374

7475
describe('environment specific', () => {
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
1+
import {Component} from '@angular/core';
22
import {TestMainComponent} from '@angular/cdk/testing/tests';
33

44
@Component({
55
selector: 'component-harness-e2e',
6-
template: `<test-main></test-main>`,
7-
encapsulation: ViewEncapsulation.None,
8-
changeDetection: ChangeDetectionStrategy.OnPush,
6+
template: `
7+
<button id="reset-state" (click)="reset()">Reset state</button>
8+
9+
@if (isShown) {
10+
<test-main></test-main>
11+
}
12+
`,
913
standalone: true,
1014
imports: [TestMainComponent],
1115
})
12-
export class ComponentHarnessE2e {}
16+
export class ComponentHarnessE2e {
17+
protected isShown = true;
18+
19+
/**
20+
* Resets the test component state without the need to refresh the page.
21+
* Used by Webdriver integration tests.
22+
*/
23+
protected reset(): void {
24+
this.isShown = false;
25+
setTimeout(() => (this.isShown = true), 100);
26+
}
27+
}

src/e2e-app/components/e2e-app/e2e-app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, ViewEncapsulation} from '@angular/core';
2-
import {NgFor, NgIf} from '@angular/common';
32
import {MatListModule} from '@angular/material/list';
43
import {RouterLink, RouterOutlet} from '@angular/router';
54

@@ -8,7 +7,7 @@ import {RouterLink, RouterOutlet} from '@angular/router';
87
templateUrl: 'e2e-app.html',
98
encapsulation: ViewEncapsulation.None,
109
standalone: true,
11-
imports: [MatListModule, NgIf, NgFor, RouterLink, RouterOutlet],
10+
imports: [MatListModule, RouterLink, RouterOutlet],
1211
})
1312
export class E2eApp {
1413
showLinks = false;

0 commit comments

Comments
 (0)