Skip to content

Commit

Permalink
Refactoring and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Feb 4, 2016
1 parent 2e2450a commit c4a8fc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,19 @@ protected JavascriptExecutorFacade getJavascriptExecutorFacade() {
public <T extends net.serenitybdd.core.pages.WebElementFacade> T element(WebElement webElement) {
return net.serenitybdd.core.pages.WebElementFacadeImpl.wrapWebElement(driver, webElement,
getImplicitWaitTimeout().in(MILLISECONDS),
getWaitForTimeout().in(MILLISECONDS));
getWaitForTimeout().in(MILLISECONDS),
nameOf(webElement));
}

private String nameOf(WebElement webElement) {
try {
return webElement.toString();
} catch (Exception e) {
return "Unknown web element";
}
}


public <T extends net.serenitybdd.core.pages.WebElementFacade> T $(WebElement webElement) {
return element(webElement);
}
Expand All @@ -892,7 +902,8 @@ public <T extends net.serenitybdd.core.pages.WebElementFacade> T element(By bySe
return net.serenitybdd.core.pages.WebElementFacadeImpl.wrapWebElement(driver,
webElement,
getImplicitWaitTimeout().in(MILLISECONDS),
getWaitForTimeout().in(MILLISECONDS));
getWaitForTimeout().in(MILLISECONDS),
bySelector.toString());
}

public <T extends net.serenitybdd.core.pages.WebElementFacade> T find(List<By> selectors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class WebElementFacadeImpl implements WebElementFacade, net.thucydides.co
private JavascriptExecutorFacade javascriptExecutorFacade;
private InternalSystemClock clock = new InternalSystemClock();
private final EnvironmentVariables environmentVariables;
private String foundBy;

private ElementLocator locator;
private WebElement resolvedELement;
Expand Down Expand Up @@ -111,14 +112,23 @@ public static <T extends WebElementFacade> T wrapWebElement(final WebDriver driv
final WebElement element,
final long timeoutInMilliseconds,
final long waitForTimeoutInMilliseconds) {
return (T) new WebElementFacadeImpl(driver, null, element, timeoutInMilliseconds, waitForTimeoutInMilliseconds);
return (T) new WebElementFacadeImpl(driver, null, element, timeoutInMilliseconds, waitForTimeoutInMilliseconds)
.foundBy("<Undefined web element>");
}

public static <T extends WebElementFacade> T wrapWebElement(final WebDriver driver,
final WebElement element,
final long timeoutInMilliseconds,
final long waitForTimeoutInMilliseconds,
final String foundBy) {
return (T) new WebElementFacadeImpl(driver, null, element, timeoutInMilliseconds, waitForTimeoutInMilliseconds)
.foundBy(foundBy);
}

public static <T extends WebElementFacade> T wrapWebElement(final WebDriver driver,
final WebElement element,
final long timeout) {
return (T) new WebElementFacadeImpl(driver, null, element, timeout, timeout);
return (T) new WebElementFacadeImpl(driver, null, element, timeout, timeout).foundBy(element.toString());

}

Expand Down Expand Up @@ -173,7 +183,8 @@ public <T extends WebElementFacade> T findBy(String xpathOrCssSelector) {
.cssSelector(xpathOrCssSelector)));
}

return wrapWebElement(driver, nestedElement, timeoutInMilliseconds(), waitForTimeoutInMilliseconds);
return wrapWebElement(driver, nestedElement, timeoutInMilliseconds(), waitForTimeoutInMilliseconds,
"element located by " + xpathOrCssSelector);
}


Expand Down Expand Up @@ -202,7 +213,7 @@ public List<WebElementFacade> thenFindAll(String xpathOrCssSelector) {
private List<WebElementFacade> webElementFacadesFrom(List<WebElement> nestedElements) {
List<WebElementFacade> results = Lists.newArrayList();
for (WebElement element : nestedElements) {
results.add(wrapWebElement(driver, element, timeoutInMilliseconds(), waitForTimeoutInMilliseconds));
results.add(wrapWebElement(driver, element, timeoutInMilliseconds(), waitForTimeoutInMilliseconds, element.toString()));
}
return results;
}
Expand All @@ -211,7 +222,8 @@ private List<WebElementFacade> webElementFacadesFrom(List<WebElement> nestedElem
public WebElementFacade findBy(By selector) {
logIfVerbose("findBy " + selector);
WebElement nestedElement = getElement().findElement(selector);
return wrapWebElement(driver, nestedElement, timeoutInMilliseconds(), waitForTimeoutInMilliseconds);
return wrapWebElement(driver, nestedElement, timeoutInMilliseconds(), waitForTimeoutInMilliseconds,
"element located by " + selector.toString());
}

@Override
Expand Down Expand Up @@ -278,7 +290,10 @@ public Duration getImplicitTimeout() {

@Override
public WebElementFacade withTimeoutOf(int timeout, TimeUnit unit) {
return wrapWebElement(driver, getElement(), implicitTimeoutInMilliseconds, TimeUnit.MILLISECONDS.convert(timeout, unit));
return wrapWebElement(driver, getElement(),
implicitTimeoutInMilliseconds,
TimeUnit.MILLISECONDS.convert(timeout, unit),
foundBy);
}

/**
Expand Down Expand Up @@ -466,6 +481,10 @@ public void resetTimeouts() {
}
}

public String getFoundBy() {
return foundBy;
}


class ExtractText implements Converter<WebElement, String> {
public String convert(WebElement from) {
Expand Down Expand Up @@ -975,7 +994,7 @@ private void logClick() {

private void logIfVerbose(String logMessage) {
if (useVerboseLogging()) {
LOGGER.debug(logMessage + " : " + locator.toString());
LOGGER.debug(logMessage + " : " + toString());
}
}

Expand Down Expand Up @@ -1009,8 +1028,7 @@ protected void notifyScreenChange() {

@Override
public String toString() {
String locatorString = (locator != null) ? locator.toString() : "<Undefined web element>";
return (resolvedElement() != null) ? resolvedElement().toString() : locatorString;
return foundBy;
}

public void submit() {
Expand Down Expand Up @@ -1105,4 +1123,9 @@ public boolean hasClass(String cssClassName) {
List<String> cssClasses = Lists.newArrayList(Splitter.on(" ").omitEmptyStrings().trimResults().split(cssClassValue));
return cssClasses.contains(cssClassName.toLowerCase());
}

public WebElementFacade foundBy(String foundBy) {
this.foundBy = foundBy;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected Object newElementInstance() throws NoSuchMethodException, Instantiatio
WebDriver driver = page.getDriver();
if (implementerClass == WebElementFacadeImpl.class) {
// the target constructor is protected; use the static wrapper method
return WebElementFacadeImpl.wrapWebElement(driver, element, implicitTimeoutInMilliseconds, waitForTimeoutInMilliseconds);
return WebElementFacadeImpl.wrapWebElement(driver, element, implicitTimeoutInMilliseconds, waitForTimeoutInMilliseconds, locator.toString());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public void is_currently_enabled_should_be_false_for_an_inexistant_element() thr
assertThat(page.element(page.fieldDoesNotExist).isCurrentlyEnabled(), is(false));
}

@Test
public void is_currently_enabled_should_be_false_for_an_inexistant_web_element_facade() throws InterruptedException {
assertThat(page.fieldDoesNotExist.isCurrentlyEnabled(), is(false));
}

@Test
public void should_wait_for_field_to_be_enabled() throws InterruptedException {
assertThat(page.element(page.initiallyDisabled).isCurrentlyEnabled(), is(false));
Expand Down

0 comments on commit c4a8fc1

Please sign in to comment.