#146 Selenium2以JUnit @Rule的形式,支持在出错时,截屏到target/scennshot目录。改进写法。及其他Functional Test小细节.
This commit is contained in:
+4
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-18
@@ -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 {
|
||||
|
||||
+2
-3
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
+4
-4
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-18
@@ -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 {
|
||||
|
||||
+2
-3
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 函數 //
|
||||
|
||||
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
+10
-6
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user