Skip to content

Commit

Permalink
test: create general purpose vscode operator function & finish select…
Browse files Browse the repository at this point in the history
… azure tenant test case (#225)

* feat: create general purpose vscode operator function & finish select azure tenant test case

* test: select tenant use env

* test: use dot env

* test: add codecov ignore test folder

* test: add codecov ignore test folder

* test: add codecov ignore test folder

* test: add codecov ignore test folder

* test: debug

* test: debug

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: yaml test

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: create general purpose vscode operator function & finish select azure tenant test case

* test: change case name

---------

Co-authored-by: Ivan_Chen <[email protected]>
  • Loading branch information
ayachensiyuan and Ivan_Chen authored Aug 6, 2024
1 parent 7974b72 commit 79b8a68
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 28 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@
"copy-webpack-plugin": "^12.0.2",
"copyfiles": "^2.4.1",
"crypto-js": "^3.1.2",
"dotenv": "^16.4.5",
"eslint": "^8.50.0",
"eslint-plugin-header": "^3.1.1",
"glob": "^10.3.3",
Expand Down
19 changes: 19 additions & 0 deletions src/test/e2e/baseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { Database } from 'sqlite3';
import { TestENV } from "./utils/constants";
export { expect } from '@playwright/test';

export type TestOptions = {
Expand Down Expand Up @@ -50,6 +51,7 @@ export const test = base.extend<TestFixtures>({
});
const workbox = await electronApp.firstWindow();
await insertToDB(args[1].split('=')[1]);
await setupTenantAndSub(args[1].split('=')[1]);
await workbox.context().tracing.start({ screenshots: true, snapshots: true, title: test.info().title });
await use(workbox);
const tracePath = test.info().outputPath('trace.zip');
Expand Down Expand Up @@ -141,3 +143,20 @@ function getJsonFromBytes(base64String: string) {
const byteArrayArray = Array.from(byteArray);
return byteArrayArray;
}

async function setupTenantAndSub(path) {
const setJson = `${path}/User/settings.json`;
await fs.ensureFile(setJson);
let data = {
"azure-api-center.selectedSubscriptions": [
`${TestENV.AZURE_TENANT_ID}/${TestENV.AZURE_TENANT_ID_2}`
],
"azure-api-center.tenant": {
"name": TestENV.AZURE_TENANT_NAME,
"id": TestENV.AZURE_TENANT_ID
}
};

let dataString = JSON.stringify(data);
await fs.writeFile(setJson, dataString, { encoding: 'utf8', flag: 'w' });
}
37 changes: 19 additions & 18 deletions src/test/e2e/tests/registerApiViaCICD.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { expect, test } from '../baseTest';
import { APICenter, Timeout, VSCode } from '../utils/constants';
import VscodeOperator from '../utils/vscodeOperator';

test('trigger generateAPIviaCICD with Azure DevOps', async ({ workbox }) => {
await workbox.waitForTimeout(Timeout.PREPARE_TEST);
// wait API Center extension installed on VS Code.
await workbox.getByRole("tab", { name: "API Center" }).isVisible();
await workbox.getByRole("tab", { name: "API Center" }).locator('a').click();
expect(await VscodeOperator.isSideTabItemExist(workbox, "API Center")).toBeTruthy();
await VscodeOperator.activeSideTab(workbox, VSCode.TAB_API_CENTER);
// trigger command palette.
await workbox.keyboard.press('Control+Shift+KeyP');
await workbox.getByRole("combobox", { name: "INPUT" }).fill('>Azure API Center: Register API');
await workbox.getByRole('listbox').first().press('Enter');
await VscodeOperator.execCommandInCommandPalette(workbox, APICenter.REGISTER_API);
// select the first option.
await workbox.getByRole('option', { name: "CI/CD" }).locator('a').click();
await VscodeOperator.selectOptionByName(workbox, APICenter.CI_CD);
// select the next option.
await workbox.getByRole('option', { name: 'Azure DevOps' }).locator('a').click();
await workbox.getByRole("tab", { name: "Explorer" }).locator('a').click();
await VscodeOperator.selectOptionByName(workbox, APICenter.AZURE_DEVOPS);
await VscodeOperator.activeSideTab(workbox, VSCode.TAB_EXPLORER);
// check result.
await expect(workbox.getByRole('treeitem', { name: 'register-api.yml' })).toHaveCount(1);
expect(await VscodeOperator.isSideTabItemExist(workbox, "register-api.yml")).toBeTruthy();
});

test('trigger generateAPIviaCICD with GitHub', async ({ workbox }) => {
await workbox.waitForTimeout(Timeout.PREPARE_TEST);
// wait API Center extension installed on VS Code.
await workbox.getByRole("tab", { name: "API Center" }).isVisible();
await workbox.getByRole("tab", { name: "API Center" }).locator('a').click();
expect(await VscodeOperator.isSideTabItemExist(workbox, "API Center")).toBeTruthy();
await VscodeOperator.activeSideTab(workbox, VSCode.TAB_API_CENTER);
// trigger command palette.
await workbox.keyboard.press('Control+Shift+KeyP');
await workbox.getByRole("combobox", { name: "INPUT" }).fill('>Azure API Center: Register API');
await workbox.getByRole('listbox').first().press('Enter');
await VscodeOperator.execCommandInCommandPalette(workbox, APICenter.REGISTER_API);
// select the first option.
await workbox.getByRole('option', { name: "CI/CD" }).locator('a').click();
await VscodeOperator.selectOptionByName(workbox, APICenter.CI_CD);
// select the next option.
await workbox.getByRole('option', { name: 'GitHub' }).locator('a').click();
await workbox.getByRole("tab", { name: "Explorer" }).locator('a').click();
await VscodeOperator.selectOptionByName(workbox, APICenter.GITHUB);
await VscodeOperator.activeSideTab(workbox, VSCode.TAB_EXPLORER);
// check result.
await expect(workbox.getByRole('treeitem', { name: 'register-api.yml' })).toHaveCount(1);
expect(await VscodeOperator.isSideTabItemExist(workbox, "register-api.yml")).toBeTruthy();
});
10 changes: 0 additions & 10 deletions src/test/e2e/tests/selectAzureTenant.test.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/test/e2e/tests/validateSubscription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { expect, test } from '../baseTest';
import { APICenter, TestENV, Timeout, VSCode } from '../utils/constants';
import VscodeOperator from '../utils/vscodeOperator';

test('validate subscription', async ({ workbox }) => {
await workbox.waitForTimeout(Timeout.PREPARE_TEST);
// wait API Center extension installed on VS Code.
expect(await VscodeOperator.isSideTabItemExist(workbox, "API Center")).toBeTruthy();
await VscodeOperator.activeSideTab(workbox, VSCode.TAB_API_CENTER, Timeout.PREPARE_EXT);
expect(await VscodeOperator.isTreeItemExist(workbox, "Teams Cloud - E2E Testing with TTL = 1 Days")).toBeTruthy();
});
50 changes: 50 additions & 0 deletions src/test/e2e/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import dotenv from "dotenv";
dotenv.config();
export class Timeout {
public static readonly CLICK_WAIT = 1000;
public static readonly PREPARE_TEST = 5000;
public static readonly PREPARE_EXT = 10000;
public static readonly SHORT_WAIT = 5000;
}

export class VSCode {
// cmd palette
public static readonly CMD_PALETTE_KEY = "F1";
public static readonly CMD_PALETTE = "combobox";
public static readonly CMD_PALETTE_OPTION = "option";
public static readonly CMD_PALETTE_LIST = "listbox";
// side tabs
public static readonly SIDE_TAB = "tab";
public static readonly TAB_EXPLORER = "Explorer";
public static readonly TAB_API_CENTER = "API Center";
// tree items
public static readonly TREE_ITEM = "treeitem";
// elements
public static readonly INPUT = "INPUT";
public static readonly LINK = "a";
// keys
public static readonly ENTER = "Enter";
}

export class APICenter {
// commands
public static readonly REGISTER_API = ">Azure API Center: Register API";
// tree items
public static readonly SELECT_TENANT = "Select tenant...";
public static readonly SELECT_SUBS = "Select Subscriptions...";
// cicd
public static readonly CI_CD = "CI/CD";
public static readonly AZURE_DEVOPS = "Azure DevOps";
public static readonly GITHUB = "GitHub";
public static readonly REGISTER_API_YML = "register-api.yml";
}

export class TestENV {
public static readonly AZURE_TENANT_NAME = process.env["AZURE_TENANT_NAME"] || "Microsoft";
public static readonly AZURE_TENANT_ID = process.env["AZURE_TENANT_ID"] || "72f988bf-86f1-41af-91ab-2d7cd011db47";
public static readonly AZURE_SUBSCRIPTION_NAME = process.env["AZURE_SUBSCRIPTION_NAME"] || "Teams Cloud - E2E Testing with TTL = 1 Days";
public static readonly AZURE_TENANT_ID_2 = process.env["AZURE_TENANT_ID"] || "af46c703-f714-4f4c-af42-835a673c2b13";
}
59 changes: 59 additions & 0 deletions src/test/e2e/utils/vscodeOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { Page } from '@playwright/test';
import { Timeout, VSCode } from '../utils/constants';

export default class VscodeOperator {
static async execCommandInCommandPalette(page: Page, command: string) {
await page.keyboard.press(VSCode.CMD_PALETTE_KEY);
await page.waitForTimeout(Timeout.CLICK_WAIT);
const cmdPalette = await VscodeOperator.getCMDPalette(page);
await cmdPalette.fill(command);
await page.waitForTimeout(Timeout.CLICK_WAIT);
await page.getByRole(VSCode.CMD_PALETTE_LIST).first().press(VSCode.ENTER);
await page.waitForTimeout(Timeout.CLICK_WAIT);
}

static async getCMDPalette(page: Page) {
return page.getByRole(VSCode.CMD_PALETTE, { name: VSCode.INPUT });
}

static async selectOptionByName(page: Page, option: string) {
await page.getByRole(VSCode.CMD_PALETTE_OPTION, { name: option }).locator(VSCode.LINK).click();
await page.waitForTimeout(Timeout.CLICK_WAIT);
}

static async selectOptionByIndex(page: Page, index: number) {
await page.getByRole(VSCode.CMD_PALETTE_OPTION).nth(index).locator(VSCode.LINK).click();
await page.waitForTimeout(Timeout.CLICK_WAIT);
}

static async activeSideTab(page: Page, tabName: string, timeout = Timeout.CLICK_WAIT) {
await page.getByRole(VSCode.SIDE_TAB, { name: tabName }).locator(VSCode.LINK).click();
await page.waitForTimeout(timeout);
}

static async isSideTabItemExist(page: Page, tabName: string) {
return await page.getByRole(VSCode.SIDE_TAB, { name: tabName }).isVisible();
}

static async isTreeItemExist(page: Page, treeItemName: string) {
return await page.getByRole(VSCode.TREE_ITEM, { name: treeItemName }).isVisible();
}

static async clickTreeItem(page: Page, treeItemName: string) {
await page.getByRole(VSCode.TREE_ITEM, { name: treeItemName }).locator(VSCode.LINK).click();
await page.waitForTimeout(Timeout.CLICK_WAIT);
}

static async getCheckallCheckbox(page: Page) {
return await page.waitForSelector('input.quick-input-check-all[type="checkbox"]', { timeout: Timeout.SHORT_WAIT });
}

static async checkallCheckbox(page: Page) {
await (await VscodeOperator.getCheckallCheckbox(page)).check();
await page.waitForTimeout(Timeout.CLICK_WAIT);
await (await VscodeOperator.getCMDPalette(page)).press(VSCode.ENTER);
await page.waitForTimeout(Timeout.CLICK_WAIT);
}
}

0 comments on commit 79b8a68

Please sign in to comment.