-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCreateWorkspace.ts
More file actions
192 lines (150 loc) · 7.42 KB
/
CreateWorkspace.ts
File metadata and controls
192 lines (150 loc) · 7.42 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
/** *******************************************************************
* 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 { inject, injectable } from 'inversify';
import { CLASSES } from '../../configs/inversify.types';
import { DriverHelper } from '../../utils/DriverHelper';
import { By, Key, WebElement } from 'selenium-webdriver';
import { Logger } from '../../utils/Logger';
import { TIMEOUT_CONSTANTS } from '../../constants/TIMEOUT_CONSTANTS';
import { BASE_TEST_CONSTANTS } from '../../constants/BASE_TEST_CONSTANTS';
import { TrustAuthorPopup } from './TrustAuthorPopup';
@injectable()
export class CreateWorkspace {
private static readonly FACTORY_URL: By = By.xpath('//input[@id="git-repo-url"]');
private static readonly GIT_REPO_OPTIONS: By = By.xpath('//span[text()="Git Repo Options"]');
private static readonly GIT_BRANCH_NAME: By = By.xpath('//input[@aria-label="Git Branch"]');
private static readonly PATH_TO_DEVFILE: By = By.xpath('//input[@aria-label="Path to Devfile"]');
private static readonly CREATE_AND_OPEN_BUTTON: By = By.xpath('//button[@id="create-and-open-button"]');
private static readonly CREATE_NEW_WORKPACE_CHECKBOX: By = By.xpath('//label[@for="create-new-if-exist-switch"]');
private static readonly CREATE_NEW_WORKPACE_CHECKBOX_VALUE: By = By.xpath('//input[@id="create-new-if-exist-switch"]');
constructor(
@inject(CLASSES.DriverHelper)
private readonly driverHelper: DriverHelper,
@inject(CLASSES.TrustAuthorPopup)
private readonly trustAuthorPopup: TrustAuthorPopup
) {}
async waitTitleContains(expectedText: string, timeout: number = TIMEOUT_CONSTANTS.TS_COMMON_DASHBOARD_WAIT_TIMEOUT): Promise<void> {
Logger.debug(`text: "${expectedText}"`);
const pageTitleLocator: By = By.xpath(`//h1[contains(text(), '${expectedText}')]`);
await this.driverHelper.waitVisibility(pageTitleLocator, timeout);
}
async waitPage(timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_LOAD_PAGE_TIMEOUT): Promise<void> {
Logger.debug();
await this.waitTitleContains('Create Workspace', timeout);
}
async clickOnSampleNoEditorSelection(
sampleName: string,
timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT
): Promise<void> {
Logger.debug(`sampleName: "${sampleName}"`);
const sampleLocator: By = this.getSampleLocator(sampleName);
await this.driverHelper.waitAndClick(sampleLocator, timeout);
}
async clickOnSampleForSpecificEditor(
sampleName: string,
timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT
): Promise<void> {
await this.clickOnEditorsDropdownListButton(sampleName, timeout);
Logger.debug(`sampleName: "${sampleName}"`);
const sampleLocator: By = this.getSampleWithSpecificEditorLocator(sampleName);
await this.driverHelper.waitAndClick(sampleLocator, timeout);
}
async importFromGitUsingUI(
factoryUrl: string,
branchName?: string,
timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT
): Promise<void> {
Logger.debug(`factoryUrl: "${factoryUrl}"`);
await this.driverHelper.waitVisibility(CreateWorkspace.FACTORY_URL, timeout);
await this.driverHelper.type(CreateWorkspace.FACTORY_URL, Key.chord(factoryUrl), timeout);
if (branchName) {
await this.driverHelper.waitAndClick(CreateWorkspace.GIT_REPO_OPTIONS, timeout);
await this.driverHelper.waitVisibility(CreateWorkspace.GIT_BRANCH_NAME, timeout);
await this.driverHelper.type(CreateWorkspace.GIT_BRANCH_NAME, Key.chord(branchName, Key.ENTER), timeout);
}
await this.driverHelper.waitAndClick(CreateWorkspace.CREATE_AND_OPEN_BUTTON, timeout);
await this.performTrustAuthorPopup();
}
async setGitRepositoryUrl(factoryUrl: string, timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT): Promise<void> {
Logger.debug(`factoryUrl: "${factoryUrl}"`);
await this.driverHelper.waitVisibility(CreateWorkspace.FACTORY_URL, timeout);
await this.driverHelper.type(CreateWorkspace.FACTORY_URL, Key.chord(factoryUrl), timeout);
}
async getGitRepositoryUrl(timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT): Promise<string> {
Logger.debug();
return await this.driverHelper.waitAndGetValue(CreateWorkspace.FACTORY_URL, timeout);
}
async clickOnCreateAndOpenButton(timeout: number = TIMEOUT_CONSTANTS.TS_CLICK_DASHBOARD_ITEM_TIMEOUT): Promise<void> {
Logger.debug();
await this.driverHelper.waitAndClick(CreateWorkspace.CREATE_AND_OPEN_BUTTON, timeout);
}
async clickOnEditorsDropdownListButton(sampleName: string, timeout: number): Promise<void> {
Logger.debug(`sampleName: "${sampleName}, editor ${BASE_TEST_CONSTANTS.TS_SELENIUM_EDITOR}"`);
const editorDropdownListLocator: By = this.getEditorsDropdownListLocator(sampleName);
await this.driverHelper.waitAndClick(editorDropdownListLocator, timeout);
}
async performTrustAuthorPopup(): Promise<void> {
Logger.debug();
try {
await this.trustAuthorPopup.clickContinue();
} catch (e) {
Logger.info('"Trust author" popup was not shown');
}
}
async isCreateNewWorkspaceCheckboxChecked(timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_WAIT_FOR_URL): Promise<boolean> {
Logger.debug();
const element: WebElement = await this.driverHelper.waitPresence(CreateWorkspace.CREATE_NEW_WORKPACE_CHECKBOX_VALUE, timeout);
return await element.isSelected();
}
async clickOnCreateNewWorkspaceCheckbox(timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_WAIT_FOR_URL): Promise<void> {
Logger.debug();
await this.driverHelper.waitAndClick(CreateWorkspace.CREATE_NEW_WORKPACE_CHECKBOX, timeout);
}
async setCreateNewWorkspaceCheckbox(
checked: boolean = true,
timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_WAIT_FOR_URL
): Promise<void> {
Logger.debug(`checked: ${checked}`);
// check current state
const isCurrentlyChecked: boolean = await this.isCreateNewWorkspaceCheckboxChecked(timeout);
// if already in desired state, do nothing
if (isCurrentlyChecked === checked) {
Logger.debug(`Checkbox is already ${checked ? 'set' : 'unset'}, no action needed`);
return;
}
// click to change state
Logger.debug(`Checkbox is ${isCurrentlyChecked ? 'set' : 'unset'}, ${checked ? 'setting' : 'unsetting'} it now`);
await this.driverHelper.waitAndClick(CreateWorkspace.CREATE_NEW_WORKPACE_CHECKBOX, timeout);
await this.driverHelper.wait(1000);
}
private getEditorsDropdownListLocator(sampleName: string): By {
return By.xpath(`//div[text()=\'${sampleName}\']//parent::article//button`);
}
private getSampleWithSpecificEditorLocator(sampleName: string): By {
let editor: string = '';
switch (process.env.TS_SELENIUM_EDITOR) {
case 'che-code':
editor = 'code';
break;
default:
throw new Error(`Unsupported editor ${process.env.TS_SELENIUM_EDITOR}`);
}
Logger.trace(`sampleName: ${sampleName}, editor "${editor}"`);
return By.xpath(`//div[text()='${sampleName}']//parent::article//span[text()[
contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
'${editor}')]
]//parent::a`);
}
private getSampleLocator(sampleName: string): By {
Logger.trace(`sampleName: ${sampleName}, used default editor`);
return By.xpath(`//div[contains(@id, 'sample-card') and text()='${sampleName}']`);
}
}