public class HelloSelenium extends TestCase {
private Selenium selenium;
public void setUp() throws Exception {
String url = "http://www.iteye.com";
selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);//4444是SeleniumServer默认的端口
selenium.start();
}
protected void tearDown() throws Exception {
selenium.stop();
}
public void testGoogleTestSearch() throws Throwable {
selenium.open("/search/");
selenium.type("query", "selenium");
selenium.click("link=论坛");
selenium.waitForPageToLoad("5000");
assertEquals("JavaEye论坛频道首页",selenium.getTitle() );
}
}
跑了一下....
引用
java.lang.RuntimeException: Could not contact Selenium Server; have you started it?
Catch body broken: IOException from cmd=getNewBrowserSession&1=*firefox&2=http%3A%2F%2Fwww.iteye.com -> java.net.ConnectException: Connection refused: connect
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:70)
at com.maodajun.webjetty.HelloSelenium.setUp(HelloSelenium.java:14)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
看来还是有问题
再看文档原来还要启动server端....
引用
D:\server所在的目录>java -jar selenium-server.jar
10:21:50.625 INFO - Java: Sun Microsystems Inc. 1.6.0_03-b05
10:21:50.625 INFO - OS: Windows XP 5.1 x86
10:21:50.625 INFO - v0.9.2 [2006], with Core v0.8.3 [1879]
10:21:50.718 INFO - Version Jetty/5.1.x
10:21:50.718 INFO - Started HttpContext[/selenium-server/driver,/selenium-server
/driver]
10:21:50.718 INFO - Started HttpContext[/selenium-server,/selenium-server]
10:21:50.718 INFO - Started HttpContext[/,/]
10:21:50.734 INFO - Started SocketListener on 0.0.0.0:4444
10:21:50.734 INFO - Started org.mortbay.jetty.Server@901887
再运行
弹出一个页面....
Skype也来捣乱
改成*iexplore
或把skype插件拆了
再运行.....
页面卡住了....
刷新页面再运行...
引用
com.thoughtworks.selenium.SeleniumException: Timed out after 5000ms
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:73)
at com.thoughtworks.selenium.DefaultSelenium.waitForPageToLoad(DefaultSelenium.java:510)
at com.maodajun.webjetty.HelloSelenium.testGoogleTestSearch(HelloSelenium.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
鸟的....5秒都没打开.....太慢了点...再加大,9000秒
selenium.waitForPageToLoad("9000");
发现是网断了....
上了网之后改回5秒...
这个测试终于绿了....
--------------------------------------
我又想能不能不用dos启动selenium来呢?
看了一 下API
public class HelloSelenium extends TestCase {
private Selenium selenium;
private SeleniumServer ss;
public void setUp() throws Exception {
ss = new SeleniumServer(SeleniumServer.DEFAULT_PORT,true,false);
ss.start();
String url = "http://www.iteye.com";
//大家不要太过分啊...robbin会杀人的.
selenium = new DefaultSelenium("localhost",
SeleniumServer.DEFAULT_PORT, "*firefox", url);
selenium.start();
}
protected void tearDown() throws Exception {
selenium.stop();
ss.stop();
}
public void testGoogleTestSearch() throws Throwable {
selenium.open("/search/");
selenium.type("query", "selenium");
selenium.click("link=论坛");
selenium.waitForPageToLoad("5000");
assertEquals("JavaEye论坛频道首页",selenium.getTitle() );
}
}
引用
java.lang.VerifyError: (class: org/openqa/selenium/server/SeleniumServer, method: assembleHandlers signature: (Z)V) Incompatible argument to function
at com.maodajun.webjetty.HelloSelenium.setUp(HelloSelenium.java:15)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
板本错误????
把所有的包拆开一看......
....竟然selenium固化了一套jetty在里面.....
我倒....5.1.x
把我导入的包删去....
引用
org.mortbay.util.MultiException[java.net.BindException: Address already in use: JVM_Bind]
at org.mortbay.http.HttpServer.doStart(HttpServer.java:686)
at org.mortbay.util.Container.start(Container.java:72)
at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:839)
at com.maodajun.webjetty.HelloSelenium.setUp(HelloSelenium.java:16)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at org.mortbay.util.ThreadedServer.newServerSocket(ThreadedServer.java:391)
at org.mortbay.util.ThreadedServer.open(ThreadedServer.java:477)
at org.mortbay.util.ThreadedServer.start(ThreadedServer.java:503)
at org.mortbay.http.SocketListener.start(SocketListener.java:203)
at org.mortbay.http.HttpServer.doStart(HttpServer.java:716)
at org.mortbay.util.Container.start(Container.java:72)
at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:839)
at com.maodajun.webjetty.HelloSelenium.setUp(HelloSelenium.java:16)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Server server = new Server(); // 创建一个新的HttpServer
SocketListener listener = new SocketListener();// 创建一个新监听器
listener.setPort(3003); // 设置监听端口为8080
server.addListener(listener ); // 将监听类注册到server中
server.addWebApplication("/webapp","webapp"); // 将这个web应用注册到这个Server中
server.start(); // 最后启动这个server
我本以为没什么问题但:
引用
2008-1-10 11:09:27 org.mortbay.http.HttpServer doStart
信息: Version Jetty/5.1.x
2008-1-10 11:09:27 org.mortbay.util.FileResource <clinit>
信息: Checking Resource aliases
2008-1-10 11:09:28 org.mortbay.util.Container start
信息: Started org.mortbay.jetty.servlet.WebApplicationHandler@d9660d
Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/log/Log
at com.sun.org.apache.commons.logging.JettyLog.<init>(JettyLog.java:36)
at com.sun.org.apache.commons.logging.LogFactory.getLog(LogFactory.java:35)
at org.apache.jasper.servlet.JspServlet.<clinit>(JspServlet.java:100)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at java.lang.Class.newInstance0(Class.java:350)
at java.lang.Class.newInstance(Class.java:303)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:199)
at org.mortbay.jetty.servlet.ServletHolder.start(ServletHolder.java:240)
at org.mortbay.jetty.servlet.ServletHandler.initializeServlets(ServletHandler.java:446)
at org.mortbay.jetty.servlet.WebApplicationHandler.initializeServlets(WebApplicationHandler.java:321)
at org.mortbay.jetty.servlet.WebApplicationContext.doStart(WebApplicationContext.java:509)
at org.mortbay.util.Container.start(Container.java:72)
at org.mortbay.http.HttpServer.doStart(HttpServer.java:708)
at org.mortbay.util.Container.start(Container.java:72)
at com.maodajun.webjetty.WebStart.main(WebStart.java:41)
在test.jsp里写:
<a href="test.html" value = "test">test</a>
在test.html里写:
<html>
abc
</html>
运行一下....OK了....
不过有候会被卡住....
出
引用
7125 [SocketListener0-2] WARN org.mortbay.http.HttpConnection - POST http://localhost:3003/selenium-server/driver/?&localFrameAddress=top&seleniumWindowName=&uniqueId=sel_17535&sessionId=767859&counterToMakeURsUniqueAndSoStopPageCachingInTheBrowser=1199938773453&sequenceNumber=2 HTTP/1.1
java.net.SocketException: No buffer space available (maximum connections reached?): recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.mortbay.util.LineInput.fill(LineInput.java:477)
at org.mortbay.util.LineInput.read(LineInput.java:352)
at java.io.FilterInputStream.read(FilterInputStream.java:111)
at org.mortbay.http.HttpInputStream.read(HttpInputStream.java:120)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:411)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:453)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:183)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:132)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:118)
at java.io.InputStreamReader.read(InputStreamReader.java:151)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.readPostedData(SeleniumDriverResourceHandler.java:251)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleBrowserResponse(SeleniumDriverResourceHandler.java:169)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(SeleniumDriverResourceHandler.java:125)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:816)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:982)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:833)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:244)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)