-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathWorkspaceHandlingTests.ts
More file actions
218 lines (194 loc) · 9.2 KB
/
WorkspaceHandlingTests.ts
File metadata and controls
218 lines (194 loc) · 9.2 KB
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
/** *******************************************************************
* copyright (c) 2019-2023 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 'reflect-metadata';
import { inject, injectable } from 'inversify';
import { CLASSES } from '../configs/inversify.types';
import { Dashboard } from '../pageobjects/dashboard/Dashboard';
import { CreateWorkspace } from '../pageobjects/dashboard/CreateWorkspace';
import { BrowserTabsUtil } from '../utils/BrowserTabsUtil';
import { Logger } from '../utils/Logger';
import { ApiUrlResolver } from '../utils/workspace/ApiUrlResolver';
import { TIMEOUT_CONSTANTS } from '../constants/TIMEOUT_CONSTANTS';
import { DriverHelper } from '../utils/DriverHelper';
import { By, error } from 'selenium-webdriver';
@injectable()
export class WorkspaceHandlingTests {
private static WORKSPACE_NAME: By = By.xpath('//h1[contains(.,"Starting workspace ")]');
private static WORKSPACE_STATUS: By = By.xpath('//*/span[@class="pf-c-label__content"]');
private static WORKSPACE_ALERT_TITLE: By = By.xpath('//h4[@class="pf-c-alert__title"]');
private static WORKSPACE_ALERT_DESCRIPTION: By = By.xpath('//*/div[@class="pf-c-alert__description"]');
private static workspaceName: string = 'undefined';
private static parentGUID: string;
constructor(
@inject(CLASSES.Dashboard) private readonly dashboard: Dashboard,
@inject(CLASSES.CreateWorkspace)
private readonly createWorkspace: CreateWorkspace,
@inject(CLASSES.BrowserTabsUtil)
private readonly browserTabsUtil: BrowserTabsUtil,
@inject(CLASSES.ApiUrlResolver)
private readonly apiUrlResolver: ApiUrlResolver,
@inject(CLASSES.DriverHelper)
private readonly driverHelper: DriverHelper
) {}
static getWorkspaceName(): string {
return WorkspaceHandlingTests.workspaceName;
}
static clearWorkspaceName(): void {
WorkspaceHandlingTests.workspaceName = 'undefined';
}
async createAndOpenWorkspace(stack: string, createNewWorkspace: boolean = true): Promise<void> {
await this.dashboard.clickWorkspacesButton();
await this.dashboard.waitPage();
Logger.debug('fetching user kubernetes namespace, storing auth token by getting workspaces API URL.');
await this.apiUrlResolver.getWorkspacesApiUrl();
await this.dashboard.clickCreateWorkspaceButton();
await this.createWorkspace.waitPage();
await this.createWorkspace.setCreateNewWorkspaceCheckbox(createNewWorkspace);
WorkspaceHandlingTests.parentGUID = await this.browserTabsUtil.getCurrentWindowHandle();
await this.createWorkspace.clickOnSampleNoEditorSelection(stack);
await this.browserTabsUtil.waitAndSwitchToAnotherWindow(WorkspaceHandlingTests.parentGUID, TIMEOUT_CONSTANTS.TS_IDE_LOAD_TIMEOUT);
}
async createAndOpenWorkspaceFromGitRepository(
factoryUrl: string,
branchName?: string,
createNewWorkspace: boolean = true
): Promise<void> {
await this.dashboard.waitPage();
Logger.debug('fetching user kubernetes namespace, storing auth token by getting workspaces API URL.');
await this.apiUrlResolver.getWorkspacesApiUrl();
await this.dashboard.clickCreateWorkspaceButton();
await this.createWorkspace.waitPage();
await this.createWorkspace.setCreateNewWorkspaceCheckbox(createNewWorkspace);
WorkspaceHandlingTests.parentGUID = await this.browserTabsUtil.getCurrentWindowHandle();
await this.createWorkspace.importFromGitUsingUI(factoryUrl, branchName);
await this.browserTabsUtil.waitAndSwitchToAnotherWindow(WorkspaceHandlingTests.parentGUID, TIMEOUT_CONSTANTS.TS_IDE_LOAD_TIMEOUT);
}
async createAndOpenWorkspaceWithExistedWorkspaceName(stack: string): Promise<void> {
Logger.debug('create and open workspace with existed workspace name.');
await this.createAndOpenWorkspace(stack);
await this.dashboard.waitExistingWorkspaceFoundAlert();
await this.dashboard.clickOnCreateNewWorkspaceButton();
}
async obtainWorkspaceNameFromStartingPage(): Promise<void> {
const timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_START_WORKSPACE_TIMEOUT;
const polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING;
const attempts: number = Math.ceil(timeout / polling);
for (let i: number = 0; i < attempts; i++) {
try {
const startingWorkspaceLineContent: string = await this.driverHelper
.getDriver()
.findElement(WorkspaceHandlingTests.WORKSPACE_NAME)
.getText();
Logger.trace(`obtained starting workspace getText():${startingWorkspaceLineContent}`);
// cutting away leading text
WorkspaceHandlingTests.workspaceName = startingWorkspaceLineContent.substring('Starting workspace '.length).trim();
Logger.trace(`trimmed workspace name from getText():${WorkspaceHandlingTests.workspaceName}`);
break;
} catch (err) {
if (err instanceof error.StaleElementReferenceError) {
Logger.trace('failed to obtain name from workspace start page, element possibly detached from DOM. Retrying.');
await this.driverHelper.wait(polling);
continue;
}
if (err instanceof error.NoSuchElementError) {
Logger.trace('failed to obtain name from workspace start page, element not visible yet. Retrying.');
await this.driverHelper.wait(polling);
continue;
}
Logger.error(`obtaining workspace name failed with an unexpected error:${err}`);
throw err;
}
}
if (WorkspaceHandlingTests.workspaceName !== '' && WorkspaceHandlingTests.workspaceName !== 'undefined') {
Logger.info(`obtained workspace name from workspace loader page: ${WorkspaceHandlingTests.workspaceName}`);
return;
}
Logger.error('failed to obtain workspace name');
await this.logStartWorkspaceInfo();
throw new error.InvalidArgumentError('WorkspaceHandlingTests.obtainWorkspaceNameFromStartingPage failed to obtain workspace name.');
}
async stopWorkspace(workspaceName: string): Promise<void> {
await this.dashboard.openDashboard();
await this.dashboard.stopWorkspaceByUI(workspaceName);
}
async removeWorkspace(workspaceName: string): Promise<void> {
await this.dashboard.openDashboard();
await this.dashboard.deleteStoppedWorkspaceByUI(workspaceName);
}
async stopAndRemoveWorkspace(workspaceName: string): Promise<void> {
await this.dashboard.openDashboard();
await this.dashboard.stopAndRemoveWorkspaceByUI(workspaceName);
}
async logStartWorkspaceInfo(): Promise<void> {
const status: string = await this.getWorkspaceStatus();
const alertTitle: string = await this.getWorkspaceAlertTitle();
const alertDescription: string = await this.getWorkspaceAlertDescription();
Logger.info('Start workspace status: ' + status);
Logger.info('Start workspace progress title: ' + alertTitle);
Logger.info('Start workspace progress description: ' + alertDescription);
}
async createAndOpenWorkspaceWithSpecificEditorAndSample(
editor: string,
sampleName: string,
xPath: string,
polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_START_WORKSPACE_TIMEOUT
): Promise<void> {
Logger.debug('Create and open workspace with specific Editor and Sample. Sample ' + editor);
await this.selectEditor(editor);
await this.createWorkspace.clickOnSampleNoEditorSelection(sampleName);
await this.waitForControlXpath(xPath, polling);
}
async createAndOpenWorkspaceWithSpecificEditorAndGitUrl(
editor: string,
sampleUrl: string,
xPath: string,
polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_START_WORKSPACE_TIMEOUT
): Promise<void> {
Logger.debug('Create and open workspace with specific Editor and URL. Sample ' + editor);
await this.selectEditor(editor);
await this.createWorkspace.importFromGitUsingUI(sampleUrl);
await this.waitForControlXpath(xPath, polling);
}
async selectEditor(editor: string): Promise<void> {
Logger.debug('select Editor. Editor: ' + editor);
await this.dashboard.openChooseEditorMenu();
await this.dashboard.chooseEditor(editor);
}
async getTextFromUIElementByXpath(xpath: string): Promise<string> {
Logger.debug('returning text from xPath: ' + xpath);
return await this.driverHelper.getDriver().findElement(By.xpath(xpath)).getText();
}
private async waitForControlXpath(xPathToWait: string, polling: number): Promise<void> {
await this.browserTabsUtil.waitAndSwitchToAnotherWindow(WorkspaceHandlingTests.parentGUID, TIMEOUT_CONSTANTS.TS_IDE_LOAD_TIMEOUT);
await this.obtainWorkspaceNameFromStartingPage();
await this.driverHelper.waitVisibility(By.xpath(xPathToWait), TIMEOUT_CONSTANTS.TS_SELENIUM_START_WORKSPACE_TIMEOUT, polling);
}
private async getWorkspaceAlertDescription(): Promise<string> {
try {
return await this.driverHelper.getDriver().findElement(WorkspaceHandlingTests.WORKSPACE_ALERT_DESCRIPTION).getText();
} catch (err) {
return '(unknown)';
}
}
private async getWorkspaceStatus(): Promise<string> {
try {
return await this.driverHelper.getDriver().findElement(WorkspaceHandlingTests.WORKSPACE_STATUS).getText();
} catch (err) {
return '(unknown)';
}
}
private async getWorkspaceAlertTitle(): Promise<string> {
try {
return await this.driverHelper.getDriver().findElement(WorkspaceHandlingTests.WORKSPACE_ALERT_TITLE).getAttribute('innerHTML');
} catch (err) {
return '(unknown)';
}
}
}