-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPortalHelper.java
More file actions
552 lines (459 loc) · 17.4 KB
/
PortalHelper.java
File metadata and controls
552 lines (459 loc) · 17.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Copyright (c) 2013-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.test.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.test.Locator;
import org.labkey.test.WebDriverWrapper;
import org.labkey.test.components.BodyWebPart;
import org.labkey.test.components.SideWebPart;
import org.labkey.test.components.WebPart;
import org.labkey.test.components.html.BootstrapMenu;
import org.labkey.test.components.html.SiteNavBar;
import org.labkey.test.components.labkey.PortalTab;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import static org.junit.Assert.assertEquals;
/**
* TODO: Move appropriate functionality into {@link org.labkey.test.pages.PortalBodyPanel} and {@link org.labkey.test.components.WebPart}
*/
public class PortalHelper extends WebDriverWrapper
{
protected WrapsDriver _driverWrapper;
public PortalHelper(WrapsDriver driverWrapper)
{
_driverWrapper = driverWrapper;
}
public PortalHelper(WebDriver driver)
{
this(() -> driver);
}
public static void doInAdminMode(WebDriver driver, Consumer<PortalHelper> actions)
{
new PortalHelper(driver).doInAdminMode(actions);
}
@Override
public WebDriver getWrappedDriver()
{
return _driverWrapper.getWrappedDriver();
}
public PortalTab activateTab(String tabText)
{
tabText = tabText.trim();
if (tabText.equals("+") || tabText.equals("add"))
throw new IllegalArgumentException("Use PortalHelper.addTab()");
return PortalTab.find(tabText, getDriver()).activate();
}
@LogMethod(quiet = true)
private void clickTabMenuItem(@LoggedParam String tabText, boolean wait, @LoggedParam String... items)
{
BootstrapMenu tabMenu = PortalTab.find(tabText, getDriver()).getMenu();
tabMenu.clickSubMenu(wait, items);
}
@LogMethod(quiet = true)
public void moveTab(@LoggedParam String tabText, @LoggedParam Direction direction)
{
if (direction.isVertical())
throw new IllegalArgumentException("Can't move folder tabs vertically.");
String tabId = tabText.replace(" ", "") + "Tab";
Locator.XPathLocator tabList = Locator.xpath("//ul[contains(@class, 'lk-nav-tabs-admin')]/li");
Locator.XPathLocator tabLink = Locator.xpath("//a[@id=" + Locator.xq(tabId) + "]");
int tabCount = getElementCount(tabList) - 1;
int startIndex = getElementIndex(tabList.withDescendant(tabLink)); // zero-based
int expectedEndIndex = startIndex;
clickTabMenuItem(tabText, false, "Move", direction.toString());
switch (direction)
{
case LEFT:
if (startIndex > 0)
expectedEndIndex = startIndex - 1;
break;
case RIGHT:
if (startIndex < (tabCount - 2)) // offset by 2 due to the '+' and the 'pencil' tabs
expectedEndIndex = startIndex + 1;
break;
}
waitForElement(tabList.index(expectedEndIndex).withDescendant(tabLink));
}
@LogMethod(quiet = true)
public void hideTab(@LoggedParam String tabText)
{
PortalTab.find(tabText, getDriver()).hide();
exitAdminMode();
assertElementNotPresent(Locator.xpath("//div[@class='lk-nav-tabs-ct']//ul//li//a[text()='" + tabText +"']"));
enterAdminMode();
}
@LogMethod(quiet = true)
public void showTab(@LoggedParam String tabText)
{
PortalTab.find(tabText, getDriver()).show();
exitAdminMode();
assertElementVisible(Locator.xpath("//div[@class='lk-nav-tabs-ct']//ul//li//a[contains(text(),'" + tabText +"')]"));
enterAdminMode();
}
@LogMethod(quiet = true)
public void deleteTab(@LoggedParam String tabText)
{
Locator tabLocator = Locator.xpath("//div[@class='lk-nav-tabs-ct']//ul//li//a[text()='" + tabText +"']");
PortalTab.find(tabText, getDriver()).delete();
waitForElementToDisappear(tabLocator);
assertElementNotPresent(tabLocator);
}
@LogMethod(quiet = true)
public void addTab(@LoggedParam String tabName)
{
addTab(tabName, null);
}
@LogMethod(quiet = true)
public void addTab(@LoggedParam String tabName, @Nullable @LoggedParam String expectedError)
{
mouseOver(Locator.folderTab("+"));
click(Locator.folderTab("+"));
waitForText("Add Tab");
setFormElement(Locator.input("tabName"), tabName);
if (expectedError != null)
{
clickButton("Ok", 0);
waitForText(expectedError);
clickButton("OK", 0);
}
else
{
clickButton("Ok");
waitForElement(Locator.folderTab(tabName));
}
}
@LogMethod(quiet = true)
public void renameTab(@LoggedParam String tabText, @LoggedParam String newName)
{
renameTab(tabText, newName, null);
}
@LogMethod(quiet = true)
public void renameTab(@LoggedParam String tabText, @LoggedParam String newName, @Nullable @LoggedParam String expectedError)
{
doInAdminMode(() -> {
PortalTab portalTab = PortalTab.find(tabText, getDriver());
portalTab.rename(newName);
if (expectedError != null)
{
waitForText(expectedError);
clickButton("OK", 0);
// Close the rename tab window.
clickButton("Cancel", 0);
}
else
{
waitForElement(Locator.linkWithText(newName));
assertElementNotPresent(Locator.linkWithText(tabText));
}
});
}
public List<String> getWebPartTitles()
{
List<WebPart> webparts = new ArrayList<>();
webparts.addAll(getBodyWebParts());
webparts.addAll(getSideWebParts());
List<String> webpartTitles = new ArrayList<>();
for (WebPart wp : webparts)
{
webpartTitles.add(wp.getTitle());
}
return webpartTitles;
}
public List<BodyWebPart> getBodyWebParts()
{
List<WebElement> webPartElements = Locator.css("div[name=webpart]").findElements(getDriver());
List<BodyWebPart> bodyWebParts = new ArrayList<>();
for (WebElement el : webPartElements)
{
bodyWebParts.add(new BodyWebPart(getDriver(), el));
}
return bodyWebParts;
}
public BodyWebPart getBodyWebPart(String partName)
{
return new BodyWebPart(getDriver(), partName);
}
public List<SideWebPart> getSideWebParts()
{
List<WebElement> webPartElements = Locator.css(".labkey-side-panel > table[name=webpart]").findElements(getDriver());
List<SideWebPart> sideWebParts = new ArrayList<>();
for (WebElement el : webPartElements)
{
sideWebParts.add(new SideWebPart(getDriver(), el));
}
return sideWebParts;
}
/**
* Allows test code to navigate to a Webpart Ext-based navigation menu.
* @param webPartTitle title (not name) of webpart to be clicked. Multiple web parts with the same title not supported.
*/
public void clickWebpartMenuItem(String webPartTitle, String... items)
{
clickWebpartMenuItem(webPartTitle, true, items);
}
public void clickWebpartMenuItem(String webPartTitle, boolean wait, String... items)
{
WebPart webPart = new BodyWebPart(getDriver(), webPartTitle);
webPart.clickMenuItem(wait, items);
}
private void doInAdminMode(Runnable runnable)
{
new SiteNavBar(getDriver()).doInAdminMode(runnable);
}
public void doInAdminMode(Consumer<PortalHelper> actions)
{
new SiteNavBar(getDriver()).doInAdminMode(() -> actions.accept(this));
}
public void enterAdminMode()
{
new SiteNavBar(getDriver()).enterPageAdminMode();
}
public void exitAdminMode()
{
new SiteNavBar(getDriver()).exitPageAdminMode();
}
@LogMethod(quiet = true)
private void addWebPart(@LoggedParam String webPartName, String formLocation)
{
doInAdminMode(() -> {
waitForElement(Locator.xpath("//option").withText(webPartName));
Locator.XPathLocator formLocatorBase = Locator.tag("form").withAttributeContaining("action", "addWebPart.view");
final Locator.XPathLocator formLocator;
if (formLocation == null)
formLocator = formLocatorBase.withDescendant(Locator.tagWithText("option", webPartName));
else
formLocator = formLocatorBase.withChild(Locator.input("location").withAttribute("value", formLocation));
List<WebElement> forms = formLocator.findElements(getDriver());
WebElement form = forms.stream().filter(WebElement::isDisplayed).findFirst()
.orElseThrow(() -> new NoSuchElementException("No visible webpart form: " + formLocator));
selectOptionByText(Locator.tag("select").findElement(form), webPartName);
doAndWaitForPageToLoad(form::submit);
});
}
public void addWebPart(String webPartName)
{
addWebPart(webPartName, null);
}
public void addBodyWebPart(String webPartName)
{
addWebPart(webPartName, "!content");
}
public void addSideWebPart(String webPartName)
{
addWebPart(webPartName, "right");
}
@LogMethod(quiet = true)
public void removeWebPart(@LoggedParam String webPartTitle)
{
new BodyWebPart(getDriver(), webPartTitle).remove();
}
public void removeAllWebParts()
{
doInAdminMode(() -> {
for (BodyWebPart webPart : getBodyWebParts())
webPart.remove();
for (SideWebPart webPart : getSideWebParts())
webPart.remove();
});
}
public void addQueryWebPart(@LoggedParam String schemaName)
{
addQueryWebPart(null, schemaName, null, null);
}
@LogMethod(quiet = true)
public void addQueryWebPart(@LoggedParam @Nullable String title, @LoggedParam String schemaName, @LoggedParam @Nullable String queryName, @Nullable String viewName)
{
addQueryWebPart(title, schemaName, queryName, viewName, WAIT_FOR_JAVASCRIPT);
}
@LogMethod(quiet = true)
public void addQueryWebPart(@LoggedParam @Nullable String title, @LoggedParam String schemaName, @LoggedParam @Nullable String queryName, @Nullable String viewName, int wait)
{
addWebPart("Query");
waitForElement(Locator.css(".schema-loaded-marker"));
if (title != null)
setFormElement(Locator.name("title"), title);
_ext4Helper.selectComboBoxItem(Locator.id("schemaName"), schemaName);
if (queryName != null)
{
click(Locator.xpath("//input[@type='button' and @id='selectQueryContents-inputEl']"));
waitForElement(Locator.css(".query-loaded-marker"), wait);
_ext4Helper.selectComboBoxItem(Locator.id("queryName"), queryName);
waitForElement(Locator.css(".view-loaded-marker"), wait);
if (viewName != null)
_ext4Helper.selectComboBoxItem(Locator.id("viewName"), viewName);
}
clickButton("Submit");
if (title == null)
{
if (queryName == null)
title = schemaName.substring(0, 1).toUpperCase() + schemaName.substring(1) + " Queries";
else
title = queryName;
}
waitForElement(Locator.xpath("//span").withClass("labkey-wp-title-text").withText(title));
}
public void addReportWebPart(@LoggedParam String reportId)
{
addReportWebPart(null, reportId, null);
}
@LogMethod(quiet = true)
public void addReportWebPart(@LoggedParam @Nullable String title, @LoggedParam @NotNull String reportId, @Nullable Boolean showTabs, String... visibleSections)
{
addWebPart("Report");
if (title != null)
setFormElement(Locator.name("title"), title);
selectOptionByText(Locator.id("reportId"), reportId);
if (showTabs != null)
{
if(showTabs)
checkCheckbox(Locator.id("showTabs"));
else
uncheckCheckbox(Locator.id("showTabs"));
}
if (visibleSections.length > 0)
waitForElement(Locator.id("showSection"));
for (String section : visibleSections)
{
selectOptionByValue(Locator.id("showSection"), section);
}
clickButton("Submit");
waitForElement(Locator.xpath("//span").withClass("labkey-wp-title-text").withText(title != null ? title : "Reports"));
}
@LogMethod(quiet = true)
public void moveWebPart(@LoggedParam String webPartTitle, @LoggedParam Direction direction)
{
if (direction.isHorizontal())
throw new IllegalArgumentException("Can't move webpart horizontally.");
doInAdminMode(() -> {
new BodyWebPart<>(getDriver(), webPartTitle).moveWebPart(direction == Direction.DOWN);
});
}
public void openWebpartPermissionWindow(String webpart)
{
clickWebpartMenuItem(webpart, false, "Permissions");
_ext4Helper.waitForMask();
waitForText("Check Permission");
}
/**
*/
public void setWebpartPermission(String webpart, String permission, String folder)
{
doInAdminMode(() -> {
openWebpartPermissionWindow(webpart);
_ext4Helper.selectComboBoxItem("Required Permission:", permission);
if (folder == null)
_ext4Helper.selectRadioButton("Check Permission On:", "Current Folder");
else
{
_ext4Helper.selectRadioButton("Check Permission On:", "Choose Folder");
click(Locator.tagWithText("div", folder));
}
click(Locator.tagWithText("span", "Save"));
_ext4Helper.waitForMaskToDisappear();
});
}
/**
* @param expectedFolder The folder that is expected to be selected, null=current folder
*/
public void checkWebpartPermission(String webpart, String expectedPermission, String expectedFolder)
{
doInAdminMode(() -> {
openWebpartPermissionWindow(webpart);
Locator loc1 = Locator.name("permission");
assertEquals(expectedPermission, getFormElement(loc1));
if (expectedFolder == null)
{
Locator loc = Locator.name("permissionContainer");
assertEquals("", getFormElement(loc));
}
else
{
Locator loc = Locator.name("permissionContainer");
assertEquals(expectedFolder, getFormElement(loc));
}
click(Locator.tagWithText("span", "Cancel"));
_ext4Helper.waitForMaskToDisappear();
});
}
public enum Direction
{
UP("Up", Axis.VERTICAL),
DOWN("Down", Axis.VERTICAL),
LEFT("Left", Axis.HORIZONTAL),
RIGHT("Right", Axis.HORIZONTAL);
private final String _dir;
private final Axis _axis;
Direction (String dir, Axis axis)
{
_dir = dir;
_axis = axis;
}
public String toString()
{
return _dir;
}
public Boolean isHorizontal()
{
return _axis == Axis.HORIZONTAL;
}
public Boolean isVertical()
{
return _axis == Axis.VERTICAL;
}
public enum Axis
{
HORIZONTAL,
VERTICAL
}
}
public static class Locators
{
public static Locator.XPathLocator webPartTitle()
{
return webPart().child(webPartTitleContainer());
}
public static Locator.XPathLocator webPartTitle(String title)
{
return webPart().child(webPartTitleContainer().withText(title));
}
public static Locator.XPathLocator webPart(String title)
{
return webPart().withChild(webPartTitleContainer().withAttribute("title", title));
}
public static Locator.XPathLocator webPartWithTitleContaining(String partialTitle)
{
return webPart().withChild(webPartTitleContainer().withAttributeContaining("title", partialTitle));
}
public static Locator.CssLocator activeTab()
{
return Locator.css(".lk-nav-tabs-ct > .nav > .active");
}
private static Locator.XPathLocator webPartTitleContainer()
{
return Locator.xpath("div/div/*").withClass("panel-title");
}
public static Locator.XPathLocator webPart()
{
return Locator.tagWithName("div", "webpart");
}
}
}