diff options
Diffstat (limited to 'uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java')
-rw-r--r-- | uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java | 125 |
1 files changed, 107 insertions, 18 deletions
diff --git a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java index 8dd10216d2..1c5fb380ab 100644 --- a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java +++ b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java @@ -16,6 +16,8 @@ package com.vaadin.tests.tb3; +import static com.vaadin.tests.tb3.TB3Runner.localWebDriverIsUsed; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -24,11 +26,15 @@ import java.net.URL; import java.util.Collections; import java.util.List; -import com.vaadin.testbench.TestBenchElement; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; -import org.openqa.selenium.*; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.Platform; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.interactions.HasInputDevices; import org.openqa.selenium.interactions.Keyboard; import org.openqa.selenium.interactions.Mouse; @@ -45,13 +51,12 @@ import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium; import com.vaadin.server.LegacyApplication; import com.vaadin.server.UIProvider; import com.vaadin.testbench.TestBench; +import com.vaadin.testbench.TestBenchElement; import com.vaadin.testbench.TestBenchTestCase; import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.tests.tb3.MultiBrowserTest.Browser; import com.vaadin.ui.UI; -import static com.vaadin.tests.tb3.TB3Runner.localWebDriverIsUsed; - /** * Base class for TestBench 3+ tests. All TB3+ tests in the project should * extend this class. @@ -85,6 +90,8 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { */ private static final int BROWSER_TIMEOUT_IN_MS = 30 * 1000; + private static final int BROWSER_INIT_ATTEMPTS = 5; + private DesiredCapabilities desiredCapabilities; private boolean debug = false; @@ -134,9 +141,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { if (localWebDriverIsUsed()) { setupLocalDriver(capabilities); } else { - WebDriver dr = TestBench.createDriver(new RemoteWebDriver( - new URL(getHubURL()), capabilities)); - setDriver(dr); + setupRemoteDriver(capabilities); } } @@ -166,7 +171,8 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { } protected WebElement getTooltipElement() { - return getDriver().findElement(com.vaadin.testbench.By.className("v-tooltip-text")); + return getDriver().findElement( + com.vaadin.testbench.By.className("v-tooltip-text")); } protected Coordinates getCoordinates(TestBenchElement element) { @@ -218,12 +224,53 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { DesiredCapabilities desiredCapabilities); /** + * Creates a {@link WebDriver} instance used for running the test remotely. + * + * @since + * @param capabilities + * the type of browser needed + * @throws Exception + */ + private void setupRemoteDriver(DesiredCapabilities capabilities) + throws Exception { + for (int i = 1; i <= BROWSER_INIT_ATTEMPTS; i++) { + try { + WebDriver dr = TestBench.createDriver(new RemoteWebDriver( + new URL(getHubURL()), capabilities)); + setDriver(dr); + } catch (Exception e) { + System.err.println("Browser startup for " + capabilities + + " failed on attempt " + i + ": " + e.getMessage()); + if (i == BROWSER_INIT_ATTEMPTS) { + throw e; + } + } + } + + } + + /** * Opens the given test (defined by {@link #getTestUrl()}, optionally with * debug window and/or push (depending on {@link #isDebug()} and * {@link #isPush()}. */ protected void openTestURL() { - driver.get(getTestUrl()); + openTestURL(""); + } + + /** + * Opens the given test (defined by {@link #getTestUrl()}, optionally with + * debug window and/or push (depending on {@link #isDebug()} and + * {@link #isPush()}. + */ + protected void openTestURL(String extraParameters) { + String url = getTestUrl(); + if (url.contains("?")) { + url = url + "&" + extraParameters; + } else { + url = url + "?" + extraParameters; + } + driver.get(url); } /** @@ -341,8 +388,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @return Focused element or null */ protected WebElement getFocusedElement() { - Object focusedElement = ((JavascriptExecutor) getDriver()) - .executeScript("return document.activeElement"); + Object focusedElement = executeScript("return document.activeElement"); if (null != focusedElement) { return (WebElement) focusedElement; } else { @@ -351,6 +397,19 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { } /** + * Executes the given Javascript + * + * @param script + * the script to execute + * @return whatever + * {@link org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)} + * returns + */ + protected Object executeScript(String script) { + return ((JavascriptExecutor) getDriver()).executeScript(script); + } + + /** * Find a Vaadin element based on its id given using Component.setId * * @param id @@ -396,7 +455,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @param condition * the condition to wait for to become true */ - protected void waitUntil(ExpectedCondition<Boolean> condition) { + protected <T> void waitUntil(ExpectedCondition<T> condition) { waitUntil(condition, 10); } @@ -408,7 +467,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @param condition * the condition to wait for to become true */ - protected void waitUntil(ExpectedCondition<Boolean> condition, + protected <T> void waitUntil(ExpectedCondition<T> condition, long timeoutInSeconds) { new WebDriverWait(driver, timeoutInSeconds).until(condition); } @@ -421,7 +480,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @param condition * the condition to wait for to become false */ - protected void waitUntilNot(ExpectedCondition<Boolean> condition) { + protected <T> void waitUntilNot(ExpectedCondition<T> condition) { waitUntilNot(condition, 10); } @@ -433,14 +492,42 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @param condition * the condition to wait for to become false */ - protected void waitUntilNot(ExpectedCondition<Boolean> condition, + protected <T> void waitUntilNot(ExpectedCondition<T> condition, long timeoutInSeconds) { waitUntil(ExpectedConditions.not(condition), timeoutInSeconds); } - protected void waitForElementToBePresent(By by) { - waitUntil(ExpectedConditions.not(ExpectedConditions - .invisibilityOfElementLocated(by))); + protected void waitForElementPresent(final By by) { + waitUntil(ExpectedConditions.presenceOfElementLocated(by)); + } + + protected void waitForElementVisible(final By by) { + waitUntil(ExpectedConditions.visibilityOfElementLocated(by)); + } + + /** + * Checks if the given element has the given class name. + * + * Matches only full class names, i.e. has ("foo") does not match + * class="foobar" + * + * @param element + * @param className + * @return + */ + protected boolean hasCssClass(WebElement element, String className) { + String classes = element.getAttribute("class"); + if (classes == null || classes.isEmpty()) { + return (className == null || className.isEmpty()); + } + + for (String cls : classes.split(" ")) { + if (className.equals(cls)) { + return true; + } + } + + return false; } /** @@ -817,6 +904,8 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { public static DesiredCapabilities ie(int version) { DesiredCapabilities c = DesiredCapabilities.internetExplorer(); c.setVersion("" + version); + c.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, + true); return c; } |