From 542375d7bc1f806582d776115ea0450468275d9d Mon Sep 17 00:00:00 2001 From: Zbynek Cervinka Date: Sat, 3 Dec 2022 00:14:28 +0100 Subject: [PATCH] Create UI test for testing whether the autocompletion works Fixes #853 Signed-off-by: Zbynek Cervinka --- test/ui-test/Utilities.ts | 25 +++++++++++++++ test/ui-test/allTestsSuite.ts | 2 ++ test/ui-test/autocompletionTest.ts | 50 ++++++++++++++++++++++++++++++ test/ui-test/contentAssistTest.ts | 33 +++++++++----------- test/ui-test/customTagsTest.ts | 39 ++++++++++++++--------- test/ui-test/schemaIsSetTest.ts | 24 ++++---------- 6 files changed, 121 insertions(+), 52 deletions(-) create mode 100644 test/ui-test/Utilities.ts create mode 100644 test/ui-test/autocompletionTest.ts diff --git a/test/ui-test/Utilities.ts b/test/ui-test/Utilities.ts new file mode 100644 index 00000000..efc2382b --- /dev/null +++ b/test/ui-test/Utilities.ts @@ -0,0 +1,25 @@ +import os = require('os'); +import path = require('path'); +import { StatusBar, By, WebElement } from 'vscode-extension-tester'; + +/** + * @author Zbynek Cervinka + */ +export class Utilities { + public deleteFileInHomeDir(filename: string): void { + /* eslint-disable */ + const fs = require('fs'); + const homeDir = os.homedir(); + const pathtofile = path.join(homeDir, filename); + + if (fs.existsSync(pathtofile)) { + fs.rmSync(pathtofile, { recursive: true, force: true }); + } + } + + public async getSchemaLabel(text: string): Promise | undefined { + const statusbar = new StatusBar(); + const schemalabel = await statusbar.findElements(By.xpath('.//a[@aria-label="' + text + ', Select JSON Schema"]')); + return schemalabel[0]; + } +} diff --git a/test/ui-test/allTestsSuite.ts b/test/ui-test/allTestsSuite.ts index 0274d9c7..cba34338 100644 --- a/test/ui-test/allTestsSuite.ts +++ b/test/ui-test/allTestsSuite.ts @@ -2,10 +2,12 @@ import { extensionUIAssetsTest } from './extensionUITest'; import { contentAssistSuggestionTest } from './contentAssistTest'; import { customTagsTest } from './customTagsTest'; import { schemaIsSetTest } from './schemaIsSetTest'; +import { autocompletionTest } from './autocompletionTest'; describe('VSCode YAML - UI tests', () => { extensionUIAssetsTest(); contentAssistSuggestionTest(); customTagsTest(); schemaIsSetTest(); + autocompletionTest(); }); diff --git a/test/ui-test/autocompletionTest.ts b/test/ui-test/autocompletionTest.ts new file mode 100644 index 00000000..519ac00d --- /dev/null +++ b/test/ui-test/autocompletionTest.ts @@ -0,0 +1,50 @@ +import { expect } from 'chai'; +import { WebDriver, VSBrowser, Key, InputBox, TextEditor, WebElement } from 'vscode-extension-tester'; +import { Utilities } from './Utilities'; + +/** + * @author Zbynek Cervinka + */ +export function autocompletionTest(): void { + describe('Verify autocompletion completes what should be completed', () => { + it('Autocompletion works as expected', async function () { + this.timeout(30000); + + const driver: WebDriver = VSBrowser.instance.driver; + await driver.actions().sendKeys(Key.F1).perform(); + + let input = await InputBox.create(); + await input.setText('>new file'); + await input.confirm(); + await input.confirm(); + + await driver.actions().sendKeys(Key.chord(TextEditor.ctlKey, 's')).perform(); + input = await InputBox.create(); + await input.setText('~/kustomization.yaml'); + await input.confirm(); + + // wait until the schema is set and prepared + (await VSBrowser.instance.driver.wait(async () => { + this.timeout(10000); + const utils = new Utilities(); + return await utils.getSchemaLabel('kustomization.yaml'); + }, 10000)) as WebElement | undefined; + + await driver.actions().sendKeys('api').perform(); + await new TextEditor().toggleContentAssist(true); + await driver.actions().sendKeys(Key.ENTER).perform(); + + const editor = new TextEditor(); + const text = await editor.getText(); + + if (text != 'apiVersion: ') { + expect.fail("The 'apiVersion: ' string has not been autocompleted."); + } + }); + + afterEach(async function () { + const utils = new Utilities(); + utils.deleteFileInHomeDir('kustomization.yaml'); + }); + }); +} diff --git a/test/ui-test/contentAssistTest.ts b/test/ui-test/contentAssistTest.ts index 45c67d0d..22b7bde5 100644 --- a/test/ui-test/contentAssistTest.ts +++ b/test/ui-test/contentAssistTest.ts @@ -1,6 +1,6 @@ -import path = require('path'); -import os = require('os'); -import { WebDriver, VSBrowser, Key, InputBox, TextEditor, ContentAssist } from 'vscode-extension-tester'; +import { expect } from 'chai'; +import { WebDriver, VSBrowser, Key, InputBox, TextEditor, ContentAssist, WebElement } from 'vscode-extension-tester'; +import { Utilities } from './Utilities'; /** * @author Zbynek Cervinka @@ -23,35 +23,30 @@ export function contentAssistSuggestionTest(): void { await input.setText('~/kustomization.yaml'); await input.confirm(); - await delay(2000); - await driver.actions().sendKeys('api').perform(); + // wait until the schema is set and prepared + (await VSBrowser.instance.driver.wait(async () => { + this.timeout(10000); + const utils = new Utilities(); + return await utils.getSchemaLabel('kustomization.yaml'); + }, 10000)) as WebElement | undefined; + await driver.actions().sendKeys('api').perform(); const contentAssist: ContentAssist | void = await new TextEditor().toggleContentAssist(true); // find if an item with given label is present if (contentAssist instanceof ContentAssist) { const hasItem = await contentAssist.hasItem('apiVersion'); if (!hasItem) { - throw new Error("The 'apiVersion' string did not appear in the content assist's suggestion list."); + expect.fail("The 'apiVersion' string did not appear in the content assist's suggestion list."); } } else { - throw new Error("The 'apiVersion' string did not appear in the content assist's suggestion list."); + expect.fail("The 'apiVersion' string did not appear in the content assist's suggestion list."); } }); afterEach(async function () { - /* eslint-disable */ - const fs = require('fs'); - const homeDir = os.homedir(); - const pathtofile = path.join(homeDir, 'kustomization.yaml'); - - if (fs.existsSync(pathtofile)) { - fs.rmSync(pathtofile, { recursive: true, force: true }); - } + const utils = new Utilities(); + utils.deleteFileInHomeDir('kustomization.yaml'); }); }); } - -function delay(milliseconds: number): Promise { - return new Promise((resolve) => setTimeout(resolve, milliseconds)); -} diff --git a/test/ui-test/customTagsTest.ts b/test/ui-test/customTagsTest.ts index 538ca4b2..668d4e1c 100644 --- a/test/ui-test/customTagsTest.ts +++ b/test/ui-test/customTagsTest.ts @@ -1,6 +1,16 @@ -import path = require('path'); -import os = require('os'); -import { By, WebDriver, VSBrowser, Key, TextEditor, Workbench, InputBox, ContentAssist } from 'vscode-extension-tester'; +import { expect } from 'chai'; +import { + By, + WebDriver, + VSBrowser, + Key, + TextEditor, + Workbench, + InputBox, + ContentAssist, + WebElement, +} from 'vscode-extension-tester'; +import { Utilities } from './Utilities'; /** * @author Zbynek Cervinka @@ -29,10 +39,15 @@ export function customTagsTest(): void { await driver.actions().sendKeys(Key.chord(TextEditor.ctlKey, 's')).perform(); input = await InputBox.create(); - await input.setText('~/customTagsTestFile.yaml'); + await input.setText('~/kustomization.yaml'); await input.confirm(); - await delay(2000); + // wait until the schema is set and prepared + (await VSBrowser.instance.driver.wait(async () => { + this.timeout(30000); + const utils = new Utilities(); + return await utils.getSchemaLabel('kustomization.yaml'); + }, 30000)) as WebElement | undefined; await driver.actions().sendKeys('custom').perform(); const contentAssist: ContentAssist | void = await new TextEditor().toggleContentAssist(true); @@ -41,22 +56,16 @@ export function customTagsTest(): void { if (contentAssist instanceof ContentAssist) { const hasItem = await contentAssist.hasItem('customTag1'); if (!hasItem) { - throw new Error("The 'customTag1' custom tag did not appear in the content assist's suggestion list."); + expect.fail("The 'customTag1' custom tag did not appear in the content assist's suggestion list."); } } else { - throw new Error("The 'customTag1' custom tag did not appear in the content assist's suggestion list."); + expect.fail("The 'customTag1' custom tag did not appear in the content assist's suggestion list."); } }); afterEach(async function () { - /* eslint-disable */ - const fs = require('fs'); - const homeDir = os.homedir(); - const pathtofile = path.join(homeDir, 'customTagsTestFile.yaml'); - - if (fs.existsSync(pathtofile)) { - fs.rmSync(pathtofile, { recursive: true, force: true }); - } + const utils = new Utilities(); + utils.deleteFileInHomeDir('kustomization.yaml'); }); }); } diff --git a/test/ui-test/schemaIsSetTest.ts b/test/ui-test/schemaIsSetTest.ts index b7025057..d6882c42 100644 --- a/test/ui-test/schemaIsSetTest.ts +++ b/test/ui-test/schemaIsSetTest.ts @@ -1,6 +1,5 @@ -import path = require('path'); -import os = require('os'); -import { WebDriver, VSBrowser, Key, InputBox, TextEditor, StatusBar, By, WebElement } from 'vscode-extension-tester'; +import { WebDriver, VSBrowser, Key, InputBox, TextEditor, WebElement } from 'vscode-extension-tester'; +import { Utilities } from './Utilities'; /** * @author Zbynek Cervinka @@ -25,25 +24,14 @@ export function schemaIsSetTest(): void { (await VSBrowser.instance.driver.wait(async () => { this.timeout(10000); - return await getSchemaLabel({ text: 'kustomization.yaml' }); + const utils = new Utilities(); + return await utils.getSchemaLabel('kustomization.yaml'); }, 10000)) as WebElement | undefined; }); afterEach(async function () { - /* eslint-disable */ - const fs = require('fs'); - const homeDir = os.homedir(); - const pathtofile = path.join(homeDir, 'kustomization.yaml'); - - if (fs.existsSync(pathtofile)) { - fs.rmSync(pathtofile, { recursive: true, force: true }); - } + const utils = new Utilities(); + utils.deleteFileInHomeDir('kustomization.yaml'); }); }); } - -async function getSchemaLabel({ text }: { text: string; }): Promise | undefined { - const statusbar = new StatusBar(); - var schemalabel = await statusbar.findElements(By.xpath('.//a[@aria-label="' + text + ', Select JSON Schema"]')); - return schemalabel[0]; -}