设为首页 收藏本站
查看: 807|回复: 0

[经验分享] 研究一下jetty & selenium

[复制链接]

尚未签到

发表于 2017-2-26 11:57:03 | 显示全部楼层 |阅读模式
趁还没有忘干净写下blog
jetty的入门文章:
灰狐工作室的:http://docs.huihoo.com/jetty/1.html
selenium
yananay:      http://www.iteye.com/topic/107276
一开始下载了:jetty
这里要说明
http://docs.codehaus.org/display/JETTY/Downloading+and+Installing#download
这页的下载地址可用.....
sourceforge.net上的地址全部作废
我下载6.1之后:
写了HelloJetty

public class HelloJetty {
public static void main(String[] args) throws Exception {  
Server server = new Server();
BoundedThreadPool threadPool = new BoundedThreadPool();
threadPool.setMaxThreads(100);
server.setThreadPool(threadPool);
Connector connector = new SelectChannelConnector();
connector.setPort(8080);
server.setConnectors(new Connector[] { connector });
WebAppContext context = new WebAppContext("webapp", "/webapp");
server.addHandler(context);
server.setStopAtShutdown(true);
server.setSendServerVersion(true);
server.start();
server.join();
}  
}

项目下 建一个webapp目录
webapp
|
-testhtml.html
|
-testjsp.jsp

//testhtml.html
<html>
abc
</html>
//-----------------------
//testjsp.jsp
<%String a="jsp";
//System.out.println(a);
%><%=a %>

发现jsp不能运行
引用

HTTP ERROR: 404
NOT_FOUND
RequestURI=/webapp/testjsp.jsp
Powered by jetty://


应该是少包的原因:
找到jsp2.1的安装包
jsp2.1.jar
jsp-api-2.1.jar
core-3.1.1.jar
加上之后
------OK----------
再下载个selenium RC
下来一看傻了眼....
NN多的包
找到selenium-java-client-driver.jar
导入项目中去.
写了一个
HellSelenium

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)



dos下还开着一个server占着4444端口呢.
关了dos下的seleniumserver再跑
绿了....
不过问题又出了
.....
刚刚一开始写jettyserver全红了....
看来6.1与5.1有本质上的不同啊....
不得以...去找5.1的写法...

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)


这个问题上卡了半天...
左找右找....
直到我看到了这个
DSC0000.jpg
把jsp2.1的包换成2.0的包....lib文件数好多啊
第一阶段的改造基本完成
--------------
我把一个webserver一个seleniumserver同时启动
之后再写一个测试

public class HelloWebSelenium extends TestCase {
Server server ;
SeleniumServer ss ;
Selenium selenium;
public static void main(String[] args) {
junit.textui.TestRunner.run(HelloWebSelenium.class);
}
protected void setUp() throws Exception {
super.setUp();
ss = new SeleniumServer();
server  = new Server();
SocketListener listener = new SocketListener();
listener.setPort(3003);
server.addListener(listener );
server.addWebApplication("/webapp","webapp");
server.start();
ss.start();
selenium = new DefaultSelenium("localhost",
SeleniumServer.DEFAULT_PORT, "*firefox",
"http://localhost:3003/webapp/");   
selenium.start();   
}
protected void tearDown() throws Exception {
server.stop();
ss.stop();
super.tearDown();
}
public void testJettyServer() throws IOException, URISyntaxException{
URL url = new URI("http://localhost:3003/webapp/test.jsp").toURL();
InputStream in = url.openStream();
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String str = br.readLine();
assertEquals("jsp",str);

}
public void testJettyServerbyWebClient() throws MalformedURLException {
selenium.open("/webapp/test.jsp");
selenium.click("link=test");
selenium.waitForPageToLoad("3000");//自己本地的还是很快的...
assertEquals("abc",selenium.getBodyText() );
}
}


在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)


或者是没有响应测试也不结束.....
------------------------------------
这些可能是由于多次启动停止seleniumserver的原因...所以可以把
selenium放到testsuit中去....
引用

连接被重置
   载入页面时到服务器的连接被重置。
    *   此站点暂时不可用或者太忙。请稍后重试。
    *   如果您无法载入任何页面,请检查您计算机的网络连接。
    *   如果您的计算机受到防火墙或代理服务器的保护,请确认 Firefox  被授权访问网页。


这个可能是由于没有设定连接数....线程被卡住了.
---------------

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347428-1-1.html 上篇帖子: eclipse+maven+jetty+struts2 HelloWorld 下篇帖子: 准备好好学习一下Jetty
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表