2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
+ import { Workbench , By , WebviewView , WebElement } from 'vscode-extension-tester'
6
+ import { until } from 'selenium-webdriver'
5
7
6
- import { Workbench , By , EditorView , WebviewView , WebElement } from 'vscode-extension-tester'
7
-
8
- describe ( 'Amazon Q Login Flow' , function ( ) {
8
+ describe ( 'Amazon Q E2E UI Test' , function ( ) {
9
9
// need this timeout because Amazon Q takes awhile to load
10
- this . timeout ( 30000 )
11
- let webviewView : WebviewView
12
10
13
- // NOTE: I tested all the timeouts and they are necessary for the webview to load properly
11
+ // need this timeout
12
+ this . timeout ( 150000 )
13
+ let webviewView : WebviewView
14
+ let workbench : Workbench
14
15
before ( async function ( ) {
15
- const workbench = new Workbench ( )
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 ( )
16
19
await workbench . executeCommand ( 'Amazon Q: Open Chat' )
17
20
18
- await new Promise ( ( resolve ) => setTimeout ( resolve , 15000 ) )
21
+ // need this timeout
22
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) )
19
23
webviewView = new WebviewView ( )
20
24
await webviewView . switchToFrame ( )
21
- } )
22
25
23
- after ( async ( ) => {
24
- await webviewView . switchBack ( )
25
- try {
26
- await new EditorView ( ) . closeAllEditors ( )
27
- } catch { }
28
- } )
29
-
30
- it ( 'Should click through the Amazon Q login screen' , async ( ) => {
31
- // Select company account option
32
- const selectableItems = await webviewView . findWebElements ( By . css ( '.selectable-item' ) )
33
- console . log ( typeof selectableItems )
26
+ const selectableItems = await waitForElement ( webviewView , By . css ( '.selectable-item' ) , true )
34
27
if ( selectableItems . length === 0 ) {
35
28
throw new Error ( 'No selectable login options found' )
36
29
}
37
30
38
- // Find and click company account
39
31
const companyItem = await findItemByText ( selectableItems , 'Company account' )
40
32
await companyItem . click ( )
41
- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
42
-
43
- // Click first continue button
44
33
const signInContinue = await webviewView . findWebElement ( By . css ( '#connection-selection-continue-button' ) )
45
34
await signInContinue . click ( )
46
- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
47
-
48
- // Enter start URL
49
35
const startUrlInput = await webviewView . findWebElement ( By . id ( 'startUrl' ) )
50
36
await startUrlInput . clear ( )
51
37
await startUrlInput . sendKeys ( 'https://amzn.awsapps.com/start' )
52
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
53
-
54
- // Click second continue button
55
38
const UrlContinue = await webviewView . findWebElement ( By . css ( 'button.continue-button.topMargin' ) )
56
39
await UrlContinue . click ( )
57
- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
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 ( )
58
54
} )
59
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
+
60
125
// Helper to find item by text content
61
126
async function findItemByText ( items : WebElement [ ] , text : string ) {
62
127
for ( const item of items ) {
@@ -70,4 +135,32 @@ describe('Amazon Q Login Flow', function () {
70
135
}
71
136
throw new Error ( `Item with text "${ text } " not found` )
72
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
+ }
73
166
} )
0 commit comments