|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import { Workbench, By, WebviewView, WebElement } from 'vscode-extension-tester' |
| 6 | +import { until } from 'selenium-webdriver' |
| 7 | + |
| 8 | +describe('Amazon Q E2E UI Test', function () { |
| 9 | + // need this timeout because Amazon Q takes awhile to load |
| 10 | + |
| 11 | + // need this timeout |
| 12 | + this.timeout(150000) |
| 13 | + let webviewView: WebviewView |
| 14 | + let workbench: Workbench |
| 15 | + before(async function () { |
| 16 | + /* TO-DO |
| 17 | + possibly before the workbench executes Amazon Q: Open Chat, we can make sure that all the tabs are closed first*/ |
| 18 | + workbench = new Workbench() |
| 19 | + await workbench.executeCommand('Amazon Q: Open Chat') |
| 20 | + |
| 21 | + // need this timeout |
| 22 | + await new Promise((resolve) => setTimeout(resolve, 5000)) |
| 23 | + webviewView = new WebviewView() |
| 24 | + await webviewView.switchToFrame() |
| 25 | + |
| 26 | + const selectableItems = await waitForElement(webviewView, By.css('.selectable-item'), true) |
| 27 | + if (selectableItems.length === 0) { |
| 28 | + throw new Error('No selectable login options found') |
| 29 | + } |
| 30 | + |
| 31 | + const companyItem = await findItemByText(selectableItems, 'Company account') |
| 32 | + await companyItem.click() |
| 33 | + const signInContinue = await webviewView.findWebElement(By.css('#connection-selection-continue-button')) |
| 34 | + await signInContinue.click() |
| 35 | + const startUrlInput = await webviewView.findWebElement(By.id('startUrl')) |
| 36 | + await startUrlInput.clear() |
| 37 | + await startUrlInput.sendKeys('https://amzn.awsapps.com/start') |
| 38 | + const UrlContinue = await webviewView.findWebElement(By.css('button.continue-button.topMargin')) |
| 39 | + await UrlContinue.click() |
| 40 | + console.log('Waiting for manual authentication...') |
| 41 | + // need this timeout |
| 42 | + await new Promise((resolve) => setTimeout(resolve, 12000)) |
| 43 | + console.log('Manual authentication should be done') |
| 44 | + await webviewView.switchBack() |
| 45 | + |
| 46 | + // AFTER AUTHENTICATION WE MUST RELOAD THE WEBVIEW BECAUSE MULTIPLE WEVIEWS CANNOT BE READ AT THE SAME TIME |
| 47 | + const editorView = workbench.getEditorView() |
| 48 | + console.log('editorview successfully created') |
| 49 | + await editorView.closeAllEditors() |
| 50 | + console.log('Closed all editors') |
| 51 | + webviewView = new WebviewView() |
| 52 | + console.log('Reopened webview view') |
| 53 | + await webviewView.switchToFrame() |
| 54 | + }) |
| 55 | + |
| 56 | + after(async () => { |
| 57 | + /* |
| 58 | + mynah-tabs-container is the css that contains all the mynah ui tabs |
| 59 | + inside that there are two spans that have key values |
| 60 | + inside those spans there is a div with the css mynah-tab-item-label |
| 61 | + and finally INSIDE THAT there is a button with the css mynah-tabs-close-button, we need to click that button and close all the tabs after the test is done |
| 62 | +
|
| 63 | + Logic: |
| 64 | + Find all the tahs by looking for the close buttons and then close them one by one. To check if all the tabs are closed, we can check if the mynah-tabs-container is empty. |
| 65 | + */ |
| 66 | + try { |
| 67 | + const closeButtons = await webviewView.findWebElements(By.css('.mynah-tabs-close-button')) |
| 68 | + |
| 69 | + for (const button of closeButtons) { |
| 70 | + await button.click() |
| 71 | + await new Promise((resolve) => setTimeout(resolve, 500)) |
| 72 | + } |
| 73 | + |
| 74 | + // double check that all tabs are closed by checking if the mynah-tabs-container is empty |
| 75 | + const tabsContainer = await webviewView.findWebElements(By.css('.mynah-tabs-container')) |
| 76 | + if ( |
| 77 | + tabsContainer.length === 0 || |
| 78 | + (await tabsContainer[0].findElements(By.css('.mynah-tab-item-label'))).length === 0 |
| 79 | + ) { |
| 80 | + console.log('All chat tabs successfully closed') |
| 81 | + } |
| 82 | + } catch (error) { |
| 83 | + console.log('Error closing tabs:', error) |
| 84 | + } |
| 85 | + await webviewView.switchBack() |
| 86 | + }) |
| 87 | + |
| 88 | + it('Chat Prompt Test', async () => { |
| 89 | + const chatInput = await waitForElement(webviewView, By.css('.mynah-chat-prompt-input')) |
| 90 | + await chatInput.sendKeys('Hello, Amazon Q!') |
| 91 | + const sendButton = await waitForElement(webviewView, By.css('.mynah-chat-prompt-button')) |
| 92 | + await sendButton.click() |
| 93 | + const responseReceived = await waitForChatResponse(webviewView) |
| 94 | + if (!responseReceived) { |
| 95 | + throw new Error('Chat response not received within timeout') |
| 96 | + } |
| 97 | + |
| 98 | + console.log('Chat response detected successfully') |
| 99 | + }) |
| 100 | + |
| 101 | + // Helper to wait for ui elements to load, utilizes typescript function overloading to account for all possible edge cases |
| 102 | + async function waitForElement( |
| 103 | + webview: WebviewView, |
| 104 | + locator: By, |
| 105 | + multiple: true, |
| 106 | + timeout?: number |
| 107 | + ): Promise<WebElement[]> |
| 108 | + async function waitForElement( |
| 109 | + webview: WebviewView, |
| 110 | + locator: By, |
| 111 | + multiple?: false, |
| 112 | + timeout?: number |
| 113 | + ): Promise<WebElement> |
| 114 | + async function waitForElement( |
| 115 | + webview: WebviewView, |
| 116 | + locator: By, |
| 117 | + multiple = false, |
| 118 | + timeout = 15000 |
| 119 | + ): Promise<WebElement | WebElement[]> { |
| 120 | + const driver = webview.getDriver() |
| 121 | + await driver.wait(until.elementsLocated(locator), timeout) |
| 122 | + return multiple ? await webview.findWebElements(locator) : await webview.findWebElement(locator) |
| 123 | + } |
| 124 | + |
| 125 | + // Helper to find item by text content |
| 126 | + async function findItemByText(items: WebElement[], text: string) { |
| 127 | + for (const item of items) { |
| 128 | + const titleDivs = await item.findElements(By.css('.title')) |
| 129 | + for (const titleDiv of titleDivs) { |
| 130 | + const titleText = await titleDiv.getText() |
| 131 | + if (titleText?.trim().startsWith(text)) { |
| 132 | + return item |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + throw new Error(`Item with text "${text}" not found`) |
| 137 | + } |
| 138 | + |
| 139 | + /* My Idea: Basically the conversation container's css is .mynah-chat-items-conversation-container |
| 140 | + Instead of looking for a specific message like how we look for other elements in the test, |
| 141 | + I can check how many elements there are in our specific conversation container. If there is 2 elements, |
| 142 | + we can assume that the chat response has been generated. The challenge is, we must grab the latest |
| 143 | + conversation container, as there can be multiple conversations in the webview. */ |
| 144 | + async function waitForChatResponse(webview: WebviewView, timeout = 15000): Promise<boolean> { |
| 145 | + const startTime = Date.now() |
| 146 | + |
| 147 | + while (Date.now() - startTime < timeout) { |
| 148 | + const conversationContainers = await webview.findWebElements( |
| 149 | + By.css('.mynah-chat-items-conversation-container') |
| 150 | + ) |
| 151 | + |
| 152 | + if (conversationContainers.length > 0) { |
| 153 | + const latestContainer = conversationContainers[conversationContainers.length - 1] |
| 154 | + |
| 155 | + const chatItems = await latestContainer.findElements(By.css('*')) |
| 156 | + |
| 157 | + if (chatItems.length >= 2) { |
| 158 | + return true |
| 159 | + } |
| 160 | + } |
| 161 | + await new Promise((resolve) => setTimeout(resolve, 500)) |
| 162 | + } |
| 163 | + |
| 164 | + return false |
| 165 | + } |
| 166 | +}) |
0 commit comments