一、下载必备的一些工具和jar包
1.下载并安装firefox
(1)下载selenium IDE
http://release.seleniumhq.org/selenium-ide/1.9.0/selenium-ide-1.9.0.xpi
(2)下载Test Suite Batch Converter
https://addons.mozilla.org/en-US/firefox/addon/test-suite-batch-converter-sel/
(3)firefox插件的安装
将要安装的附件拖拽到firefox中,就可以进行安装了。
2.在下面的地址内下载selenium-server-standalone-2.28.0.jar,这个jar包是用来向firefox发布命令用的。
http://seleniumhq.org/download/
二、录制脚本,并将脚本转化成java脚本
1.录制脚本。
打开firefox浏览器,开启selenium IDE如下图
默认的状态下是录制开启的情况,这时候,对firefox所作的任何操作,selenium都会记录下来的。如我们在firefox中输入“www.baidu.com”然后搜索
“selenium”,如下图
Selenium IDE的界面
点击上图中被红色框选起来的按钮,停止脚本的录制。
点击selenium IDE中的文件,选择Export Test Case As---java Junit 4/Web driver这个可以自己选择,我这里选择的是java的单元测试脚本。
取个名字后保存。代码如下
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Test {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("selenium");
driver.findElement(By.id("su")).click();
driver.findElement(By.linkText("selenium_百度百科")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
新建立一个java工程将该段代码放到一个新建的类里面,然后将下载好的selenium-server-standalone-2.28.0.ja导入到工程里面。最后工程的结构如下
在Testa.java里面,右键---Run As选择Junit运行。
测试的结果如下
脚本运行后,会自动的开启一个firefox窗口,然后按照之前操作的顺序执行。
这里面有两个地方需要注意。
(1)当把录制好的脚本导入到新建立的工程的时候,需要导入java的Junit包。
(2)Unable to connect to host 127.0.0.1 on port7055 after 45000 ms,具体错误代码如下
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
*** LOG addons.manager: Application has been upgraded
*** LOG addons.xpi: startup
*** LOG addons.xpi: Skipping unavailable install location app-system-local
*** LOG addons.xpi: Skipping unavailable install location app-system-share
*** LOG addons.xpi: Ignoring file entry whose name is not a valid add-on ID: C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\webdriver-staging
*** LOG addons.xpi: checkForChanges
*** LOG addons.xpi: Installed distribution add-on cehomepage@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on cemigration@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on coba@mozilla.com.cn
*** LOG addons.xpi: Installed distribution add-on cpmanager@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on livemargins@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on quicklaunch@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on semodifier@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on share_all_cn@mozillaonline.com
*** LOG addons.xpi: Installed distribution add-on tabimprovelite@mozillaonline.com
*** LOG addons.xpi-utils: Opening database
*** LOG addons.xpi-utils: Creating database schema
*** LOG addons.xpi: New add-on fxdriver@googlecode.com installed in app-profile
*** LOG addons.xpi: New add-on cehomepage@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on cemigration@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on coba@mozilla.com.cn installed in app-profile
*** LOG addons.xpi: New add-on cpmanager@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on livemargins@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on quicklaunch@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on semodifier@mozillaonline.com installed in app-profile
*** LOG addons.xpi: Loading bootstrap scope from C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\semodifier@mozillaonline.com.xpi
*** LOG addons.xpi: Calling bootstrap method install on semodifier@mozillaonline.com version 0.2
*** LOG addons.xpi: New add-on share_all_cn@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on tabimprovelite@mozillaonline.com installed in app-profile
*** LOG addons.xpi: New add-on {972ce4c6-7e08-4474-a285-3208198ce6fd} installed in app-global
*** LOG addons.xpi: Updating database with changes to installed add-ons
*** LOG addons.xpi-utils: Updating add-on states
*** LOG addons.xpi-utils: Writing add-ons list
*** LOG addons.xpi: Calling bootstrap method startup on semodifier@mozillaonline.com version 0.2
*** LOG addons.manager: shutdown
*** LOG addons.xpi: shutdown
*** LOG addons.xpi-utils: shutdown
*** LOG addons.xpi-utils: Database closed
*** LOG addons.xpi: startup
*** LOG addons.xpi: Skipping unavailable install location app-system-local
*** LOG addons.xpi: Skipping unavailable install location app-system-share
*** LOG addons.xpi: Ignoring file entry whose name is not a valid add-on ID: C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\webdriver-staging
*** LOG addons.xpi: checkForChanges
*** LOG addons.xpi-utils: Opening database
*** LOG addons.xpi: No changes found
*** LOG addons.xpi: Loading bootstrap scope from C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\semodifier@mozillaonline.com.xpi
*** LOG addons.xpi: Calling bootstrap method startup on semodifier@mozillaonline.com version 0.2
*** LOG addons.repository: Recreating database schema
*** LOG addons.repository: Creating database schema
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:109)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:245)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:109)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:185)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:178)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:174)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
at com.org.selenium.Testa.setUp(Testa.java:18)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)
只要更新导入,到java工程中的selenium包到最新就可以了。我之前使用的是2.25.0就出现了这个错误,后来到官网下载了最新的2.28就OK了。
怎么样,简单吧。赶快来试试吧。
参考文章: