Write Selenium tests with Junit In Eclipse

来源:互联网 发布:苹果手机怎么设置数据下载 编辑:程序博客网 时间:2024/06/05 02:41

Write Selenium tests with Junit In Eclipse

分类: Selenium 903人阅读 评论(0)收藏 举报
seleniumjunitexceptionserverbuildnetbeans

在Eclipse IDE中编写Junit代码去驱动Selenium,对基于HTTP协议的Web Application进行测试。


一、洗个手准备工具:

0.Java环境,这个就不说了。

1.Eclipse IDE,下载地址 www.Eclipse.org,当然其他的IDE(例如 Netbeans)也可以。

2. Selenium Server和Selenium-java-client,下载地址 http://seleniumhq.org/download/ 

3.Firefox,采用FF浏览器进行测试,其他浏览器是一样的测试方法。

4.Firebug | xpather | Selenium IDE,测试辅助工具

5.CmdHere,在cmd中启动Selenium Server时可能会切换到很深的目录,比较繁琐,使用这个小工具右键SeleniumServer所在的目录选择"Open Command window

 Here",很快捷。下载地址 http://download.microsoft.com/download/whistler/Install/2/WXP/EN-US/CmdHerePowertoySetup.exe

(第0和2项必要;第1和3项可被其他相应的软件代替;第4和5项可选)


二、埋头开始动手

1.创建Java Project (File->New->Java Project->工程名“SeleniumTest”)

2.创建包名(选中"工程名SeleniumTest"->New->包名“com.selenium.test.init”)

3.创建类(选中"包名"->New->“Class”->类名MyFirstSeleniumTestCase)

4.添加Selenium Server和Selenium-Java-Client到“Build Path”

  1)右键工程名“SeleniumTest”

  2)点击"Build Path"->"Configure Build Path"->"Libraries"-> "Add External JARs"

  3)浏览选择Selenium Server和Selenium-Java-Client对于的jar文件

5.添加Junit

  1)右键工程名“SeleniumTest”

  2)点击"Build Path"->"Add Library"->"JUnit"->"JUnit4"

6.编写如下代码

[html] view plaincopy
  1. package com.selenium.test.init;  
  2.   
  3. import org.junit.After;  
  4. import org.junit.Before;  
  5. import org.junit.Test;  
  6.   
  7. import com.thoughtworks.selenium.DefaultSelenium;  
  8. import com.thoughtworks.selenium.SeleneseTestBase;  
  9.   
  10. public class MyFirstSeleniumTestCase extends SeleneseTestBase {  
  11.   
  12.     DefaultSelenium selenium = null;  
  13.     @Before  
  14.     public void setUp() throws Exception {  
  15.         selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.google.com.hk");  
  16.         selenium.start();  
  17.     }  
  18.       
  19.     @Test  
  20.     public void testGoogleSearch() throws Exception {  
  21.         selenium.open("http://www.google.com.hk");  
  22.         selenium.type("//input[@id='lst-ib']","Selenium-RC");  
  23.         selenium.click("//input[@name='btnK']");  
  24. //              selenium.waitForPageToLoad("60000");  
  25.     }  
  26.       
  27.       
  28.     @After  
  29.     public void tearDown()throws Exception {  
  30.         selenium.stop();  
  31.     }  
  32.       
  33.       
  34. }  

7.启动Selenium Server

  方法1)在cmd中切换到Selenium Server的jar文件所在的目录下,执行命令 java -jar selenium-server-standalone-2.21.0.jar

 方法2)右键Selenium Server的jar文件所在的目录,选择"Open Command window Here",执行命令 java -jar selenium-server-standalone-2.21.0.jar

 方法3)创建bat文件来启动

启动信息如下:

[plain] view plaincopy
  1. 2012-6-14 22:01:32 org.openqa.grid.selenium.GridLauncher main  
  2. 信息: Launching a standalone server  
  3. 22:01:32.968 INFO - Java: Sun Microsystems Inc. 17.1-b03  
  4. 22:01:32.968 INFO - OS: Windows XP 5.1 x86  
  5. 22:01:33.000 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552  
  6. 22:01:33.875 INFO - RemoteWebDriver instances should connect to: http://127.0.0.  
  7. 1:4444/wd/hub  
  8. 22:01:33.875 INFO - Version Jetty/5.1.x  
  9. 22:01:33.890 INFO - Started HttpContext[/selenium-server/driver,/selenium-server  
  10. /driver]  
  11. 22:01:33.890 INFO - Started HttpContext[/selenium-server,/selenium-server]  
  12. 22:01:33.890 INFO - Started HttpContext[/,/]  
  13. 22:01:34.046 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@5d173  
  14. 22:01:34.046 INFO - Started HttpContext[/wd,/wd]  
  15. 22:01:34.062 INFO - Started SocketListener on 0.0.0.0:4444  
  16. 22:01:34.062 INFO - Started org.openqa.jetty.jetty.Server@32fb4f  

8.运行

  右键选择类文件“MyFirstSeleniumTestCase.java”->"Run As"->"JUnit Test"

9.预期结果

 1)Firefox启动

 2)打开google主页

 3)在搜索框中输入"Selenium-RC"

 4)点击"google搜索"按钮

 5)呈现搜索结果

 6)FF浏览关闭

 7)Junit Test状态条呈"绿色"


三、其他说明

1.代码"selenium.waitForPageToLoad("60000");"之所以被注释掉是因为打开会抛以下异常:

[plain] view plaincopy
  1. com.thoughtworks.selenium.SeleniumException: Timed out after 30000ms  
  2.     at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)  
  3.     at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)  
  4.     at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:369)  
  5.     at com.selenium.test.init.MyFirstSeleniumTestCase.testGoogleSearch(MyFirstSeleniumTestCase.java:21)  
  6.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  7.     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
  8.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
  9.     at java.lang.reflect.Method.invoke(Unknown Source)  
  10.     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)  
  11.     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)  
  12.     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)  
  13.     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)  
  14.     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)  
  15.     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)  
  16.     at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)  
  17.     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)  
  18.     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)  
  19.     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)  
  20.     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)  
  21.     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)  
  22.     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)  
  23.     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)  
  24.     at org.junit.runners.ParentRunner.run(ParentRunner.java:236)  
  25.     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  
  26.     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  
  27.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)  
  28.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  
  29.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  
  30.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)  

我在网上查了,很多人都遇到了,也试了查到的所有办法,都没有解决。有些说这个方法存在bug,所有想实现其他功能只能另辟蹊径了。2.使用Selenium IDE录制然后导出来的代码中可能继承(extends)的是类"SeleneseTestCase",类名中间有条横线,意思是该类被弃用,虽然不影响功能实现,但是看着不舒服,手动把"SeleneseTestCase"换成"SeleneseTestBase"警告就没有了
0 0