-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathGlpiPage.ts
More file actions
466 lines (416 loc) · 14.4 KB
/
Copy pathGlpiPage.ts
File metadata and controls
466 lines (416 loc) · 14.4 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
import { expect, type Locator, type Page } from '@playwright/test';
import { readFileSync } from 'fs';
import path from 'path';
/**
* Store common actions that can be executed on any GLPI page.
*/
export class GlpiPage
{
public readonly page: Page;
public readonly user_menu: Locator;
public readonly logout_link: Locator;
public readonly change_profile_button: Locator;
public readonly history_rows: Locator;
public readonly dashboards_widgets: Locator;
public readonly active_entity: Locator;
// Layout locators (common to all authenticated pages)
public readonly sidebar: Locator;
public readonly main_header: Locator;
public readonly sidebar_menu_toggles: Locator;
public readonly user_menu_dropdown: Locator;
public readonly entity_menu_toggle: Locator;
public readonly entity_menu_dropdown: Locator;
public readonly about_link: Locator;
// Notes tab locators
public readonly add_note_button: Locator;
public readonly submit_note_button: Locator;
public readonly is_visible_on_ticket_checkbox: Locator;
public readonly note_content_input: Locator;
public readonly notes: Locator;
public readonly notes_content: Locator;
public constructor(page: Page)
{
this.page = page;
// Define locators
this.user_menu = this.getLink('User menu');
this.logout_link = this.getLink('Logout');
this.change_profile_button = this.getButton('Change profile');
this.history_rows = page.getByRole('row');
this.dashboards_widgets = page.getByTestId("dashboard-widget");
// .first() because we always display this information twice, with only
// one being shown depending on the screen size.
this.active_entity = page.getByTestId("current-entity").first();
// Layout locators
this.sidebar = page.getByTestId('sidebar');
this.main_header = page.getByTestId('main-header');
this.sidebar_menu_toggles = page.getByTestId('sidebar-menu-toggle');
this.user_menu_dropdown = page.getByTestId('user-menu-dropdown').filter({ visible: true });
this.entity_menu_toggle = page.getByRole('link', { name: 'Select the desired entity' });
this.entity_menu_dropdown = page.getByRole('dialog', { name: 'Select the desired entity' }).filter({ visible: true });
this.about_link = page.getByTestId('about-link').filter({ visible: true });
// Notes tab locators
this.add_note_button = this.getButton("Add a note");
this.submit_note_button = this.getButton("Add");
this.is_visible_on_ticket_checkbox = this.getCheckbox("Visible on tickets");
this.note_content_input = this.getRichTextByLabel('Content');
this.notes = page.getByTestId('note-container');
this.notes_content = page.getByTestId('note-content');
}
public async doSearchAndClickDropdownValue(
dropdown: Locator,
value: string,
exact: boolean = true
): Promise<void> {
await dropdown.click();
await this.page.keyboard.type(value);
// Select2 roles are different if the dropdown group some values
const simple_dropdown = this.page
.getByRole('listbox')
.getByRole('option', {'name': value, exact: exact})
.first()
;
const simple_dropdown_with_prefix = this.page
.getByRole('listbox')
.getByRole('option', {'name': `»${value}`, exact: exact})
;
const dropdown_with_groups = this.page
.getByRole('listbox')
.getByRole('listitem', {'name': value, exact: exact})
;
await simple_dropdown
.or(simple_dropdown_with_prefix)
.or(dropdown_with_groups)
.click()
;
await expect(dropdown).toContainText(value);
}
public async doSetDropdownValue(
dropdown: Locator,
value: string,
exact: boolean = true,
check_value: boolean = true,
): Promise<void> {
await dropdown.click();
// Select2 roles are different if the dropdown group some values
const simple_dropdown = this.page
.getByRole('listbox')
.getByRole('option', {'name': value, exact: exact})
;
const dropdown_with_groups = this.page
.getByRole('listbox')
.getByRole('listitem', {'name': value, exact: exact})
;
await simple_dropdown.or(dropdown_with_groups).click();
if (check_value) {
await expect(dropdown).toContainText(value);
}
}
public async doClearDropdownValue(
dropdown: Locator,
value: string,
): Promise<void> {
// We have no control on select2 selectors
// eslint-disable-next-line playwright/no-raw-locators
await dropdown.getByText(value)
.locator('..')
.locator('.select2-selection__choice__remove')
.click()
;
}
public async doLogout(): Promise<void>
{
await this.user_menu.click();
await this.page.goto('/front/logout.php');
}
public async doChangeProfile(profile: string): Promise<void>
{
await this.user_menu.click();
await this.change_profile_button.click();
await this.page.getByRole('button', {name: profile}).click();
}
public async doGoToTab(name: string): Promise<void>
{
const tab = this.getTab(name);
await tab.click();
}
public async doOpenEntitySelector(): Promise<void>
{
await this.page.keyboard.press('Control+Alt+KeyE');
}
public async doSwitchToAllEntities(): Promise<void>
{
await this.getButton("Select all").click();
}
public async doSearchForEntity(entity_name: string): Promise<void>
{
await this.getTextbox("Search entities").fill(entity_name);
}
public async doSwitchToEntityWithRecursion(
entity_name: string
): Promise<void> {
// eslint-disable-next-line playwright/no-raw-locators
await this.getButton(entity_name).locator('//ancestor::li').getByRole('button', {
name: 'Select this entity and all its children',
includeHidden: true, // Button is hidden in the accessibility tree because it is replaced by keyboard controls.
}).click();
}
public async doSwitchToEntityWithoutRecursion(
entity_name: string
): Promise<void> {
await this.getButton(entity_name).click();
}
public async doAddNote(content: string): Promise<void>
{
await this.add_note_button.click();
await this.note_content_input.fill(content);
await this.submit_note_button.click();
}
public async doUpdateNoteContent(
index: number,
content: string,
): Promise<void> {
// Update content
await this.getButton("Edit").click();
await this.note_content_input.fill(content);
await this.getButton("Update").click();
}
public async doAddFileToNote(
index: number,
file: string,
): Promise<void> {
// Add file to note
await this.getButton("Edit").click();
await this.doAddFileToUploadArea(file, this.page.getByRole('dialog'));
await this.getButton("Update").click();
}
public async doDeleteNote(): Promise<void> {
// Prepare to confirm delete dialog
this.page.once('dialog', dialog => dialog.accept());
// Trigger deletion
await this.getButton("Delete").click();
}
public async doAddFileToUploadArea(file: string, parent: Locator): Promise<void>
{
// We have no control over this input locator as it is handled by a 3rd
// party lib.
// eslint-disable-next-line playwright/no-raw-locators
await parent.locator('input[type="file"]')
.setInputFiles(path.join(__dirname, `../../fixtures/${file}`))
;
const progress = parent.getByRole('progressbar');
// Upload progress should fill up then disappear
await expect(progress).toHaveText("Upload successful");
await expect(progress).not.toBeAttached();
}
public async doClickDownloadLinkAndGetcontent(
page: Page,
link: Locator
): Promise<string> {
// Start download
const download_promise = page.waitForEvent('download');
await link.click();
// Read file once download is complete
const download = await download_promise;
const path = await download.path();
return readFileSync(path).toString();
}
/**
* Locate the orignal <select> item using its label.
* Select2's container is the span right after the select.
* The interactive element is the combobox inside the container.
*/
public getDropdownByLabel(label: string, base?: Locator): Locator
{
// eslint-disable-next-line playwright/no-raw-locators
return (base ?? this.page)
.getByLabel(label, {exact: true})
.locator('+ span')
.getByRole('combobox')
;
}
public async getDropdownOptions(
dropdown: Locator,
): Promise<(string | null)[]> {
await dropdown.click();
const options = await this.page
.getByRole('listbox')
.getByRole('option')
.all()
;
const values = await Promise.all(
options.map((option) => option.textContent())
);
await dropdown.click(); // Close dropdown
return values;
}
/**
* Locate the original <textarea> item using its label.
* TinyMCE's container is the div right after the textarea.
* The interactive element is the body of the iframe.
*/
public getRichTextByLabel(label: string, base?: Locator): Locator
{
// eslint-disable-next-line playwright/no-raw-locators
return (base ?? this.page)
.getByLabel(label)
.locator('+ div')
.locator('iframe:visible')
.contentFrame()
.locator('body')
;
}
/**
* Initialize a rich text editor by clicking on it, then return its body locator.
*/
public async initRichTextByLabel(label: string, base?: Locator): Promise<Locator>
{
// eslint-disable-next-line playwright/no-raw-locators
await (base ?? this.page).getByLabel(label).locator('+ div').click();
return this.getRichTextByLabel(label, base);
}
/**
* Helper method to make common operation less verbose
*/
public getLink(name: string): Locator
{
return this.page.getByRole('link', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getButton(name: string): Locator
{
return this.page.getByRole('button', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getCheckbox(name: string): Locator
{
return this.page.getByRole('checkbox', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getTextbox(name: string): Locator
{
return this.page.getByRole('textbox', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getTab(name: string): Locator
{
return this.page.getByRole('tab', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getRegion(name: string): Locator
{
return this.page.getByRole('region', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getHeading(name: string): Locator
{
return this.page.getByRole('heading', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getRadio(name: string): Locator
{
return this.page.getByRole('radio', {
name: name,
exact: true,
}).filter({ visible: true });
}
/**
* Helper method to make common operation less verbose
*/
public getSpinButton(name: string): Locator
{
return this.page.getByRole('spinbutton', {
name: name,
exact: true,
}).filter({ visible: true });
}
public getAlert(title: string): Locator
{
return this.page.getByRole('alert')
.filter({
visible: true,
hasText: title,
})
;
}
public getDialog(text: string): Locator
{
return this.page.getByRole('dialog')
.filter({
visible: true,
hasText: text,
})
;
}
public getEntityFromTree(name: string): Locator
{
return this.page.getByRole('button', {
name: name,
}).filter({ visible: true });
}
}