From 88930305be336a7de77f3bebcda98d9f39bf724f Mon Sep 17 00:00:00 2001 From: Calvin Date: Sat, 5 Jan 2013 01:43:40 +0800 Subject: [PATCH] =?UTF-8?q?#146=20Selenium2=E4=BB=A5JUnit=20@Rule=E7=9A=84?= =?UTF-8?q?=E5=BD=A2=E5=BC=8F=EF=BC=8C=E6=94=AF=E6=8C=81=E5=9C=A8=E5=87=BA?= =?UTF-8?q?=E9=94=99=E6=97=B6=EF=BC=8C=E6=88=AA=E5=B1=8F=E5=88=B0target/sc?= =?UTF-8?q?ennshot=E7=9B=AE=E5=BD=95=E3=80=82=E6=94=B9=E8=BF=9B=E5=86=99?= =?UTF-8?q?=E6=B3=95=E3=80=82=E5=8F=8A=E5=85=B6=E4=BB=96Functional=20Test?= =?UTF-8?q?=E5=B0=8F=E7=BB=86=E8=8A=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../functional/BaseFunctionalTestCase.java | 8 +++--- .../functional/BaseSeleniumTestCase.java | 20 ++------------ .../functional/QuickStartServer.java | 5 ++-- .../application.functional.properties | 1 + .../functional/BaseFunctionalTestCase.java | 8 +++--- .../functional/BaseSeleniumTestCase.java | 20 ++------------ .../showcase/functional/ShowcaseServer.java | 5 ++-- .../application.functional.properties | 1 + .../modules/test/selenium/Selenium2.java | 13 ++++++--- .../test/selenium/SeleniumSnapshotRule.java | 27 +++++++++++++++++++ .../test/selenium/WebDriverFactory.java | 16 ++++++----- 11 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 modules/test/src/main/java/org/springside/modules/test/selenium/SeleniumSnapshotRule.java diff --git a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseFunctionalTestCase.java b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseFunctionalTestCase.java index 4a7a28fa..bbdf1781 100644 --- a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseFunctionalTestCase.java +++ b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseFunctionalTestCase.java @@ -38,11 +38,11 @@ public class BaseFunctionalTestCase { @BeforeClass public static void beforeClass() throws Exception { - - baseUrl = propertiesLoader.getProperty("baseUrl", QuickStartServer.BASE_URL); + baseUrl = propertiesLoader.getProperty("baseUrl"); //如果是目标地址是localhost,则启动嵌入式jetty。如果指向远程地址,则不需要启动Jetty. - Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost"); + Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost") + && propertiesLoader.getBoolean("embeddedForLocal"); if (isEmbedded) { startJettyOnce(); @@ -64,7 +64,7 @@ public class BaseFunctionalTestCase { JettyFactory.setTldJarNames(jettyServer, QuickStartServer.TLD_JAR_NAMES); jettyServer.start(); - logger.info("Jetty Server started"); + logger.info("Jetty Server started at {}", baseUrl); } } diff --git a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseSeleniumTestCase.java b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseSeleniumTestCase.java index 0809a435..55171477 100644 --- a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseSeleniumTestCase.java +++ b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/BaseSeleniumTestCase.java @@ -1,17 +1,12 @@ package org.springside.examples.quickstart.functional; -import java.io.File; -import java.io.IOException; - -import org.apache.commons.io.FileUtils; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.springside.modules.test.selenium.Selenium2; +import org.springside.modules.test.selenium.SeleniumSnapshotRule; import org.springside.modules.test.selenium.WebDriverFactory; /** @@ -27,18 +22,7 @@ public class BaseSeleniumTestCase extends BaseFunctionalTestCase { //出错时截屏的规则 @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void failed(Throwable e, Description description) { - File scrFile = s.snapshot(); - String scrFilename = description.getClassName() + "_" + description.getMethodName() + ".png"; - File outputFile = new File("target/screenshot/", scrFilename); - try { - FileUtils.copyFile(scrFile, outputFile); - } catch (IOException ioe) { - } - } - }; + public TestRule snapshotrule = new SeleniumSnapshotRule(s); @BeforeClass public static void init() throws Exception { diff --git a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java index a2c04033..081b87a1 100644 --- a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java +++ b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java @@ -12,7 +12,6 @@ public class QuickStartServer { public static final int PORT = 8080; public static final String CONTEXT = "/quickstart"; - public static final String BASE_URL = "http://localhost:8080/quickstart"; public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web", "springside-core" }; @@ -27,8 +26,8 @@ public class QuickStartServer { try { server.start(); - System.out.println("Server running at " + BASE_URL); - System.out.println("Hit Enter to reload the application"); + System.out.println("Server running at http://localhost:" + PORT + CONTEXT); + System.out.println("Hit Enter to reload the application quickly"); // 等待用户输入回车重载应用. while (true) { diff --git a/examples/quickstart/src/test/resources/application.functional.properties b/examples/quickstart/src/test/resources/application.functional.properties index 0bdc806d..9b2905ba 100644 --- a/examples/quickstart/src/test/resources/application.functional.properties +++ b/examples/quickstart/src/test/resources/application.functional.properties @@ -1,4 +1,5 @@ baseUrl=http://localhost:8082/quickstart +embeddedForLocal=true #selenium settings, options include firefox,ie,chrome,htmlunit,remote:localhost:4444:firefox selenium.driver=firefox diff --git a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseFunctionalTestCase.java b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseFunctionalTestCase.java index 541878e4..c27301eb 100644 --- a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseFunctionalTestCase.java +++ b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseFunctionalTestCase.java @@ -33,9 +33,10 @@ public class BaseFunctionalTestCase { @BeforeClass public static void beforeClass() throws Exception { - baseUrl = propertiesLoader.getProperty("baseUrl", ShowcaseServer.BASE_URL); + baseUrl = propertiesLoader.getProperty("baseUrl"); - Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost"); + Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost") + && propertiesLoader.getBoolean("embeddedForLocal"); if (isEmbedded) { startJettyOnce(); @@ -57,7 +58,7 @@ public class BaseFunctionalTestCase { JettyFactory.setTldJarNames(jettyServer, ShowcaseServer.TLD_JAR_NAMES); jettyServer.start(); - logger.info("Jetty Server started at " + baseUrl); + logger.info("Jetty Server started at {}", baseUrl); } } @@ -82,5 +83,4 @@ public class BaseFunctionalTestCase { protected static void reloadSampleData() throws Exception { DataFixtures.executeScript(dataSource, "classpath:data/cleanup-data.sql", "classpath:data/import-data.sql"); } - } diff --git a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseSeleniumTestCase.java b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseSeleniumTestCase.java index 499300ef..12086dca 100644 --- a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseSeleniumTestCase.java +++ b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/BaseSeleniumTestCase.java @@ -1,16 +1,11 @@ package org.springside.examples.showcase.functional; -import java.io.File; -import java.io.IOException; - -import org.apache.commons.io.FileUtils; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.openqa.selenium.WebDriver; import org.springside.modules.test.selenium.Selenium2; +import org.springside.modules.test.selenium.SeleniumSnapshotRule; import org.springside.modules.test.selenium.WebDriverFactory; /** @@ -26,18 +21,7 @@ public class BaseSeleniumTestCase extends BaseFunctionalTestCase { //出错时截屏的规则 @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void failed(Throwable e, Description description) { - File scrFile = s.snapshot(); - String scrFilename = description.getClassName() + "_" + description.getMethodName() + ".png"; - File outputFile = new File("target/screenshot/", scrFilename); - try { - FileUtils.copyFile(scrFile, outputFile); - } catch (IOException ioe) { - } - } - }; + public TestRule snapshotrule = new SeleniumSnapshotRule(s); @BeforeClass public static void createSeleniumOnce() throws Exception { diff --git a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/ShowcaseServer.java b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/ShowcaseServer.java index fd05a65f..875cceef 100644 --- a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/ShowcaseServer.java +++ b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/ShowcaseServer.java @@ -12,7 +12,6 @@ public class ShowcaseServer { public static final int PORT = 8080; public static final String CONTEXT = "/showcase"; - public static final String BASE_URL = "http://localhost:8080/showcase"; public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web" }; public static void main(String[] args) throws Exception { @@ -26,8 +25,8 @@ public class ShowcaseServer { try { server.start(); - System.out.println("Server running at " + BASE_URL); - System.out.println("Hit Enter to reload the application"); + System.out.println("Server running at http://localhost:" + PORT + CONTEXT); + System.out.println("Hit Enter to reload the application quickly"); //等待用户输入回车重载应用. while (true) { diff --git a/examples/showcase/src/test/resources/application.functional.properties b/examples/showcase/src/test/resources/application.functional.properties index 596f9c72..c81675ff 100644 --- a/examples/showcase/src/test/resources/application.functional.properties +++ b/examples/showcase/src/test/resources/application.functional.properties @@ -1,4 +1,5 @@ baseUrl=http://localhost:8083/showcase +embeddedForLocal=true #selenium settings, options include firefox,ie,chrome,htmlunit,remote:localhost:4444:firefox selenium.driver=firefox diff --git a/modules/test/src/main/java/org/springside/modules/test/selenium/Selenium2.java b/modules/test/src/main/java/org/springside/modules/test/selenium/Selenium2.java index 1d70e15c..36fe5f50 100644 --- a/modules/test/src/main/java/org/springside/modules/test/selenium/Selenium2.java +++ b/modules/test/src/main/java/org/springside/modules/test/selenium/Selenium2.java @@ -6,9 +6,11 @@ package org.springside.modules.test.selenium; import java.io.File; +import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; +import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.OutputType; @@ -248,10 +250,15 @@ public class Selenium2 { } /** - * 截屏成png文件,存放于临时目录,在JVM退出时自动删除. + * 截屏成png文件,复制到目标文件。源文件存放于临时目录,在JVM退出时自动删除. */ - public File snapshot() { - return ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); + public void snapshot(String basePath, String outputFileName) { + File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); + File targetFile = new File(basePath, outputFileName); + try { + FileUtils.copyFile(srcFile, targetFile); + } catch (IOException ioe) { + } } // WaitFor 函數 // diff --git a/modules/test/src/main/java/org/springside/modules/test/selenium/SeleniumSnapshotRule.java b/modules/test/src/main/java/org/springside/modules/test/selenium/SeleniumSnapshotRule.java new file mode 100644 index 00000000..51b80ccb --- /dev/null +++ b/modules/test/src/main/java/org/springside/modules/test/selenium/SeleniumSnapshotRule.java @@ -0,0 +1,27 @@ +package org.springside.modules.test.selenium; + +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; + +/** + * 在出错时截屏的规则. + * + * 在出错时将屏幕保存为png格式的文件,默认路径为项目的target/screensnapshot/测试类名_测试方法名.png + * + * @author calvin + */ +public class SeleniumSnapshotRule extends TestWatcher { + + private final Selenium2 s; + + public SeleniumSnapshotRule(Selenium2 s) { + this.s = s; + } + + @Override + protected void failed(Throwable e, Description description) { + String basePath = "target/screenshot/"; + String outputFileName = description.getClassName() + "_" + description.getMethodName() + ".png"; + s.snapshot(basePath, outputFileName); + } +} diff --git a/modules/test/src/main/java/org/springside/modules/test/selenium/WebDriverFactory.java b/modules/test/src/main/java/org/springside/modules/test/selenium/WebDriverFactory.java index 6a32e5df..b87195d2 100644 --- a/modules/test/src/main/java/org/springside/modules/test/selenium/WebDriverFactory.java +++ b/modules/test/src/main/java/org/springside/modules/test/selenium/WebDriverFactory.java @@ -18,18 +18,20 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.springframework.util.Assert; /** - * 创建WebDriver的工厂类, 支持主要的firefox,ie和remote三种driver. + * 创建WebDriver的工厂类. * * @author calvin */ public class WebDriverFactory { - /** * 根据driverName创建各种WebDriver的简便方法. * - * 当持续集成服务器安装在非Windows机器上, 没有IE浏览器与XWindows时, 需要使用remote driver调用远程的Windows机器. + * 支持主要的firefox,ie,chrome三种常用浏览器. + * + * 支持不需要浏览器,可快速运行的HtmlUnit. + * + * 当持续集成服务器安装在非Windows机器上, 没有IE浏览器与XWindows时, 又希望使用真正的浏览器时,需要使用remote driver调用远程的Windows机器. * drivername如remote:192.168.0.2:4444:firefox, 此时要求远程服务器在http://192.168.0.2:4444/wd/hub上启动selenium remote服务. - * @throws MalformedURLException */ public static WebDriver createDriver(String driverName) { WebDriver driver = null; @@ -51,8 +53,10 @@ public class WebDriverFactory { String remoteHost = params[1]; String remotePort = params[2]; String driverType = params[3]; - DesiredCapabilities cap = null; + String remoteUrl = "http://" + remoteHost + ":" + remotePort + "/wd/hub"; + + DesiredCapabilities cap = null; if (BrowserType.firefox.name().equals(driverType)) { cap = DesiredCapabilities.firefox(); } else if (BrowserType.ie.name().equals(driverType)) { @@ -62,7 +66,7 @@ public class WebDriverFactory { } try { - driver = new RemoteWebDriver(new URL("http://" + remoteHost + ":" + remotePort + "/wd/hub"), cap); + driver = new RemoteWebDriver(new URL(remoteUrl), cap); } catch (MalformedURLException e) { throw new RuntimeException(e); }