Write Selenium tests with Junit In Eclipse
2012-06-14 22:18
691 查看
在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.编写如下代码
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文件来启动
启动信息如下:
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");"之所以被注释掉是因为打开会抛以下异常:
我在网上查了,很多人都遇到了,也试了查到的所有办法,都没有解决。有些说这个方法存在bug,所有想实现其他功能只能另辟蹊径了。
2.使用Selenium IDE录制然后导出来的代码中可能继承(extends)的是类"SeleneseTestCase",类名中间有条横线,意思是该类被弃用,虽然不影响功能实现,但是看着不舒服,手动把"SeleneseTestCase"换成"SeleneseTestBase"警告就没有了
一、洗个手准备工具:
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.编写如下代码
package com.selenium.test.init; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.SeleneseTestBase; public class MyFirstSeleniumTestCase extends SeleneseTestBase { DefaultSelenium selenium = null; @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.google.com.hk"); selenium.start(); } @Test public void testGoogleSearch() throws Exception { selenium.open("http://www.google.com.hk"); selenium.type("//input[@id='lst-ib']","Selenium-RC"); selenium.click("//input[@name='btnK']"); // selenium.waitForPageToLoad("60000"); } @After public void tearDown()throws Exception { selenium.stop(); } }
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文件来启动
启动信息如下:
2012-6-14 22:01:32 org.openqa.grid.selenium.GridLauncher main 信息: Launching a standalone server 22:01:32.968 INFO - Java: Sun Microsystems Inc. 17.1-b03 22:01:32.968 INFO - OS: Windows XP 5.1 x86 22:01:33.000 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552 22:01:33.875 INFO - RemoteWebDriver instances should connect to: http://127.0.0. 1:4444/wd/hub 22:01:33.875 INFO - Version Jetty/5.1.x 22:01:33.890 INFO - Started HttpContext[/selenium-server/driver,/selenium-server /driver] 22:01:33.890 INFO - Started HttpContext[/selenium-server,/selenium-server] 22:01:33.890 INFO - Started HttpContext[/,/] 22:01:34.046 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@5d173 22:01:34.046 INFO - Started HttpContext[/wd,/wd] 22:01:34.062 INFO - Started SocketListener on 0.0.0.0:4444 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");"之所以被注释掉是因为打开会抛以下异常:
com.thoughtworks.selenium.SeleniumException: Timed out after 30000ms at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112) at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106) at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:369) at com.selenium.test.init.MyFirstSeleniumTestCase.testGoogleSearch(MyFirstSeleniumTestCase.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我在网上查了,很多人都遇到了,也试了查到的所有办法,都没有解决。有些说这个方法存在bug,所有想实现其他功能只能另辟蹊径了。
2.使用Selenium IDE录制然后导出来的代码中可能继承(extends)的是类"SeleneseTestCase",类名中间有条横线,意思是该类被弃用,虽然不影响功能实现,但是看着不舒服,手动把"SeleneseTestCase"换成"SeleneseTestBase"警告就没有了
相关文章推荐
- Write Selenium tests with Junit In Eclipse
- [Selenium]Eclipse hangs at 57% in debug mode with TestNG tests
- junit.framework.AssertionFailedError: No tests found in错误解决办法
- Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(一)
- Integrating with Eclipse: The Subclipse Plug-in
- “A configuration with this name already exists” error in eclipse run configurations
- 单元测试-Junit 报错:No tests Found in **
- Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(一)
- Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(三)
- junit.framework.AssertionFailedError: No tests found in错误解决办法
- Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(二)
- Running Selenium 'headless' with Chrome Driver in Python - YouTube
- Problems with Eclipse buttons in Ubuntu 9.10(Cannot create/import project)
- android学习笔记转--------------junit.framework.AssertionFailedError: No tests found in错误解决办法
- Introduction to functional testing with Selenium in Django
- junit.framework.AssertionFailedError: No tests found in错误解决办法
- Failed to Copy Aritfact in Eclipse Ganymede with Workspace Resolution Enabled
- Debugging with the Maven Jetty Plugin (Jetty Launcher插件)in Eclipse
- Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(三)
- The import org.junit.Test conflicts with a type defined in the same file报错