Skip to content

Commit

Permalink
Add actual values of web element in SeleniumWebDriver error message (#…
Browse files Browse the repository at this point in the history
…11153)

Signed-off-by: Dmytro Nochevnov <dnochevnov@codenvy.com>
  • Loading branch information
dmytro-ndp authored Sep 11, 2018
1 parent 73d38f7 commit 15072ae
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package org.eclipse.che.selenium.core.webdriver;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.eclipse.che.selenium.core.constant.TestTimeoutsConstants.APPLICATION_START_TIMEOUT_SEC;
Expand Down Expand Up @@ -562,20 +563,24 @@ public String waitVisibilityAndGetText(WebElement webElement, int timeout) {
}

/**
* Waits during {@code timeout} until text extracted from {@link WebElement} with specified {@code
* elementLocator} by {@link WebElement#getAttribute(String)} equals to provided {@code
* expectedValue}.
* Waits during {@code timeout} until value extracted from {@link WebElement} with specified
* {@code locator} by {@link WebElement#getAttribute(String)} equals to provided {@code expected}
* one.
*
* @param elementLocator locator of element in which text should be checked
* @param expectedValue expected text which should be present in the element
* @param locator locator of element in which value should be checked
* @param expected expected value which should be present in the element
* @param timeout waiting time in seconds
*/
public void waitValueEqualsTo(By elementLocator, String expectedValue, int timeout) {
public void waitValueEqualsTo(By locator, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(timeout, () -> format("\nexpected:\n'%s'\nbut was:\n'%s'\n", expected, actual[0]))
.until(
(ExpectedCondition<Boolean>)
driver -> waitVisibilityAndGetValue(elementLocator, timeout).equals(expectedValue));
driver -> {
actual[0] = waitVisibilityAndGetValue(locator, timeout);
return actual[0].contains(expected);
});
}

/**
Expand All @@ -590,49 +595,58 @@ public void waitValueEqualsTo(By elementLocator, String expectedValue) {
}

/**
* Waits during {@code timeout} until text extracted from specified {@code webElement} by {@link
* WebElement#getAttribute(String)} equals to provided {@code expectedValue}.
* Waits during {@code timeout} until value extracted from specified {@code element} by {@link
* WebElement#getAttribute(String)} equals to provided {@code expected} one.
*
* @param webElement element in which text should be checked
* @param expectedValue expected text which should be present in the element
* @param element element in which text should be checked
* @param expected expected value which should be present in the element
* @param timeout waiting time in seconds
*/
public void waitValueEqualsTo(WebElement webElement, String expectedValue, int timeout) {
public void waitValueEqualsTo(WebElement element, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(timeout, () -> format("\nexpected:\n'%s'\nbut was:\n'%s'\n", expected, actual[0]))
.until(
(ExpectedCondition<Boolean>)
driver -> waitVisibilityAndGetValue(webElement, timeout).equals(expectedValue));
driver -> {
actual[0] = waitVisibilityAndGetValue(element, timeout);
return actual[0].equals(expected);
});
}

/**
* Waits until text extracted from specified {@code webElement} by {@link
* Waits until text extracted from specified {@code element} by {@link
* WebElement#getAttribute(String)} equals to provided {@code expectedValue}.
*
* @param webElement element in which text should be checked
* @param element element in which text should be checked
* @param expectedValue expected text which should be present in the element
*/
public void waitValueEqualsTo(WebElement webElement, String expectedValue) {
waitValueEqualsTo(webElement, expectedValue, DEFAULT_TIMEOUT);
public void waitValueEqualsTo(WebElement element, String expectedValue) {
waitValueEqualsTo(element, expectedValue, DEFAULT_TIMEOUT);
}

/**
* Waits during {@code timeout} until specified {@code element} contains the defined {@code
* expectedText}.
* expected} value.
*
* <p>Note! The text is extracted by {@link WebElement#getAttribute(String)} method.
*
* @param element element which should be checked
* @param expectedText text which should be presented
* @param expected value which should be presented
* @param timeout waiting time in seconds
*/
public void waitValueContains(WebElement element, String expectedText, int timeout) {
public void waitValueContains(WebElement element, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(
timeout,
() -> format("\nactual value:\n'%s'\ndidn't contain:\n'%s'\n", actual[0], expected))
.until(
(ExpectedCondition<Boolean>)
driver ->
waitVisibility(element, timeout).getAttribute("value").contains(expectedText));
driver -> {
actual[0] = waitVisibilityAndGetValue(element, timeout);
return actual[0].contains(expected);
});
}

/**
Expand Down Expand Up @@ -676,18 +690,22 @@ public void waitValueContains(By elementLocator, String expectedText) {

/**
* Waits during {@code timeout} until text extracted from {@link WebElement} with specified {@code
* elementLocator} by {@link WebElement#getText()} equals to provided {@code expectedText}.
* locator} by {@link WebElement#getText()} equals to provided {@code expected} one.
*
* @param elementLocator locator of element in which text should be checked
* @param expectedText expected text which should be present in the element
* @param locator locator of element in which text should be checked
* @param expected expected text which should be present in the element
* @param timeout waiting time in seconds
*/
public void waitTextEqualsTo(By elementLocator, String expectedText, int timeout) {
public void waitTextEqualsTo(By locator, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(timeout, () -> format("\nexpected:\n'%s'\nbut was:\n'%s'\n", expected, actual[0]))
.until(
(ExpectedCondition<Boolean>)
driver -> waitVisibilityAndGetText(elementLocator, timeout).equals(expectedText));
driver -> {
actual[0] = waitVisibilityAndGetText(locator, timeout);
return actual[0].equals(expected);
});
}

/**
Expand All @@ -702,48 +720,58 @@ public void waitTextEqualsTo(By elementLocator, String expectedText) {
}

/**
* Waits during {@code timeout} until text extracted from specified {@code webElement} by {@link
* WebElement#getText()} equals to provided {@code expectedText}.
* Waits during {@code timeout} until text extracted from specified {@code element} by {@link
* WebElement#getText()} equals to provided {@code expected} one.
*
* @param webElement element in which text should be checked
* @param expectedText expected text which should be present in the element
* @param element element in which text should be checked
* @param expected expected text which element text should be equal to
* @param timeout waiting time in seconds
*/
public void waitTextEqualsTo(WebElement webElement, String expectedText, int timeout) {
public void waitTextEqualsTo(WebElement element, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(timeout, () -> format("\nexpected:\n'%s'\nbut was:\n'%s'\n", expected, actual[0]))
.until(
(ExpectedCondition<Boolean>)
driver -> waitVisibilityAndGetText(webElement, timeout).equals(expectedText));
driver -> {
actual[0] = waitVisibilityAndGetText(element, timeout);
return actual[0].equals(expected);
});
}

/**
* Waits until text extracted from {@code webElement} by {@link WebElement#getText()} equals to
* provided {@code expectedText}.
*
* @param webElement element in which text should be checked
* @param expectedText expected text which should be present in the element
* @param expectedText expected text which element text should be equal to
*/
public void waitTextEqualsTo(WebElement webElement, String expectedText) {
waitTextEqualsTo(webElement, expectedText, DEFAULT_TIMEOUT);
}

/**
* Waits during {@code timeout} until specified {@code element} contains the specified {@code
* expectedText}.
* expected} one.
*
* <p>Note! Text is extracted by {@link WebElement#getText()} method.
*
* @param element element which should be checked
* @param expectedText text which should be presented
* @param expected text which should be presented
* @param timeout waiting time in seconds
*/
public void waitTextContains(WebElement element, String expectedText, int timeout) {
public void waitTextContains(WebElement element, String expected, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(
timeout,
() -> format("\nactual text:\n'%s'\ndidn't contain:\n'%s'\n", actual[0], expected))
.until(
(ExpectedCondition<Boolean>)
driver -> waitVisibility(element, timeout).getText().contains(expectedText));
driver -> {
actual[0] = waitVisibilityAndGetText(element, timeout);
return actual[0].contains(expected);
});
}

/**
Expand Down Expand Up @@ -787,33 +815,39 @@ public void waitTextContains(By elementLocator, String expectedText) {

/**
* Waits during {@code timeout} until specified {@code element} does not contain the specified
* {@code expectedText}.
* {@code absentText}.
*
* <p>Note! Text is extracted by {@link WebElement#getText()} method.
*
* @param element element which should be checked
* @param expectedText text which should not be presented
* @param webElement element which should be checked
* @param absentText text which should be absent
* @param timeout waiting time in seconds
*/
public void waitTextIsNotPresented(WebElement element, String expectedText, int timeout) {
public void waitTextIsNotPresented(WebElement webElement, String absentText, int timeout) {
String[] actual = new String[1];
webDriverWaitFactory
.get(timeout)
.get(
timeout,
() -> format("\nactual text:\n'%s'\ndid contain:\n'%s'\n", actual[0], absentText))
.until(
(ExpectedCondition<Boolean>)
driver -> !(waitVisibilityAndGetText(element, timeout).contains(expectedText)));
driver -> {
actual[0] = waitVisibilityAndGetText(webElement, timeout);
return !actual[0].contains(absentText);
});
}

/**
* Waits until {@link WebElement} which defined by {@code element} does not contain the specified
* {@code expectedText}.
* {@code absentText}.
*
* <p>Note! Text is extracted by {@link WebElement#getText()} method.
*
* @param element element which should be checked
* @param expectedText text which should not be presented
* @param absentText text which should be absent
*/
public void waitTextIsNotPresented(WebElement element, String expectedText) {
waitTextIsNotPresented(element, expectedText, DEFAULT_TIMEOUT);
public void waitTextIsNotPresented(WebElement element, String absentText) {
waitTextIsNotPresented(element, absentText, DEFAULT_TIMEOUT);
}

/**
Expand Down Expand Up @@ -1182,7 +1216,7 @@ public void waitAttributeEqualsTo(

/**
* Waits during {@code timeout} until attribute with specified {@code attributeName} has {@code
* expectedValue}.
* expected} value.
*
* @param element element which contains attribute
* @param attributeName name of the attribute
Expand All @@ -1191,13 +1225,20 @@ public void waitAttributeEqualsTo(
*/
public void waitAttributeEqualsTo(
WebElement element, String attributeName, String expectedValue, int timeout) {
String[] actualValue = new String[1];
webDriverWaitFactory
.get(timeout)
.get(
timeout,
() ->
format(
"\nexpected value of attribute '%s' was:\n'%s'\nbut actual was:\n'%s'\n",
attributeName, expectedValue, actualValue[0]))
.until(
(ExpectedCondition<Boolean>)
driver ->
waitVisibilityAndGetAttribute(element, attributeName, timeout)
.equals(expectedValue));
driver -> {
actualValue[0] = waitVisibilityAndGetAttribute(element, attributeName, timeout);
return actualValue[0].contains(expectedValue);
});
}

/**
Expand Down Expand Up @@ -1227,24 +1268,31 @@ public void waitAttributeEqualsTo(
* Waits during {@code timeout} until attribute with specified {@code attributeName} contains
* {@code expectedValue}.
*
* @param elementLocator element which contains attribute
* @param locator element which contains attribute
* @param attributeName name of the attribute
* @param expectedValue expected attribute value
* @param timeout waiting time in seconds
*/
public void waitAttributeContainsValue(
By elementLocator, String attributeName, String expectedValue, int timeout) {
By locator, String attributeName, String expectedValue, int timeout) {
String[] actualValue = new String[1];
webDriverWaitFactory
.get(timeout)
.get(
timeout,
() ->
format(
"\nactual attribute '%s' value:\n'%s'\ndidn't contain:\n'%s'\n",
attributeName, actualValue[0], expectedValue))
.until(
(ExpectedCondition<Boolean>)
driver ->
waitVisibilityAndGetAttribute(elementLocator, attributeName, timeout)
.contains(expectedValue));
driver -> {
actualValue[0] = waitVisibilityAndGetAttribute(locator, attributeName, timeout);
return actualValue[0].contains(expectedValue);
});
}

/**
* Waits until attribute with specified {@code attributeName} contains {@code expectedValue}.
* ~ Waits until attribute with specified {@code attributeName} contains {@code expectedValue}.
*
* @param elementLocator element which contains attribute
* @param attributeName name of the attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.eclipse.che.selenium.core.constant.TestTimeoutsConstants.LOAD_PAGE_TIMEOUT_SEC;

import com.google.common.base.Supplier;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;

@Singleton
public class WebDriverWaitFactory {
private SeleniumWebDriver seleniumWebDriver;
private Map<Integer, WebDriverWait> webDriverWaits = new HashMap<>();

@Inject
public WebDriverWaitFactory(SeleniumWebDriver seleniumWebDriver) {
Expand All @@ -36,16 +36,11 @@ public WebDriverWait get() {
}

public WebDriverWait get(int timeoutInSec) {
if (!webDriverWaits.isEmpty()) {
if (webDriverWaits.containsKey(timeoutInSec)) {
return webDriverWaits.get(timeoutInSec);
}
}

WebDriverWait webDriverWait = new WebDriverWait(seleniumWebDriver, timeoutInSec);
webDriverWaits.put(timeoutInSec, webDriverWait);
return new WebDriverWait(seleniumWebDriver, timeoutInSec);
}

return webDriverWait;
public FluentWait<WebDriver> get(int timeoutInSec, Supplier<String> messageSupplier) {
return new WebDriverWait(seleniumWebDriver, timeoutInSec).withMessage(messageSupplier);
}

/**
Expand Down

0 comments on commit 15072ae

Please sign in to comment.