Skip to content

Commit 50fdf5d

Browse files
authored
Merge pull request #2501 from microsoft/extract-uplicate-code-as-a-common-base-class
Extract duplicate code as a common base class
2 parents fe9ef5a + b148598 commit 50fdf5d

3 files changed

Lines changed: 51 additions & 58 deletions

File tree

test/smoke/suites/activation.test.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,20 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for details.
33

4-
import { Page } from "playwright";
5-
import { SmokeTestLogger } from "./helper/smokeTestLogger";
6-
import { app, screenshots } from "./main";
74
import assert = require("assert");
85
import { ElementHelper } from "./helper/elementHelper";
96
import { Constant } from "./helper/constants";
7+
import { BaseSmokeTest } from "./helper/baseSmokeTest";
8+
import { SmokeTestLogger } from "./helper/smokeTestLogger";
109
import { TimeoutConstants } from "./helper/timeoutConstants";
1110

1211
export function startExtensionActivationTests(): void {
1312
describe("ExtensionActivationTest", () => {
14-
async function initApp(): Promise<Page> {
15-
await app.launch();
16-
return app.getMainPage();
17-
}
18-
19-
async function dispose() {
20-
if (this.currentTest?.state === "failed") {
21-
SmokeTestLogger.info("Test failed, taking screenshot ...");
22-
await screenshots.takeScreenshots(
23-
this.currentTest.parent?.title || "Others",
24-
this.currentTest.title.replace(/\s+/g, "_"),
25-
);
26-
}
27-
try {
28-
SmokeTestLogger.info(`Dispose test: "${this.currentTest.title}" ...`);
29-
if (app) {
30-
await app.close();
31-
}
32-
} catch (error) {
33-
SmokeTestLogger.error(`Error while dispose: ${error}`);
34-
}
35-
}
36-
37-
afterEach(dispose);
13+
afterEach(BaseSmokeTest.dispose);
3814

3915
it("Verify extension is activated", async () => {
4016
let isActivited = false;
41-
await initApp();
17+
await BaseSmokeTest.initApp();
4218

4319
try {
4420
await ElementHelper.WaitElementSelectorVisible(

test/smoke/suites/cdpNodeVersionCompatibility.test.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,20 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for details.
33

4-
import { Page } from "playwright";
5-
import { SmokeTestLogger } from "./helper/smokeTestLogger";
6-
import { app, screenshots } from "./main";
74
import * as assert from "assert";
85
import { ComponentHelper } from "./helper/componentHelper";
96
import { ElementHelper } from "./helper/elementHelper";
107
import { Element } from "./helper/constants";
8+
import { BaseSmokeTest } from "./helper/baseSmokeTest";
9+
import { SmokeTestLogger } from "./helper/smokeTestLogger";
1110
import { TimeoutConstants } from "./helper/timeoutConstants";
1211

1312
export function startCDPNodeVersionCompatibilityTests(): void {
1413
describe("CDPNodeVersionCompatibilityTest", () => {
15-
async function initApp(): Promise<Page> {
16-
await app.launch();
17-
return app.getMainPage();
18-
}
19-
20-
async function dispose() {
21-
if (this.currentTest?.state === "failed") {
22-
SmokeTestLogger.info("Test failed, taking screenshot ...");
23-
await screenshots.takeScreenshots(
24-
this.currentTest.parent?.title || "Others",
25-
this.currentTest.title.replace(/\s+/g, "_"),
26-
);
27-
}
28-
try {
29-
SmokeTestLogger.info(`Dispose test: "${this.currentTest.title}" ...`);
30-
if (app) {
31-
await app.close();
32-
}
33-
} catch (error) {
34-
SmokeTestLogger.error(`Error while dispose: ${error}`);
35-
}
36-
}
37-
38-
afterEach(dispose);
14+
afterEach(BaseSmokeTest.dispose);
3915

4016
it("Verify Run and Debug tab is accessible for debugging operations", async () => {
41-
await initApp();
17+
await BaseSmokeTest.initApp();
4218

4319
await ComponentHelper.openRunAndDebugTab();
4420
await ElementHelper.WaitElementClassNameVisible(
@@ -50,7 +26,7 @@ export function startCDPNodeVersionCompatibilityTests(): void {
5026
});
5127

5228
it("Verify command palette can access debug-related commands", async () => {
53-
await initApp();
29+
await BaseSmokeTest.initApp();
5430

5531
await ComponentHelper.openCommandPalette();
5632
await ElementHelper.WaitElementClassNameVisible(
@@ -70,7 +46,7 @@ export function startCDPNodeVersionCompatibilityTests(): void {
7046
});
7147

7248
it("Verify React Native packager is available for debugging", async () => {
73-
await initApp();
49+
await BaseSmokeTest.initApp();
7450

7551
await ComponentHelper.getReactNativePackager();
7652
SmokeTestLogger.info("React Native packager is available for debugging");
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for details.
3+
4+
import { Page } from "playwright";
5+
import { SmokeTestLogger } from "./smokeTestLogger";
6+
import { app, screenshots } from "../main";
7+
8+
/**
9+
* Base class for smoke tests that provides common setup and teardown functionality
10+
*/
11+
export class BaseSmokeTest {
12+
/**
13+
* Initialize the application and return the main page
14+
*/
15+
public static async initApp(): Promise<Page> {
16+
await app.launch();
17+
return app.getMainPage();
18+
}
19+
20+
/**
21+
* Common dispose function to be called after each test
22+
* Takes screenshots on failure and closes the app
23+
*/
24+
public static async dispose(this: any): Promise<void> {
25+
if (this.currentTest?.state === "failed") {
26+
SmokeTestLogger.info("Test failed, taking screenshot ...");
27+
await screenshots.takeScreenshots(
28+
this.currentTest.parent?.title || "Others",
29+
this.currentTest.title.replace(/\s+/g, "_"),
30+
);
31+
}
32+
try {
33+
SmokeTestLogger.info(`Dispose test: "${this.currentTest.title}" ...`);
34+
if (app) {
35+
await app.close();
36+
}
37+
} catch (error) {
38+
SmokeTestLogger.error(`Error while dispose: ${error}`);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)