-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathIde.ts
393 lines (305 loc) · 17.3 KB
/
Ide.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import axios from 'axios';
import { DriverHelper } from '../../../utils/DriverHelper';
import { injectable, inject } from 'inversify';
import { CLASSES } from '../../../inversify.types';
import { TestConstants } from '../../../TestConstants';
import { By, error } from 'selenium-webdriver';
import { Logger } from '../../../utils/Logger';
import { NotificationCenter } from './NotificationCenter';
import { TimeoutConstants } from '../../../TimeoutConstants';
export enum LeftToolbarButton {
Explorer = 'Explorer',
Git = 'Git',
Debug = 'Debug',
Openshift = 'OpenShift'
}
@injectable()
export class Ide {
public static readonly EXPLORER_BUTTON_ID: string = 'shell-tab-explorer-view-container';
public static readonly SELECTED_EXPLORER_BUTTON_CSS: string = 'li#shell-tab-explorer-view-container.theia-mod-active';
public static readonly THEIA_EDITOR_CSS: string = '#theia-main-content-panel';
public static readonly SELECTED_GIT_BUTTON_XPATH: string = '(//ul[@class=\'p-TabBar-content\']//li[@title=\'Git\' and contains(@class, \'p-mod-current\')])[1]';
private static readonly TOP_MENU_PANEL_CSS: string = '#theia-app-shell #theia-top-panel .p-MenuBar-content';
private static readonly LEFT_CONTENT_PANEL_CSS: string = '#theia-left-content-panel';
private static readonly PRELOADER_CSS: string = '.theia-preload';
private static readonly IDE_IFRAME_CSS: string = 'iframe#ide-iframe';
constructor(
@inject(CLASSES.DriverHelper) private readonly driverHelper: DriverHelper,
@inject(CLASSES.NotificationCenter) private readonly notificationCenter: NotificationCenter
) { }
/**
* @deprecated Method deprecated. Iframe is not available, Replace it with waitForEditor() or waitIde() method incase for conditional wait
* @see Ide.waitForEditor()
*/
async waitAndSwitchToIdeFrame(timeout: number = TimeoutConstants.TS_SELENIUM_START_WORKSPACE_TIMEOUT) {
Logger.debug('Ide.waitAndSwitchToIdeFrame');
try {
await this.driverHelper.waitAndSwitchToFrame(By.css(Ide.IDE_IFRAME_CSS), timeout);
} catch (err) {
if (err instanceof error.StaleElementReferenceError) {
Logger.warn('StaleElementException occurred during waiting for IDE. Sleeping for 2 secs and retrying.');
await this.driverHelper.wait(2000);
try {
await this.driverHelper.waitAndSwitchToFrame(By.css(Ide.IDE_IFRAME_CSS), timeout);
} catch (err) {
if (err instanceof error.TimeoutError) {
Logger.warn(`Iframe is not available even after ${timeout} milliseconds, checking for visibility of #theia-main-content-panel.`);
await this.driverHelper.isVisible(By.css('#theia-main-content-panel'));
return;
}
throw err;
}
return;
}
if (err instanceof error.TimeoutError) {
Logger.warn(`Iframe is not available even after ${timeout} milliseconds, checking for visibility of #theia-main-content-panel.`);
await this.driverHelper.isVisible(By.css('#theia-main-content-panel'));
}
Logger.error(`Switching to IDE frame failed.`);
throw err;
}
}
async waitForEditor(timeout: number = TimeoutConstants.TS_SELENIUM_START_WORKSPACE_TIMEOUT) {
Logger.debug('Ide.waitForEditor');
try {
await this.driverHelper.waitVisibility(By.css(Ide.THEIA_EDITOR_CSS), timeout);
} catch (err) {
Logger.warn(`Editor is not displayed even after ${timeout} milliseconds.`);
throw err;
}
}
async waitNotification(notificationText: string, timeout: number = TimeoutConstants.TS_NOTIFICATION_CENTER_TIMEOUT) {
Logger.debug(`Ide.waitNotification "${notificationText}"`);
const notificationLocator: By = By.xpath(this.getNotificationXpathLocator(notificationText));
await this.driverHelper.waitVisibility(notificationLocator, timeout);
}
async waitTaskExitCodeNotificationBoolean(exitCode: string, timeout: number = TimeoutConstants.TS_SELENIUM_WAIT_TASK_EXIT_CODE_TIMEOUT): Promise<boolean> {
Logger.debug(`Ide.waitTaskExitCodeNotification "has exited with code ${exitCode}."`);
const exitCodeNotificationLocator: By = By.xpath(this.getNotificationXpathLocator(`has exited with code`));
const notificationLocator: By = By.xpath(this.getNotificationXpathLocator(`has exited with code ${exitCode}.`));
Logger.info(`Ide.waitTaskExitCodeNotification waiting for any exit code notification.`);
try {
await this.driverHelper.waitVisibility(exitCodeNotificationLocator, timeout);
} catch (err) {
if (err instanceof error.TimeoutError) {
Logger.error(`Ide.waitTaskExitCodeNotificationBoolean wait for notification timed out after ${timeout}.`);
throw err;
}
Logger.error(`Waiting for task notification failed.`);
throw err;
}
Logger.info(`Ide.waitTaskExitCodeNotification checking for correct exit core:${exitCode}`);
return await this.driverHelper.waitVisibilityBoolean(notificationLocator, 1, 1000);
}
async waitNotificationAndClickOnButton(notificationText: string,
buttonText: string,
timeout: number = TimeoutConstants.TS_NOTIFICATION_CENTER_TIMEOUT) {
Logger.debug(`Ide.waitNotificationAndClickOnButton "${notificationText}" buttonText: "${buttonText}"`);
await this.driverHelper.getDriver().wait(async () => {
await this.waitNotification(notificationText, timeout);
await this.clickOnNotificationButton(notificationText, buttonText);
try {
await this.waitNotificationDisappearance(notificationText);
return true;
} catch (err) {
if (!(err instanceof error.TimeoutError)) {
throw err;
}
console.log(`After clicking on "${buttonText}" button of the notification with text "${notificationText}" \n` +
'it is still visible (issue #14121), try again.');
await this.driverHelper.wait(TestConstants.TS_SELENIUM_DEFAULT_POLLING);
}
}, timeout);
}
async waitNotificationAndConfirm(notificationText: string, timeout: number = TimeoutConstants.TS_NOTIFICATION_CENTER_TIMEOUT) {
Logger.debug(`Ide.waitNotificationAndConfirm "${notificationText}"`);
await this.waitNotificationAndClickOnButton(notificationText, 'yes', timeout);
}
async waitNotificationAndOpenLink(notificationText: string, timeout: number) {
Logger.debug(`Ide.waitNotificationAndOpenLink "${notificationText}"`);
await this.waitNotification(notificationText, timeout);
await this.waitNotificationAndClickOnButton(notificationText, 'Open Link', timeout);
}
async isNotificationPresent(notificationText: string): Promise<boolean> {
Logger.debug(`Ide.isNotificationPresent "${notificationText}"`);
const notificationLocator: By = By.xpath(this.getNotificationXpathLocator(notificationText));
return await this.driverHelper.waitVisibilityBoolean(notificationLocator);
}
async waitNotificationDisappearance(notificationText: string,
attempts: number = TestConstants.TS_SELENIUM_DEFAULT_ATTEMPTS,
polling: number = TestConstants.TS_SELENIUM_DEFAULT_POLLING) {
Logger.debug(`Ide.waitNotificationDisappearance "${notificationText}"`);
const notificationLocator: By = By.xpath(this.getNotificationXpathLocator(notificationText));
await this.driverHelper.waitDisappearance(notificationLocator, attempts, polling);
}
async clickOnNotificationButton(notificationText: string, buttonText: string) {
Logger.debug(`Ide.clickOnNotificationButton "${notificationText}" buttonText: "${buttonText}"`);
const yesButtonLocator: string = `//div[@class='theia-notification-list']//span[contains(.,'${notificationText}')]/parent::div/parent::div/parent::div/div[@class='theia-notification-list-item-content-bottom']//div[@class='theia-notification-buttons']//button[text()='${buttonText}'] `;
await this.driverHelper.waitAndClick(By.xpath(yesButtonLocator));
}
async waitWorkspaceAndIde(timeout: number = TimeoutConstants.TS_SELENIUM_START_WORKSPACE_TIMEOUT) {
Logger.debug('Ide.waitWorkspaceAndIde');
await this.waitForEditor(timeout);
await this.waitIde(timeout);
}
async waitIde(timeout: number = TimeoutConstants.TS_SELENIUM_START_WORKSPACE_TIMEOUT) {
Logger.debug('Ide.waitIde');
const mainIdeParts: Array<By> = [By.css(Ide.TOP_MENU_PANEL_CSS), By.css(Ide.LEFT_CONTENT_PANEL_CSS), By.id(Ide.EXPLORER_BUTTON_ID)];
for (const idePartLocator of mainIdeParts) {
try {
await this.driverHelper.waitVisibility(idePartLocator, timeout);
} catch (err) {
if (err instanceof error.NoSuchWindowError) {
await this.driverHelper.waitVisibility(idePartLocator, timeout);
return;
}
if (err instanceof error.TimeoutError) {
Logger.error(`Waiting for ${idePartLocator} timeouted after ${timeout} timeout.`);
throw err;
}
Logger.error(`Waiting for ${idePartLocator} failed.`);
throw err;
}
}
}
async waitLeftToolbarButton(buttonTitle: LeftToolbarButton, timeout: number = TimeoutConstants.TS_SELENIUM_TOOLBAR_TIMEOUT) {
Logger.debug('Ide.waitLeftToolbarButton');
const buttonLocator: By = this.getLeftToolbarButtonLocator(buttonTitle);
await this.driverHelper.waitVisibility(buttonLocator, timeout);
}
async waitAndClickLeftToolbarButton(buttonTitle: LeftToolbarButton, timeout: number = TimeoutConstants.TS_SELENIUM_TOOLBAR_TIMEOUT) {
Logger.debug('Ide.waitAndClickLeftToolbarButton');
const buttonLocator: By = this.getLeftToolbarButtonLocator(buttonTitle);
await this.driverHelper.waitAndClick(buttonLocator, timeout);
}
async waitTopMenuPanel(timeout: number = TimeoutConstants.TS_SELENIUM_TOOLBAR_TIMEOUT) {
Logger.debug('Ide.waitTopMenuPanel');
await this.driverHelper.waitVisibility(By.css(Ide.TOP_MENU_PANEL_CSS), timeout);
}
async waitLeftContentPanel(timeout: number = TimeoutConstants.TS_SELENIUM_TOOLBAR_TIMEOUT) {
Logger.debug('Ide.waitLeftContentPanel');
await this.driverHelper.waitVisibility(By.css(Ide.LEFT_CONTENT_PANEL_CSS), timeout);
}
async waitPreloaderAbsent(timeout: number = TimeoutConstants.TS_SELENIUM_LOAD_PAGE_TIMEOUT) {
const polling: number = TestConstants.TS_SELENIUM_DEFAULT_POLLING;
const attempts: number = timeout / polling;
Logger.debug('Ide.waitPreloaderAbsent');
await this.driverHelper.waitDisappearance(By.css(Ide.PRELOADER_CSS), attempts, polling);
}
async waitPreloaderVisible(timeout: number = TimeoutConstants.TS_SELENIUM_START_WORKSPACE_TIMEOUT) {
Logger.debug('Ide.waitPreloaderVisible');
await this.driverHelper.waitVisibility(By.css(Ide.PRELOADER_CSS), timeout);
}
async waitStatusBarContains(expectedText: string, timeout: number = TimeoutConstants.TS_SELENIUM_LANGUAGE_SERVER_START_TIMEOUT) {
const statusBarLocator: By = By.css('div[id=\'theia-statusBar\']');
Logger.debug(`Ide.waitStatusBarContains "${expectedText}"`);
await this.driverHelper.getDriver().wait(async () => {
const elementText: string = await this.driverHelper.waitAndGetText(statusBarLocator, timeout);
const isTextPresent: boolean = elementText.search(expectedText) > 0;
if (isTextPresent) {
return true;
}
await this.driverHelper.wait(TestConstants.TS_SELENIUM_DEFAULT_POLLING * 2);
}, timeout);
}
async waitStatusBarTextAbsence(expectedText: string, timeout: number = TimeoutConstants.TS_SELENIUM_LANGUAGE_SERVER_START_TIMEOUT) {
const statusBarLocator: By = By.css('div[id=\'theia-statusBar\']');
Logger.debug(`Ide.waitStatusBarTextAbsence "${expectedText}"`);
// for ensuring that check is not invoked in the gap of status displaying
for (let i: number = 0; i < 3; i++) {
await this.driverHelper.getDriver().wait(async () => {
const elementText: string = await this.driverHelper.waitAndGetText(statusBarLocator, timeout);
const isTextAbsent: boolean = elementText.search(expectedText) === -1;
if (isTextAbsent) {
return true;
}
await this.driverHelper.wait(TestConstants.TS_SELENIUM_DEFAULT_POLLING * 2);
}, timeout);
}
}
async checkLsInitializationStart(expectedTextInStatusBar: string) {
Logger.debug('Ide.checkLsInitializationStart');
await this.waitStatusBarContains(expectedTextInStatusBar, 20000);
}
async performKeyCombination(keyCombination: string, timeout: number = TimeoutConstants.TS_SELENIUM_CLICK_ON_VISIBLE_ITEM) {
Logger.debug(`Ide.performKeyCombination "${keyCombination}"`);
const bodyLocator: By = By.tagName('body');
await this.driverHelper.type(bodyLocator, keyCombination, timeout);
}
async waitRightToolbarButtonSelection(buttonTitle: string, timeout: number = TimeoutConstants.TS_SELENIUM_TOOLBAR_TIMEOUT) {
Logger.debug('Ide.waitRightToolbarButtonSelection');
const selectedRightToolbarButtonLocator: By = this.getSelectedRightToolbarButtonLocator(buttonTitle);
await this.driverHelper.waitVisibility(selectedRightToolbarButtonLocator, timeout);
}
async getApplicationUrlFromNotification(notificationText: string, timeout: number = TimeoutConstants.TS_NOTIFICATION_CENTER_TIMEOUT) {
Logger.debug(`Ide.getApplicationUrlFromNotification ${notificationText}`);
const notificationTextLocator: By = By.xpath(`//div[@class='theia-notification-message']/span[contains(.,'${notificationText}')]`);
let notification = await this.driverHelper.waitAndGetText(notificationTextLocator, timeout);
let regexp: RegExp = new RegExp('^.*(https?://.*)$');
if (!regexp.test(notification)) {
throw new Error('Cannot obtaine url from notification message');
}
return notification.split(regexp)[1];
}
async closeAllNotifications(timeout: number = TimeoutConstants.TS_NOTIFICATION_CENTER_TIMEOUT) {
Logger.debug(`Ide.closeAllNotifications`);
for (let i: number = 0; i < 5; i++) {
await this.notificationCenter.open();
try {
await this.notificationCenter.closeAll(timeout);
break;
} catch (err) {
if (!(err instanceof error.TimeoutError)) {
throw err;
}
if (i === 4) {
Logger.debug('The last try to clear of the notification center was unsuccessful');
throw err;
}
}
}
}
async waitApllicationIsReady(url: string,
timeout: number) {
Logger.debug(`Ide.waitApllicationIsReady ${url}`);
await this.driverHelper.getDriver().wait(async () => {
try {
const res = await axios.get(url);
if (res.status === 200) {
return true;
}
} catch (error) {
await this.driverHelper.wait(TestConstants.TS_SELENIUM_DEFAULT_POLLING);
}
}, timeout);
}
async waitAndApplyTrustNotification() {
Logger.debug(`Ide.waitAndApplyTrustNotification`);
await this.waitNotificationAndClickOnButton('Do you trust the authors of', 'Yes, I trust', 60_000);
}
async clickOnCancelDialogButton() {
Logger.debug('Ide.closeRestartYourWorkspaceDialog');
const cancelButtonLocator: string = `//div[@class='dialogBlock']//button[text()='Cancel']`;
await this.driverHelper.waitAndClick(By.xpath(cancelButtonLocator));
}
private getSelectedRightToolbarButtonLocator(buttonTitle: string): By {
return By.xpath(`//div[@id='theia-left-content-panel']//ul[@class='p-TabBar-content']` +
`//li[@title[contains(.,'${buttonTitle}')] and contains(@id, 'shell-tab')] and contains(@class, 'p-mod-current')`);
}
private getLeftToolbarButtonLocator(buttonTitle: String): By {
return By.xpath(`//div[@id='theia-left-content-panel']//ul[@class='p-TabBar-content']` +
`//li[@title[contains(.,'${buttonTitle}')] and contains(@id, 'shell-tab')]`);
}
private getNotificationXpathLocator(notificationText: string): string {
return `//div[@class='theia-notification-message']/span[contains(.,'${notificationText}')]`;
}
}