Skip to content

Commit

Permalink
Fixed EditItemsPage addOwnMetadataAll()-methods
Browse files Browse the repository at this point in the history
- Removed Thread.sleep
- Added/Used new method textToBePresentInElementIgnoreCase()
  • Loading branch information
helkv committed Mar 1, 2019
1 parent 15e0cbe commit d666111
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/spot/pages/admin/BrowseStatementsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 13 additions & 10 deletions src/spot/pages/registered/EditItemsPage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spot.pages.registered;

import static test.base.SeleniumWrapper.textToBePresentInAllElements;
import static test.base.SeleniumWrapper.textToBePresentInElementIgnoreCase;

import java.util.List;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -106,12 +109,12 @@ public EditItemsPage addOwnMetadataAll(String key, String value) {
public EditItemsPage addOwnMetadataAll(String key, String value, final List<String> 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();

Expand Down
36 changes: 35 additions & 1 deletion src/test/base/SeleniumWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br>
*
* Equal to ExpectedConditions.textToBePresentInElementLocated() but checking all specified elements and ignoring case <br>
*
* This method can be used to wait for the ExpectedCondition to become true: <br>
* -> wait.until(textToBePresentInAllElements(locator, text));
*
Expand Down Expand Up @@ -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. <br>
*
* Equal to ExpectedConditions.textToBePresentInElementLocated() but ignoring case <br>
*
* This method can be used to wait for the ExpectedCondition to become true: <br>
* -> 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<Boolean> textToBePresentInElementIgnoreCase(final By locator, final String text) {
return new ExpectedCondition<Boolean>() {
@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);
}
};
}
Expand Down

0 comments on commit d666111

Please sign in to comment.