Skip to content

Commit

Permalink
Update API key tests to use ApiKeyPanel and ApiKeyDialog
Browse files Browse the repository at this point in the history
Relocate ApiKeyPanel and ApiKeyDialog
  • Loading branch information
labkey-tchad committed Oct 10, 2024
1 parent 75aba22 commit ff7dc57
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.labkey.test.credentials;
package org.labkey.test.components.core;

import org.labkey.test.Locator;
import org.labkey.test.components.bootstrap.ModalDialog;
import org.labkey.test.components.html.Input;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
Expand All @@ -27,7 +28,10 @@ public ApiKeyDialog(WebDriver driver, String title)
public ApiKeyDialog generateApiKey()
{
elementCache().generateApiKeyButton.click();
return new ApiKeyDialog(getDriver(), _title);
getWrapper().shortWait().until(ExpectedConditions.invisibilityOf(elementCache().descriptionInput.getComponentElement()));
clearElementCache();
getWrapper().shortWait().until(ExpectedConditions.visibilityOf(elementCache().inputField.getComponentElement()));
return this;
}

public ApiKeyDialog copyKey()
Expand Down Expand Up @@ -109,8 +113,8 @@ protected ApiKeyDialog.ElementCache elementCache()

protected class ElementCache extends ModalDialog.ElementCache
{
Input descriptionInput = Input.Input(Locator.tagWithId("input", "keyDescription"), getDriver()).refindWhenNeeded(this);
WebElement descriptionDisplay = Locator.tagWithClassContaining("div", "api-key__description").refindWhenNeeded(this);
Input descriptionInput = Input.Input(Locator.tagWithId("input", "keyDescription"), getDriver()).findWhenNeeded(this);
WebElement descriptionDisplay = Locator.tagWithClassContaining("div", "api-key__description").findWhenNeeded(this);
WebElement generateApiKeyButton = Locator.tagWithText("button", "Generate API Key").findWhenNeeded(this);
Input inputField = Input.Input(Locator.tagWithClass("input", "api-key__input"), getDriver()).findWhenNeeded(this);
WebElement copyKeyButton = Locator.tagWithName("button", "copy_apikey_token").findWhenNeeded(this);
Expand Down
94 changes: 94 additions & 0 deletions src/org/labkey/test/components/core/ApiKeyPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.labkey.test.components.core;

import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.labkey.test.BootstrapLocators;
import org.labkey.test.Locator;
import org.labkey.test.components.bootstrap.Panel;
import org.labkey.test.components.ui.grids.QueryGrid;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class ApiKeyPanel extends Panel<ApiKeyPanel.ElementCache>
{
protected ApiKeyPanel(WebElement element, WebDriver driver)
{
super(element, driver);
}

public static SimpleWebDriverComponentFinder<ApiKeyPanel> panelFinder(WebDriver driver)
{
return new Panel.PanelFinder(driver).withTitle("API Keys").wrap(ApiKeyPanel::new);
}

public String generateApiKey(@Nullable String description)
{
ApiKeyDialog apiKeyDialog = clickGenerateApiKey();
if (description != null)
{
apiKeyDialog.setDescription(description);
}
apiKeyDialog.generateApiKey();
Assert.assertEquals("API Key discription", description == null ? "" : description, apiKeyDialog.getDescription());
String inputFieldValue = apiKeyDialog.getInputFieldValue();
apiKeyDialog.clickDone();
return inputFieldValue;
}

public String generateApiKey()
{
return generateApiKey(null);
}
public ApiKeyDialog clickGenerateApiKey()
{
elementCache().generateApiKeyButton.click();
return new ApiKeyDialog(getDriver(), ApiKeyDialog.API_KEY_TITLE);
}

public String generateSessionKey()
{
ApiKeyDialog apiKeyDialog = clickGenerateSessionKey();
String inputFieldValue = apiKeyDialog.getInputFieldValue();
apiKeyDialog.clickDone();
return inputFieldValue;
}

public ApiKeyDialog clickGenerateSessionKey()
{
elementCache().generateSessionKeyButton.click();
return new ApiKeyDialog(getDriver(), ApiKeyDialog.SESSION_KEY_TITLE);
}

public QueryGrid getGrid()
{
return new QueryGrid.QueryGridFinder(getDriver()).findWhenNeeded();
}


public boolean isGenerateApiKeyButtonEnabled()
{
return elementCache().generateApiKeyButton.isEnabled();
}

public boolean isGenerateApiKeyButtonDisplayed()
{
return elementCache().generateApiKeyButton.isDisplayed();
}

public boolean hasDisabledMessage()
{
return BootstrapLocators.warningBanner.containing("not enabled").existsIn(this);
}

@Override
protected ElementCache newElementCache()
{
return new ElementCache();
}

protected class ElementCache extends Panel<ElementCache>.ElementCache
{
WebElement generateApiKeyButton = Locator.tagWithText("button", "Generate API Key").findWhenNeeded(this);
WebElement generateSessionKeyButton = Locator.tagWithText("button", "Generate Session Key").findWhenNeeded(this);
}
}
20 changes: 3 additions & 17 deletions src/org/labkey/test/tests/ApiKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.labkey.test.WebTestHelper;
import org.labkey.test.categories.Daily;
import org.labkey.test.components.bootstrap.ModalDialog;
import org.labkey.test.components.core.ApiKeyPanel;
import org.labkey.test.components.ui.grids.QueryGrid;
import org.labkey.test.credentials.ApiKeyDialog;
import org.labkey.test.pages.core.admin.CustomizeSitePage;
import org.labkey.test.util.Maps;
import org.labkey.test.util.TestUser;
Expand Down Expand Up @@ -373,27 +373,13 @@ private void deleteAPIKeys(List<Map<String, Object>> _generatedApiKeys) throws I
private String generateSessionKey()
{
goToExternalToolPage();
waitForText("API keys are used to authorize");
clickButton("Generate Session Key", 0);
ApiKeyDialog dialog = new ApiKeyDialog(this.getDriver(), ApiKeyDialog.SESSION_KEY_TITLE);
waitForFormElementToNotEqual(Locator.inputByNameContaining("session_token"), "");
String key = Locator.inputByNameContaining("session_token").findElement(getDriver()).getAttribute("value");
dialog.clickDone();
return key;
return ApiKeyPanel.panelFinder(getDriver()).find().generateSessionKey();
}

private String generateAPIKey(@Nullable String description)
{
goToExternalToolPage();
clickButton("Generate API Key", 0);
ApiKeyDialog dialog = new ApiKeyDialog(this.getDriver(), ApiKeyDialog.API_KEY_TITLE);
if (description != null)
dialog.setDescription(description);
dialog = dialog.generateApiKey();
waitForFormElementToNotEqual(Locator.inputByNameContaining("apikey_token"), "");
String key = Locator.inputByNameContaining("apikey_token").findElement(getDriver()).getAttribute("value");
dialog.clickDone();
return key;
return ApiKeyPanel.panelFinder(getDriver()).find().generateApiKey(description);
}

private String generateAPIKeyAndRecord(List<Map<String, Object>> _generatedApiKeys) throws IOException
Expand Down

0 comments on commit ff7dc57

Please sign in to comment.