Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Last active December 12, 2015 00:49
Show Gist options
  • Save witoldsz/4686705 to your computer and use it in GitHub Desktop.
Save witoldsz/4686705 to your computer and use it in GitHub Desktop.
package pl.espeo.sgn.e2e;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
/**
* The WebDriver adapter.
* @author Witold Szczerba
*/
public class Driver {
private final String sgnAppRootUrl;
private final WebDriver webDriver;
private final JavascriptExecutor jsExec;
public Driver(String sgnAppRootUrl, WebDriver webDriver, JavascriptExecutor jsExec) {
this.sgnAppRootUrl = sgnAppRootUrl;
this.webDriver = webDriver;
this.jsExec = jsExec;
}
public WebElement find(String description, final By selector) {
jsExec.executeAsyncScript("var callback = arguments[0]; e2eNotifyWhenReady(callback);");
List<WebElement> elements = webDriver.findElements(selector);
if (elements.isEmpty()) {
throw new RuntimeException("Cannot find: "+describe(description, selector));
}
return elements.get(0);
}
public String valueByNgModel(String tag, String ngModel) {
return findByNgModel(tag, ngModel).getAttribute("value");
}
public WebElement findByNgModel(String tag, String ngModel) throws RuntimeException {
String selector = tag + "[ng-model='" + ngModel + "']";
WebElement webElement;
try {
webElement = find("", By.cssSelector(selector));
} catch (WebDriverException ex) {
throw new RuntimeException("Invalid CSS selector: " + selector);
}
return webElement;
}
public void goTo(String path) {
jsExec.executeAsyncScript("var callback = arguments[0]; e2eNotifyWhenReady(callback);");
String prefix = path.startsWith("/") ? "#" : "#/";
webDriver.navigate().to(sgnAppRootUrl + prefix + path);
}
private String describe(String description, By selector) {
return StringUtils.isEmpty(description) ? selector.toString() : (description + " (" + selector + ")");
}
}
angular.module('webdriver-e2e', [])
.run(function($browser) {
window.e2eNotifyWhenReady = function(callbackWhenReadyFn) {
$browser.notifyWhenNoOutstandingRequests(callbackWhenReadyFn);
}
});
@witoldsz
Copy link
Author

Waiting for e2eNotifyWhenReady in Driver#goTo was crucial to stop random test fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment