SeleniumIDE与eclipse如何连接使用 【秦菲】SeleniumIDE与eclipse如何连接使用

1. 打开Firefox,利用IDE录制脚本(依次点击浏览器界面:工具->Selenium IDE)
2. 把录制好的脚本转换成其他语言(非HTML)
备注1:可以点击Selenium IDE界面:Option->Format
或是导出为其他语言,点击Selenium IDE界面:文件->Export Test Case As..
备注2:这里以Java/JUnit4/Remote Control为例

第二步:

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

第三步:录制脚本

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

第四步:转换语言

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

第五步:复制转换的脚本

第六步:新建一个class

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

第七步:把复制的脚本粘贴到eclipse中

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

第八步:更改错误
错误1:

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

错误2:

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

更改的效果:

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

错误3:

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

这里报错的原因是:test2()这个方法默认抛出异常
解决的方案1:把test2()方法

CODE:
1
public void test2() throws Exception

更改为

CODE:
1
public void test2()

方案2:把test2放在try..catch..中去调用

CODE:
1
2
3
4
5
6
try {
    ts.test2();
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    

第九步:打开selenium服务器
第十步:运行脚本

SeleniumIDE与eclipse如何连接使用
【秦菲】SeleniumIDE与eclipse如何连接使用

这样就能把你从IDE上录制的脚本拿到eclipse中运用了

下面是具体的脚本

CODE:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre>
package selenium_2;
 
import com.thoughtworks.selenium.*;
 
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import static org.junit.Assert.*;
 
import java.util.regex.Pattern;
 
public class test_selenium{
 private Selenium selenium;
 
@Before
 public void setUp(){
 //localhost:利用本机打开浏览器
 //4444:打开的端口
 //*chrome:用Firefox浏览器
 //"http://www.baidu.com/":在IDE界面Base URL里面的网站
 selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.baidu.com/");
 //启动selenium,前提是selenium服务器已经启动
 selenium.start();
 }
 
@Test
 public void test2(){
 //这里open的地址,是上面地址补充,比如录制的网址为http://www.baidu.com/XXX.abc,那么这里open("/XXX.abc")
 selenium.open("/");
 //把窗口最大化
 selenium.windowMaximize();
 selenium.type("id=kw", "cydtest");
 selenium.click("id=su");
 selenium.waitForPageToLoad("30000");
 selenium.click("link=陈永达的软件测试");
 
 }
 
@After
 public void tearDown() throws Exception {
 //关闭selenium,及为关闭运行的浏览器
 selenium.stop();
 }
 //更改完成后,就自己新建一个主函数
 public static void main(String[] agrs){
 //把class转换成一个实体
 test_selenium ts=new test_selenium();
 //用创建的实体调用你建立的方法,用于运行方法里面的具体操作
 ts.setUp();
 ts.test2();
 
}
}
<pre>

这里需要提醒童鞋们,可不要忘记导入RC必要的包哦,如何导入,我在《介绍WebDriver(Selenium 2.0)》有做介绍哦