From d66611120ece44ef20ad8d7dc57ea4f201da4d40 Mon Sep 17 00:00:00 2001 From: helkv Date: Fri, 1 Mar 2019 15:30:15 +0100 Subject: [PATCH] Fixed EditItemsPage addOwnMetadataAll()-methods - Removed Thread.sleep - Added/Used new method textToBePresentInElementIgnoreCase() --- .../pages/admin/BrowseStatementsPage.java | 2 +- src/spot/pages/registered/EditItemsPage.java | 23 ++++++------ src/test/base/SeleniumWrapper.java | 36 ++++++++++++++++++- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/spot/pages/admin/BrowseStatementsPage.java b/src/spot/pages/admin/BrowseStatementsPage.java index c14574d..39db733 100644 --- a/src/spot/pages/admin/BrowseStatementsPage.java +++ b/src/spot/pages/admin/BrowseStatementsPage.java @@ -81,7 +81,7 @@ private WebElement findStatement(String name) { } } - throw new NoSuchElementException("Statement with this name is not available on the page."); + throw new NoSuchElementException("Statement with name '" + name + "' is not available on the page."); } public int statementCount() { diff --git a/src/spot/pages/registered/EditItemsPage.java b/src/spot/pages/registered/EditItemsPage.java index c8010ae..b584696 100644 --- a/src/spot/pages/registered/EditItemsPage.java +++ b/src/spot/pages/registered/EditItemsPage.java @@ -1,6 +1,7 @@ package spot.pages.registered; import static test.base.SeleniumWrapper.textToBePresentInAllElements; +import static test.base.SeleniumWrapper.textToBePresentInElementIgnoreCase; import java.util.List; @@ -38,6 +39,8 @@ public class EditItemsPage extends BasePage { @FindBy(xpath = "//input[contains(@value, 'Overwrite')]") private WebElement overwriteAllValues; + private final By addMetadataButtonCssSelector = By.cssSelector("#editBatchForm\\:select\\:statementList .selectMdButton"); + //TODO: Refactor this class: Maybe split it in multiple Page Object Subclasses, Replace the Thread.sleeps with explicit waits public EditItemsPage(WebDriver driver) { @@ -83,12 +86,12 @@ public EditItemsPage overwriteAllValues(String key, String value) { public EditItemsPage addOwnMetadataAll(String key, String value) { metadataButton.click(); keyBox.sendKeys(key); - try { - Thread.sleep(5000); - } - catch (InterruptedException e) {} - driver.findElement(By.cssSelector("#editBatchForm\\:select\\:statementList .selectMdButton")).click(); + // Wait until the full metadata name is shown in the add metadata button before clicking it + wait.until(textToBePresentInElementIgnoreCase(addMetadataButtonCssSelector, key)); + WebElement addMetadataButton = driver.findElement(addMetadataButtonCssSelector); + addMetadataButton.click(); + WebElement confirm = retryingElement(By.id("editBatchForm:select:dialog:btnCreateStatement")); confirm.click(); @@ -106,12 +109,12 @@ public EditItemsPage addOwnMetadataAll(String key, String value) { public EditItemsPage addOwnMetadataAll(String key, String value, final List predefinedValues) { metadataButton.click(); keyBox.sendKeys(key); - try { - Thread.sleep(5000); - } - catch (InterruptedException e) {} - driver.findElement(By.cssSelector("#editBatchForm\\:select\\:statementList .selectMdButton")).click(); + // Wait until the full metadata name is shown in the add metadata button before clicking it + wait.until(textToBePresentInElementIgnoreCase(addMetadataButtonCssSelector, key)); + WebElement addMetadataButton = driver.findElement(addMetadataButtonCssSelector); + addMetadataButton.click(); + WebElement confirm = retryingElement(By.id("editBatchForm:select:dialog:btnCreateStatement")); confirm.click(); diff --git a/src/test/base/SeleniumWrapper.java b/src/test/base/SeleniumWrapper.java index 88771e5..f674021 100644 --- a/src/test/base/SeleniumWrapper.java +++ b/src/test/base/SeleniumWrapper.java @@ -59,6 +59,9 @@ public static void waitForLoadOfNewPage(WebDriverWait wait, WebElement oldElemen /** * An expectation for checking if the given text is present in ALL specified elements.
+ * + * Equal to ExpectedConditions.textToBePresentInElementLocated() but checking all specified elements and ignoring case
+ * * This method can be used to wait for the ExpectedCondition to become true:
* -> wait.until(textToBePresentInAllElements(locator, text)); * @@ -88,7 +91,38 @@ public Boolean apply(WebDriver driver) { @Override public String toString() { - return String.format("inner text '%s' to be the value of all elements located %s", text, locator); + return String.format("text ('%s') to be present in all elements located by %s ignoring case", text, locator); + } + }; + } + + /** + * An expectation for checking if the given text is present in the specified element.
+ * + * Equal to ExpectedConditions.textToBePresentInElementLocated() but ignoring case
+ * + * This method can be used to wait for the ExpectedCondition to become true:
+ * -> wait.until(textToBePresentInAllElements(locator, text)); + * + * @param locator used to find the element + * @param text to be present the element + * @return true once the element contains the given text + */ + public static ExpectedCondition textToBePresentInElementIgnoreCase(final By locator, final String text) { + return new ExpectedCondition() { + @Override + public Boolean apply(WebDriver driver) { + try { + String elementText = driver.findElement(locator).getText(); + return elementText.toLowerCase().contains(text.toLowerCase()); + } catch (StaleElementReferenceException e) { + return null; + } + } + + @Override + public String toString() { + return String.format("text ('%s') to be present in element located by %s ignoring case", text, locator); } }; }