|
| 1 | +package org.labkey.test.pages.core.admin; |
| 2 | + |
| 3 | +import org.labkey.remoteapi.CommandException; |
| 4 | +import org.labkey.remoteapi.Connection; |
| 5 | +import org.labkey.remoteapi.SimplePostCommand; |
| 6 | +import org.labkey.test.BootstrapLocators; |
| 7 | +import org.labkey.test.Locator; |
| 8 | +import org.labkey.test.Locators; |
| 9 | +import org.labkey.test.WebDriverWrapper; |
| 10 | +import org.labkey.test.WebTestHelper; |
| 11 | +import org.labkey.test.components.html.Input; |
| 12 | +import org.labkey.test.pages.LabKeyPage; |
| 13 | +import org.labkey.test.util.PortalHelper; |
| 14 | +import org.openqa.selenium.NoSuchElementException; |
| 15 | +import org.openqa.selenium.StaleElementReferenceException; |
| 16 | +import org.openqa.selenium.TimeoutException; |
| 17 | +import org.openqa.selenium.WebDriver; |
| 18 | +import org.openqa.selenium.WebElement; |
| 19 | +import org.openqa.selenium.support.ui.ExpectedConditions; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +public class AllowedFileExtensionAdminPage extends LabKeyPage<AllowedFileExtensionAdminPage.ElementCache> |
| 31 | +{ |
| 32 | + public AllowedFileExtensionAdminPage(WebDriver driver) |
| 33 | + { |
| 34 | + super(driver); |
| 35 | + } |
| 36 | + |
| 37 | + public static AllowedFileExtensionAdminPage beginAt(WebDriverWrapper webDriverWrapper) |
| 38 | + { |
| 39 | + webDriverWrapper.beginAt(WebTestHelper.buildURL("admin", "allowList", |
| 40 | + Map.of("type", "FileExtension"))); |
| 41 | + return new AllowedFileExtensionAdminPage(webDriverWrapper.getDriver()); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void waitForPage() |
| 46 | + { |
| 47 | + waitFor(()-> { |
| 48 | + try |
| 49 | + { |
| 50 | + return !BootstrapLocators.loadingSpinner.areAnyVisible(getDriver()) && |
| 51 | + elementCache().extension.getComponentElement().isDisplayed(); |
| 52 | + } |
| 53 | + catch (NoSuchElementException | StaleElementReferenceException | TimeoutException retry) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + }, "Allowed File Extensions page did not load in time.", 3_000); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Clears any file extensions that are set as the only allowable names, letting users upload any filename they like. |
| 63 | + * |
| 64 | + * @param connection A connection object to use in the command.execute call. |
| 65 | + */ |
| 66 | + public static void deleteAllAllowedFileExtension(Connection connection) throws IOException, CommandException |
| 67 | + { |
| 68 | + SimplePostCommand command = new SimplePostCommand("admin", "deleteAllValues"); |
| 69 | + Map<String, Object> params = new HashMap<>(); |
| 70 | + params.put("type", "FileExtension"); |
| 71 | + command.setParameters(params); |
| 72 | + command.execute(connection, "/"); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public AllowedFileExtensionAdminPage setExtension(String extension) |
| 77 | + { |
| 78 | + elementCache().extension.set(extension); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public String clickSaveExpectingError() |
| 83 | + { |
| 84 | + return clickButtonExpectingError(elementCache().saveExtension); |
| 85 | + } |
| 86 | + |
| 87 | + public AllowedFileExtensionAdminPage clickSaveExtension() |
| 88 | + { |
| 89 | + return clickButtonNoError(elementCache().saveExtension); |
| 90 | + } |
| 91 | + |
| 92 | + public AllowedFileExtensionAdminPage updateExtension(String oldExtension, String newExtension) |
| 93 | + { |
| 94 | + getAllowedExtension(getAllowedExtensionIndex(oldExtension)).set(newExtension); |
| 95 | + clearCache(); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public AllowedFileExtensionAdminPage clickSaveUpdateExtension() |
| 100 | + { |
| 101 | + return clickButtonNoError(elementCache().saveUpdateExtension); |
| 102 | + } |
| 103 | + |
| 104 | + public String clickUpdateExtensionExpectingError() |
| 105 | + { |
| 106 | + return clickButtonExpectingError(elementCache().saveUpdateExtension); |
| 107 | + } |
| 108 | + |
| 109 | + private AllowedFileExtensionAdminPage clickButtonNoError(WebElement button) |
| 110 | + { |
| 111 | + clickAndWait(button); |
| 112 | + clearCache(); |
| 113 | + assertNoLabKeyErrors(); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + private String clickButtonExpectingError(WebElement button) |
| 118 | + { |
| 119 | + clickAndWait(button); |
| 120 | + clearCache(); |
| 121 | + return waitForElement(Locators.labkeyError).getText(); |
| 122 | + } |
| 123 | + |
| 124 | + public AllowedFileExtensionAdminPage deleteExtension(int index) |
| 125 | + { |
| 126 | + WebElement deleteButton = elementCache().deleteExtensions.get(index); |
| 127 | + deleteButton.click(); |
| 128 | + |
| 129 | + shortWait().withMessage("Existing extenstion was not deleted.") |
| 130 | + .until(ExpectedConditions.stalenessOf(deleteButton)); |
| 131 | + |
| 132 | + clearCache(); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public AllowedFileExtensionAdminPage deleteExtension(String extension) |
| 137 | + { |
| 138 | + return deleteExtension(getAllowedExtensionIndex(extension)); |
| 139 | + } |
| 140 | + |
| 141 | + public AllowedFileExtensionAdminPage deleteAllExtensions(boolean acceptAlert) |
| 142 | + { |
| 143 | + if (acceptAlert) |
| 144 | + { |
| 145 | + doAndWaitForPageToLoad(() -> { |
| 146 | + elementCache().deleteAll.click(); |
| 147 | + acceptAlert(); |
| 148 | + }); |
| 149 | + clearCache(); |
| 150 | + assertFalse("Delete All button should not be present after deleting all extensions", elementCache().deleteAll.isDisplayed()); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + elementCache().deleteAll.click(); |
| 155 | + cancelAlert(); |
| 156 | + assertTrue("Delete All button should be present", elementCache().deleteAll.isDisplayed()); |
| 157 | + } |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + public List<Input> getAllowedExtensions() |
| 162 | + { |
| 163 | + List<WebElement> collection = Locator.inputByIdContaining("existingValue") |
| 164 | + .findElements(elementCache().existingPanel); |
| 165 | + |
| 166 | + return collection.stream().map(el -> new Input(el, getDriver())).collect(Collectors.toList()); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + public Input getAllowedExtension(int index) |
| 171 | + { |
| 172 | + return getAllowedExtensions().get(index); |
| 173 | + } |
| 174 | + |
| 175 | + public Integer getAllowedExtensionIndex(String extension) |
| 176 | + { |
| 177 | + int index = 0; |
| 178 | + List<Input> allowedExtensions = getAllowedExtensions(); |
| 179 | + |
| 180 | + for(Input element : allowedExtensions) |
| 181 | + { |
| 182 | + if (element.getValue().equalsIgnoreCase(extension)) |
| 183 | + return index; |
| 184 | + |
| 185 | + index++; |
| 186 | + } |
| 187 | + |
| 188 | + return -1; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + protected ElementCache newElementCache() |
| 193 | + { |
| 194 | + return new ElementCache(); |
| 195 | + } |
| 196 | + |
| 197 | + protected class ElementCache extends LabKeyPage<ElementCache>.ElementCache |
| 198 | + { |
| 199 | + final WebElement registerNewPanel = PortalHelper.Locators.webPart("Register New Allowed File Extension").findWhenNeeded(this); |
| 200 | + final WebElement existingPanel = PortalHelper.Locators.webPart("Existing Allowed File Extensions").findWhenNeeded(this); |
| 201 | + |
| 202 | + final Input extension = new Input(Locator.id("newValueTextField").findWhenNeeded(registerNewPanel), getDriver()); |
| 203 | + final WebElement saveExtension = Locator.lkButton("Save").findWhenNeeded(registerNewPanel); |
| 204 | + |
| 205 | + final WebElement saveUpdateExtension = Locator.lkButton("Save").findWhenNeeded(existingPanel); |
| 206 | + |
| 207 | + List<Input> allowedExtensions() |
| 208 | + { |
| 209 | + return Locator.inputByIdContaining("existingValue") |
| 210 | + .findElements(elementCache().existingPanel) |
| 211 | + .stream().map(el -> new Input(el, getDriver())).collect(Collectors.toList()); |
| 212 | + } |
| 213 | + |
| 214 | + final List<WebElement> deleteExtensions = Locator.linkWithText("Delete").findElements(existingPanel); |
| 215 | + |
| 216 | + final WebElement deleteAll = Locator.linkWithText("Delete All").findWhenNeeded(existingPanel); |
| 217 | + |
| 218 | + } |
| 219 | +} |
0 commit comments