From 642818fef200429d4206403e98aabd54ff3b6dd8 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 14 Nov 2013 09:38:05 +0200 Subject: [PATCH] Allow using @RunLocally to run on a local Firefox/Chrome/Safari Change-Id: Ibb1dfd12dc48637d8179f80322b5203ea5562805 --- .../tests/VerifyBrowserVersionTest.java | 13 ++- .../ActionsOnInvisibleComponentsTest.java | 8 +- ...electsAndChromeKeyboardNavigationTest.java | 30 +----- .../integration/AbstractIntegrationTest.java | 5 +- .../com/vaadin/tests/tb3/AbstractTB3Test.java | 96 ++++++++++++++----- .../vaadin/tests/tb3/MultiBrowserTest.java | 35 ++++--- .../tests/tb3/PrivateTB3Configuration.java | 33 +++++-- .../src/com/vaadin/tests/tb3/TB3Runner.java | 8 +- .../com/vaadin/tests/tb3/WebsocketTest.java | 6 +- 9 files changed, 140 insertions(+), 94 deletions(-) diff --git a/uitest/src/com/vaadin/tests/VerifyBrowserVersionTest.java b/uitest/src/com/vaadin/tests/VerifyBrowserVersionTest.java index ae5806af56..bb0b40e2d1 100644 --- a/uitest/src/com/vaadin/tests/VerifyBrowserVersionTest.java +++ b/uitest/src/com/vaadin/tests/VerifyBrowserVersionTest.java @@ -30,23 +30,22 @@ public class VerifyBrowserVersionTest extends MultiBrowserTest { { expectedUserAgent - .put(BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION), + .put(Browser.FIREFOX.getDesiredCapabilities(), "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0"); expectedUserAgent - .put(BrowserUtil.ie(8), + .put(Browser.IE8.getDesiredCapabilities(), "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); expectedUserAgent - .put(BrowserUtil.ie(9), + .put(Browser.IE9.getDesiredCapabilities(), "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); expectedUserAgent - .put(BrowserUtil.ie(10), + .put(Browser.IE10.getDesiredCapabilities(), "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"); expectedUserAgent - .put(BrowserUtil.ie(11), + .put(Browser.IE11.getDesiredCapabilities(), "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"); expectedUserAgent - .put(BrowserUtil.chrome(MultiBrowserTest.TESTED_CHROME_VERSION), + .put(Browser.CHROME.getDesiredCapabilities(), "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36"); } diff --git a/uitest/src/com/vaadin/tests/actions/ActionsOnInvisibleComponentsTest.java b/uitest/src/com/vaadin/tests/actions/ActionsOnInvisibleComponentsTest.java index ca00c998a6..1d08ee5ede 100644 --- a/uitest/src/com/vaadin/tests/actions/ActionsOnInvisibleComponentsTest.java +++ b/uitest/src/com/vaadin/tests/actions/ActionsOnInvisibleComponentsTest.java @@ -18,12 +18,12 @@ public class ActionsOnInvisibleComponentsTest extends MultiBrowserTest { public List getBrowsersToTest() { List browsers = super.getBrowsersToTest(); // sendKeys does nothing on these browsers - browsers.remove(BrowserUtil.firefox(24)); - browsers.remove(BrowserUtil.ie(8)); - browsers.remove(BrowserUtil.opera(12)); + browsers.remove(Browser.FIREFOX.getDesiredCapabilities()); + browsers.remove(Browser.IE8.getDesiredCapabilities()); + browsers.remove(Browser.OPERA.getDesiredCapabilities()); // Causes 'cannot focus element' - browsers.remove(BrowserUtil.chrome(29)); + browsers.remove(Browser.CHROME.getDesiredCapabilities()); return browsers; } diff --git a/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsAndChromeKeyboardNavigationTest.java b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsAndChromeKeyboardNavigationTest.java index 364d3bd8d4..e038c451d0 100644 --- a/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsAndChromeKeyboardNavigationTest.java +++ b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsAndChromeKeyboardNavigationTest.java @@ -22,7 +22,6 @@ import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.Keys; -import org.openqa.selenium.Platform; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.remote.DesiredCapabilities; @@ -44,9 +43,8 @@ public class NativeSelectsAndChromeKeyboardNavigationTest extends */ @Override public List getBrowsersToTest() { - DesiredCapabilities chrome = DesiredCapabilities.chrome(); - chrome.setPlatform(Platform.WINDOWS); - return Collections.singletonList(chrome); + return Collections.singletonList(Browser.CHROME + .getDesiredCapabilities()); } @Test @@ -84,30 +82,6 @@ public class NativeSelectsAndChromeKeyboardNavigationTest extends return NativeSelects.class; } - // Uncomment this to debug test in local/portforwarded chromedriver - // @Override - // protected void setupLocalDriver() { - // WebDriver chromeDriver; - // try { - // chromeDriver = new RemoteWebDriver( - // new URL("http://localhost:9515"), getBrowsersToTest() - // .iterator().next()); - // setDriver(chromeDriver); - // } catch (MalformedURLException e) { - // e.printStackTrace(); - // } - // } - // - // /* - // * (non-Javadoc) - // * - // * @see com.vaadin.tests.tb3.AbstractTB3Test#runLocally() - // */ - // @Override - // public boolean runLocally() { - // return false; - // } - /** * @since * @param string diff --git a/uitest/src/com/vaadin/tests/integration/AbstractIntegrationTest.java b/uitest/src/com/vaadin/tests/integration/AbstractIntegrationTest.java index ddaf84b3e1..cbb3a8b8e4 100644 --- a/uitest/src/com/vaadin/tests/integration/AbstractIntegrationTest.java +++ b/uitest/src/com/vaadin/tests/integration/AbstractIntegrationTest.java @@ -22,7 +22,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized.Parameters; import org.openqa.selenium.remote.DesiredCapabilities; -import com.vaadin.tests.tb3.MultiBrowserTest; +import com.vaadin.tests.tb3.MultiBrowserTest.Browser; import com.vaadin.tests.tb3.PrivateTB3Configuration; /** @@ -47,8 +47,7 @@ public abstract class AbstractIntegrationTest extends PrivateTB3Configuration { @Parameters public static Collection getBrowsersForTest() { - return Collections.singleton(BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION)); + return Collections.singleton(Browser.FIREFOX.getDesiredCapabilities()); } } diff --git a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java index d4eed99f19..f6e58a51b2 100644 --- a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java +++ b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java @@ -16,6 +16,10 @@ package com.vaadin.tests.tb3; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.net.URL; import java.util.Collections; import java.util.List; @@ -39,6 +43,7 @@ import com.vaadin.server.UIProvider; import com.vaadin.testbench.TestBench; import com.vaadin.testbench.TestBenchTestCase; import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.tb3.MultiBrowserTest.Browser; import com.vaadin.ui.UI; /** @@ -76,8 +81,7 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { private boolean push = false; { // Default browser to run on unless setDesiredCapabilities is called - desiredCapabilities = BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION); + desiredCapabilities = Browser.FIREFOX.getDesiredCapabilities(); } /** @@ -103,8 +107,9 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * If something goes wrong */ protected void setupDriver() throws Exception { - if (runLocally()) { - setupLocalDriver(); + RunLocally runLocally = getClass().getAnnotation(RunLocally.class); + if (runLocally != null) { + setupLocalDriver(runLocally.value().getDesiredCapabilities()); return; } DesiredCapabilities capabilities = getDesiredCapabilities(); @@ -129,14 +134,10 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { } - /** - * Override and return true to run the test locally. This method is only to - * be used for developing tests. - * - * @return true to run the test on a local browser, false to use the hub - */ - public boolean runLocally() { - return false; + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface RunLocally { + public Browser value() default Browser.FIREFOX; } /** @@ -144,7 +145,8 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * for debug purposes. Used only when {@link #runLocally()} is overridden to * return true; */ - protected abstract void setupLocalDriver(); + protected abstract void setupLocalDriver( + DesiredCapabilities desiredCapabilities); /** * Opens the given test (defined by {@link #getTestUrl()}, optionally with @@ -212,9 +214,8 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @return The browsers to run the test on */ public List getBrowsersToTest() { - return Collections.singletonList(BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION)); - + return Collections.singletonList(Browser.FIREFOX + .getDesiredCapabilities()); } /** @@ -738,8 +739,53 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { * @return true if the capabilities refer to IE8, false otherwise */ public static boolean isIE8(DesiredCapabilities capabilities) { - return BrowserType.IE.equals(capabilities.getBrowserName()) - && "8".equals(capabilities.getVersion()); + return isIE(capabilities) && "8".equals(capabilities.getVersion()); + } + + /** + * @param capabilities + * The capabilities to check + * @return true if the capabilities refer to Internet Explorer, false + * otherwise + */ + public static boolean isIE(DesiredCapabilities capabilities) { + return BrowserType.IE.equals(capabilities.getBrowserName()); + } + + /** + * @param capabilities + * The capabilities to check + * @return true if the capabilities refer to Chrome, false otherwise + */ + public static boolean isChrome(DesiredCapabilities capabilities) { + return BrowserType.CHROME.equals(capabilities.getBrowserName()); + } + + /** + * @param capabilities + * The capabilities to check + * @return true if the capabilities refer to Opera, false otherwise + */ + public static boolean isOpera(DesiredCapabilities capabilities) { + return BrowserType.OPERA.equals(capabilities.getBrowserName()); + } + + /** + * @param capabilities + * The capabilities to check + * @return true if the capabilities refer to Safari, false otherwise + */ + public static boolean isSafari(DesiredCapabilities capabilities) { + return BrowserType.SAFARI.equals(capabilities.getBrowserName()); + } + + /** + * @param capabilities + * The capabilities to check + * @return true if the capabilities refer to Firefox, false otherwise + */ + public static boolean isFirefox(DesiredCapabilities capabilities) { + return BrowserType.FIREFOX.equals(capabilities.getBrowserName()); } /** @@ -751,21 +797,19 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { */ public static String getBrowserIdentifier( DesiredCapabilities capabilities) { - String browserName = capabilities.getBrowserName(); - - if (BrowserType.IE.equals(browserName)) { + if (isIE(capabilities)) { return "InternetExplorer"; - } else if (BrowserType.FIREFOX.equals(browserName)) { + } else if (isFirefox(capabilities)) { return "Firefox"; - } else if (BrowserType.CHROME.equals(browserName)) { + } else if (isChrome(capabilities)) { return "Chrome"; - } else if (BrowserType.SAFARI.equals(browserName)) { + } else if (isSafari(capabilities)) { return "Safari"; - } else if (BrowserType.OPERA.equals(browserName)) { + } else if (isOpera(capabilities)) { return "Opera"; } - return browserName; + return capabilities.getBrowserName(); } /** diff --git a/uitest/src/com/vaadin/tests/tb3/MultiBrowserTest.java b/uitest/src/com/vaadin/tests/tb3/MultiBrowserTest.java index e8a974343b..e166e421ef 100644 --- a/uitest/src/com/vaadin/tests/tb3/MultiBrowserTest.java +++ b/uitest/src/com/vaadin/tests/tb3/MultiBrowserTest.java @@ -40,23 +40,34 @@ import org.openqa.selenium.remote.DesiredCapabilities; */ public abstract class MultiBrowserTest extends PrivateTB3Configuration { - public static final int TESTED_SAFARI_VERSION = 7; - public static final int TESTED_CHROME_VERSION = 29; - public static final int TESTED_FIREFOX_VERSION = 24; + public enum Browser { + FIREFOX(BrowserUtil.firefox(24)), CHROME(BrowserUtil.chrome(29)), SAFARI( + BrowserUtil.safari(7)), IE8(BrowserUtil.ie(8)), IE9(BrowserUtil + .ie(9)), IE10(BrowserUtil.ie(10)), IE11(BrowserUtil.ie(11)), OPERA( + BrowserUtil.opera(17)); + private DesiredCapabilities desiredCapabilities; + + private Browser(DesiredCapabilities desiredCapabilities) { + this.desiredCapabilities = desiredCapabilities; + } + + public DesiredCapabilities getDesiredCapabilities() { + return desiredCapabilities; + } + } static List allBrowsers = new ArrayList(); static { - allBrowsers.add(BrowserUtil.ie(8)); - allBrowsers.add(BrowserUtil.ie(9)); - allBrowsers.add(BrowserUtil.ie(10)); - allBrowsers.add(BrowserUtil.ie(11)); - allBrowsers.add(BrowserUtil.firefox(TESTED_FIREFOX_VERSION)); + allBrowsers.add(Browser.IE8.getDesiredCapabilities()); + allBrowsers.add(Browser.IE9.getDesiredCapabilities()); + allBrowsers.add(Browser.IE10.getDesiredCapabilities()); + allBrowsers.add(Browser.IE11.getDesiredCapabilities()); + allBrowsers.add(Browser.FIREFOX.getDesiredCapabilities()); // Uncomment once we have the capability to run on Safari 6 - // allBrowsers.add(BrowserUtil.safari(TESTED_SAFARI_VERSION)); - allBrowsers.add(BrowserUtil.chrome(TESTED_CHROME_VERSION)); + // allBrowsers.add(SAFARI); + allBrowsers.add(Browser.CHROME.getDesiredCapabilities()); // Re-enable this when it is possible to run on a modern Opera version - // (15+) - // allBrowsers.add(BrowserUtil.opera(15)); + // allBrowsers.add(Browser.OPERA.getDesiredCapabilities()); } /** diff --git a/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java b/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java index 89fa304bfe..543484fc14 100644 --- a/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java +++ b/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java @@ -26,8 +26,11 @@ import java.util.Enumeration; import java.util.Properties; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.safari.SafariDriver; import com.vaadin.testbench.TestBench; @@ -80,7 +83,7 @@ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { @Override protected String getDeploymentHostname() { - if (runLocally()) { + if (getClass().getAnnotation(RunLocally.class) != null) { return "localhost"; } String hostName = getProperty(HOSTNAME_PROPERTY); @@ -148,17 +151,29 @@ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { * @see com.vaadin.tests.tb3.AbstractTB3Test#setupLocalDriver() */ @Override - protected void setupLocalDriver() { - String firefoxPath = getProperty("firefox.path"); + protected void setupLocalDriver(DesiredCapabilities desiredCapabilities) { WebDriver driver; - if (firefoxPath != null) { - driver = new FirefoxDriver( - new FirefoxBinary(new File(firefoxPath)), null); + if (BrowserUtil.isFirefox(desiredCapabilities)) { + String firefoxPath = getProperty("firefox.path"); + if (firefoxPath != null) { + driver = new FirefoxDriver(new FirefoxBinary(new File( + firefoxPath)), null); + } else { + driver = new FirefoxDriver(); + } + } else if (BrowserUtil.isChrome(desiredCapabilities)) { + System.setProperty("webdriver.chrome.driver", + getProperty("chrome.driver.path")); + driver = new ChromeDriver(); + } else if (BrowserUtil.isSafari(desiredCapabilities)) { + driver = new SafariDriver(); } else { - driver = new FirefoxDriver(); + throw new RuntimeException( + "Not implemented support for running locally on " + + BrowserUtil + .getBrowserIdentifier(desiredCapabilities)); } setDriver(TestBench.createDriver(driver)); - setDesiredCapabilities(BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION)); + setDesiredCapabilities(desiredCapabilities); } } diff --git a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java index 4e084ab0ed..eaffa80d09 100644 --- a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java +++ b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java @@ -33,6 +33,7 @@ import org.junit.runners.model.Statement; import org.openqa.selenium.remote.DesiredCapabilities; import com.vaadin.tests.tb3.AbstractTB3Test.BrowserUtil; +import com.vaadin.tests.tb3.AbstractTB3Test.RunLocally; /** * This runner is loosely based on FactoryTestRunner by Ted Young @@ -76,10 +77,11 @@ public class TB3Runner extends BlockJUnit4ClassRunner { .getOnlyConstructor().newInstance(); Collection desiredCapabilites = testClassInstance .getBrowsersToTest(); - if (testClassInstance.runLocally()) { + if (testClassInstance.getClass().getAnnotation(RunLocally.class) != null) { desiredCapabilites = new ArrayList(); - desiredCapabilites.add(BrowserUtil - .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION)); + desiredCapabilites.add(testClassInstance.getClass() + .getAnnotation(RunLocally.class).value() + .getDesiredCapabilities()); } for (DesiredCapabilities capabilities : desiredCapabilites) { diff --git a/uitest/src/com/vaadin/tests/tb3/WebsocketTest.java b/uitest/src/com/vaadin/tests/tb3/WebsocketTest.java index 26fef667cd..e9ef11957c 100644 --- a/uitest/src/com/vaadin/tests/tb3/WebsocketTest.java +++ b/uitest/src/com/vaadin/tests/tb3/WebsocketTest.java @@ -25,6 +25,8 @@ import java.util.List; import org.openqa.selenium.remote.DesiredCapabilities; +import com.vaadin.tests.tb3.MultiBrowserTest.Browser; + /** * A {@link MultiBrowserTest} which restricts the tests to the browsers which * support websocket @@ -35,8 +37,8 @@ public abstract class WebsocketTest extends PrivateTB3Configuration { private static List websocketBrowsers = new ArrayList(); static { websocketBrowsers.addAll(MultiBrowserTest.getAllBrowsers()); - websocketBrowsers.remove(BrowserUtil.ie(8)); - websocketBrowsers.remove(BrowserUtil.ie(9)); + websocketBrowsers.remove(Browser.IE8.getDesiredCapabilities()); + websocketBrowsers.remove(Browser.IE9.getDesiredCapabilities()); } /** -- 2.39.5