Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API key tests to use ApiKeyPanel and ApiKeyDialog #2088

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have thought using this might produce a stale element exception. If not, then so much the better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body of the dialog does go stale (hence the clearElementCache) but the outer div of the dialog is stable.

}

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