logElements = driver.findElements(By.xpath(String
.format("//div[@class='v-debugwindow-row %s']/span[@class='v-debugwindow-message']",
level.getName())));
if (!logElements.isEmpty()) {
String logRows = "";
for (WebElement e : logElements) {
logRows += "\n" + e.getText();
}
Assert.fail("Found debug messages with level " + level.getName()
+ ": " + logRows);
}
}
/**
* Should the "require window focus" be enabled for Internet Explorer.
* RequireWindowFocus makes tests more stable but seems to be broken with
* certain commands such as sendKeys. Therefore it is not enabled by default
* for all tests
*
* @return true, to use the "require window focus" feature, false otherwise
*/
protected boolean requireWindowFocusForIE() {
return false;
}
/**
* Should the "enable persistent hover" be enabled for Internet Explorer.
*
* Persistent hovering causes continuous firing of mouse over events at the
* last location the mouse cursor has been moved to. This is to avoid
* problems where the real mouse cursor is inside the browser window and
* Internet Explorer uses that location for some undefined operation
* (http://
* jimevansmusic.blogspot.fi/2012/06/whats-wrong-with-internet-explorer
* .html)
*
* @return true, to use the "persistent hover" feature, false otherwise
*/
protected boolean usePersistentHoverForIE() {
return true;
}
/**
* Should the "native events" be enabled for Internet Explorer.
*
* Native events sometimes cause failure in clicking on buttons/checkboxes
* but are possibly needed for some operations.
*
* @return true, to use "native events", false to use generated Javascript
* events
*/
protected boolean useNativeEventsForIE() {
return true;
}
// FIXME: Remove this once TB4 getRemoteControlName works properly
private RemoteWebDriver getRemoteDriver() {
WebDriver d = getDriver();
if (d instanceof TestBenchDriverProxy) {
try {
Field f = TestBenchDriverProxy.class
.getDeclaredField("actualDriver");
f.setAccessible(true);
return (RemoteWebDriver) f.get(d);
} catch (Exception e) {
e.printStackTrace();
}
}
if (d instanceof RemoteWebDriver) {
return (RemoteWebDriver) d;
}
return null;
}
// FIXME: Remove this once TB4 getRemoteControlName works properly
protected String getRemoteControlName() {
try {
RemoteWebDriver d = getRemoteDriver();
if (d == null) {
return null;
}
HttpCommandExecutor ce = (HttpCommandExecutor) d
.getCommandExecutor();
String hostName = ce.getAddressOfRemoteServer().getHost();
int port = ce.getAddressOfRemoteServer().getPort();
HttpHost host = new HttpHost(hostName, port);
DefaultHttpClient client = new DefaultHttpClient();
URL sessionURL = new URL("http://" + hostName + ":" + port
+ "/grid/api/testsession?session=" + d.getSessionId());
BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest(
"POST", sessionURL.toExternalForm());
HttpResponse response = client.execute(host, r);
JsonObject object = extractObject(response);
URL myURL = new URL(object.getString("proxyId"));
if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
return myURL.getHost();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected boolean logContainsText(String string) {
List logs = getLogs();
for (String text : logs) {
if (text.contains(string)) {
return true;
}
}
return false;
}
protected List getLogs() {
VerticalLayoutElement log = $(VerticalLayoutElement.class).id("Log");
List logLabels = log.$(LabelElement.class).all();
List logTexts = new ArrayList();
for (LabelElement label : logLabels) {
logTexts.add(label.getText());
}
return logTexts;
}
private static JsonObject extractObject(HttpResponse resp)
throws IOException {
InputStream contents = resp.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(contents, writer, "UTF8");
return JsonUtil.parse(writer.toString());
}
protected void click(CheckBoxElement checkbox) {
WebElement cb = checkbox.findElement(By.xpath("input"));
if (BrowserUtil.isChrome(getDesiredCapabilities())) {
testBenchElement(cb).click(0, 0);
} else if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
getTestBenchCommandExecutor().executeScript("arguments[0].click();",
cb);
} else {
cb.click();
}
}
protected boolean isLoadingIndicatorVisible() {
WebElement loadingIndicator = findElement(
By.className("v-loading-indicator"));
return loadingIndicator.isDisplayed();
}
protected void waitUntilLoadingIndicatorVisible() {
waitUntil(new ExpectedCondition() {
@Override
public Boolean apply(WebDriver input) {
return isLoadingIndicatorVisible();
}
});
}
protected void waitUntilLoadingIndicatorNotVisible() {
waitUntil(new ExpectedCondition() {
@Override
public Boolean apply(WebDriver input) {
return !isLoadingIndicatorVisible();
}
});
}
/**
* Selects a menu item. By default, this will click on the menu item.
*
* @param menuCaption
* caption of the menu item
*/
protected void selectMenu(String menuCaption) {
selectMenu(menuCaption, true);
}
/**
* Selects a menu item.
*
* @param menuCaption
* caption of the menu item
* @param click
* true
if should click the menu item;
* false
if not
*/
protected void selectMenu(String menuCaption, boolean click) {
WebElement menuElement = getMenuElement(menuCaption);
Dimension size = menuElement.getSize();
new Actions(getDriver())
.moveToElement(menuElement, size.width - 10, size.height / 2)
.perform();
if (click) {
new Actions(getDriver()).click().perform();
}
}
/**
* Finds the menu item from the DOM based on menu item caption.
*
* @param menuCaption
* caption of the menu item
* @return the found menu item
* @throws NoSuchElementException
* if menu item is not found
*/
protected WebElement getMenuElement(String menuCaption)
throws NoSuchElementException {
return getDriver().findElement(
By.xpath("//span[text() = '" + menuCaption + "']"));
}
/**
* Selects a submenu described by a path of menus from the first MenuBar in
* the UI.
*
* @param menuCaptions
* array of menu captions
*/
protected void selectMenuPath(String... menuCaptions) {
selectMenu(menuCaptions[0], true);
// Make sure menu popup is opened.
waitUntil(new ExpectedCondition() {
@Override
public Boolean apply(WebDriver webDriver) {
return isElementPresent(By.className("gwt-MenuBarPopup"))
|| isElementPresent(By.className("v-menubar-popup"));
}
});
// Move to the menu item opened below the menu bar.
new Actions(getDriver())
.moveByOffset(0,
getMenuElement(menuCaptions[0]).getSize().getHeight())
.perform();
for (int i = 1; i < menuCaptions.length - 1; i++) {
selectMenu(menuCaptions[i]);
new Actions(getDriver()).moveByOffset(40, 0).build().perform();
}
selectMenu(menuCaptions[menuCaptions.length - 1], true);
}
/**
* Asserts that an element is present
*
* @param by
* the locatore for the element
*/
protected void assertElementPresent(By by) {
Assert.assertTrue("Element is not present", isElementPresent(by));
}
/**
* Asserts that an element is not present
*
* @param by
* the locatore for the element
*/
protected void assertElementNotPresent(By by) {
Assert.assertFalse("Element is present", isElementPresent(by));
}
/**
* Asserts that no error notifications are shown. Requires the use of
* "?debug" as exceptions are otherwise not shown as notifications.
*/
protected void assertNoErrorNotifications() {
Assert.assertFalse(
"Error notification with client side exception is shown",
isNotificationPresent("error"));
}
/**
* Asserts that no system notifications are shown.
*/
protected void assertNoSystemNotifications() {
Assert.assertFalse(
"Error notification with system error exception is shown",
isNotificationPresent("system"));
}
/**
* Asserts that a system notification is shown.
*/
protected void assertSystemNotification() {
Assert.assertTrue(
"Error notification with system error exception is not shown",
isNotificationPresent("system"));
}
private boolean isNotificationPresent(String type) {
if ("error".equals(type)) {
Assert.assertTrue(
"Debug window must be open to be able to see error notifications",
isDebugWindowOpen());
}
return isElementPresent(By.className("v-Notification-" + type));
}
private boolean isDebugWindowOpen() {
return isElementPresent(By.className("v-debugwindow"));
}
protected void assertNoHorizontalScrollbar(WebElement element,
String errorMessage) {
// IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
// up, so using clientWidth/clientHeight will fail if the element height
// is not an integer
int clientWidth = getClientWidth(element);
int scrollWidth = getScrollWidth(element);
boolean hasScrollbar = scrollWidth > clientWidth;
Assert.assertFalse(
"The element should not have a horizontal scrollbar (scrollWidth: "
+ scrollWidth + ", clientWidth: " + clientWidth + "): "
+ errorMessage,
hasScrollbar);
}
protected void assertNoVerticalScrollbar(WebElement element,
String errorMessage) {
// IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
// up, so using clientWidth/clientHeight will fail if the element height
// is not an integer
int clientHeight = getClientHeight(element);
int scrollHeight = getScrollHeight(element);
boolean hasScrollbar = scrollHeight > clientHeight;
Assert.assertFalse(
"The element should not have a vertical scrollbar (scrollHeight: "
+ scrollHeight + ", clientHeight: " + clientHeight
+ "): " + errorMessage,
hasScrollbar);
}
protected int getScrollHeight(WebElement element) {
return ((Number) executeScript("return arguments[0].scrollHeight;",
element)).intValue();
}
protected int getScrollWidth(WebElement element) {
return ((Number) executeScript("return arguments[0].scrollWidth;",
element)).intValue();
}
/**
* Returns client height rounded up instead of as double because of IE9
* issues: https://dev.vaadin.com/ticket/18469
*/
protected int getClientHeight(WebElement e) {
String script;
if (BrowserUtil.isIE8(getDesiredCapabilities())) {
script = "return arguments[0].clientHeight;"; //
} else {
script = "var cs = window.getComputedStyle(arguments[0]);"
+ "return Math.ceil(parseFloat(cs.height)+parseFloat(cs.paddingTop)+parseFloat(cs.paddingBottom));";
}
return ((Number) executeScript(script, e)).intValue();
}
/**
* Returns client width rounded up instead of as double because of IE9
* issues: https://dev.vaadin.com/ticket/18469
*/
protected int getClientWidth(WebElement e) {
String script;
if (BrowserUtil.isIE8(getDesiredCapabilities())) {
script = "return arguments[0].clientWidth;";
} else {
script = "var cs = window.getComputedStyle(arguments[0]);"
+ "var h = parseFloat(cs.width)+parseFloat(cs.paddingLeft)+parseFloat(cs.paddingRight);"
+ "return Math.ceil(h);";
}
return ((Number) executeScript(script, e)).intValue();
}
protected void assertElementsEquals(WebElement expectedElement,
WebElement actualElement) {
while (expectedElement instanceof WrapsElement) {
expectedElement = ((WrapsElement) expectedElement)
.getWrappedElement();
}
while (actualElement instanceof WrapsElement) {
actualElement = ((WrapsElement) actualElement).getWrappedElement();
}
Assert.assertEquals(expectedElement, actualElement);
}
protected WebElement getActiveElement() {
return (WebElement) executeScript("return document.activeElement;");
}
protected void waitForThemeToChange(final String theme) {
final WebElement rootDiv = findElement(
By.xpath("//div[contains(@class,'v-app')]"));
waitUntil(new ExpectedCondition() {
@Override
public Boolean apply(WebDriver input) {
String rootClass = rootDiv.getAttribute("class").trim();
return rootClass.contains(theme);
}
}, 30);
}
}