selenium运行自动化脚本(调试验证通过)

项目顾问建议用Selenium做web应用的自动化测试,决定将业务逻辑简单、功能稳定的功能模块用自动化去测试,比如简单的登录模块。下面就将过程简单总结一下。

我是在Ubuntu系统上跑的Selenium

1. 环境准备

1.1 Java环境

安装jdk,我的JDK在/usr/local/jdk1.6.0_24

1.2 Java IDE

因为Selenium支持Java/C#/Ruby/Python等,这里我选择使用Java。到Eclipse官方网站下载最新的Eclipse,免安装,解压即可

1.3 Selenium

到Selenium官方网站http://seleniumhq.org/download/下载Selenium IDE,Selenium Server, Selenium Client Driver(这里只选了Java版)

其中,

Selenium IDE需要安装,目前只有Firefox浏览器支持,安装完成后,即作为Firefox的插件,点击Tools-Selenium IDE就可以开始录制脚本了

Selenium Server下载下来是一个jar包,我放在了/usr/local/下,在Eclipse中写test时,需要添加这个包

Selenium Client Driver下载下来是一个zip包,用unzip selenium-java-2.8.0.zip 将其解压到/usr/local/下,在解压的路径里有/usr/local/selenium-2.8.0/selenium-java- 2.8.0.jar包,在Eclipse中写test时,需要添加这个包

以上三个下载的东东之间的关系是:

Selenium IDE可以录制脚本,执行脚本,修改脚本,将录制的脚本导出成其他语言(比如Java);

Selenium Server是和Selenium Client结合使用的,Server用来启动浏览器,接受Client端的请求,执行test;

Client端则是向Server端发送请求。

2. 录制脚本

2.1 打开Firefox,点击Tools-Selenium IDE(默认是启动录制状态)

2.2 在Firefox中的地址栏输入www.google.com,回车,打开google首页面

2.3 在搜索框输入Selenium, 点击Google搜索,进入结果页面

2.4 在结果页面选择Selenium,右击,在快捷菜单中出现 verifyTextPresent Selenium并点击该条目

2.5 点击结果页面中第一个超链接

2.6 停止录制

3. 导出成Java Junit4 (remote control)

在Selenium IDE中点击File-Export Test Cases As...,选择Junit4(Remote Control),并保存

此外,我们可以使用Options-Format功能,直接显示成Java脚本。但是对于新版Selenium,这个是默认禁用的,只需要启动Format即可:

To turn it on, go to options->options and select 'enable experimental features'

参考http://seleniumcn.cn/read.php?tid=1849&page=e , it says "Format option has been turned OFF in the latest release. As per the release notes, Format changing is now marked as experimental due to possible issues, you can turn it on from the options dialog."

4. 在Eclipse中编辑并运行

4.1 新建一个Java project: File-New-Java Project,输入Project Name(JunitTest),其他的可以默认,当然也可以修改使用哪个JRE

4.2 在这个Project中新建一个Junit文件,自动会导入Junit包

4.3 添加Selenium包

4.3.1

右击左侧Package的空白区域,点击Build Path-Add External Archives...,将下载/解压到usr/local下的selenium-java-2.8.0.jar(client)和selenium- server-standalone-2.8.0.jar(server)加进来,注意,这个server的jar是必须的,虽然可能在代码里并不 import这个包(使用server的Api时才import),如果不加这个包,会在执行是出现错误信息: java.lang.NoClassDefFoundError: com/google/common/base/Charsets,因为这个class是server包里的。

4.3.2 在新建的Junit文件里引入这两个包

import org.openqa.selenium.server.SeleniumServer;      //server
import com.thoughtworks.selenium.*;                              //client

4.4 将在3中导出的代码选择性的拷贝到新建的Junit文件里,并进行适当的修改

4.5 运行case

4.5.1 启动Selenium server

(1) 启动方式可以用命令行形式 java -jar selenium-server-standalone-2.8.0.jar (注意java是否配置环境变量以及jar包路径)

在我的系统里可以这样运行/usr/local/jdk1.6.0_24/bin/java -jar /usr/local/selenium-server-standalone-2.8.0.jar

(2) 可以在testcase里调用

static SeleniumServer server ;
   
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.print("setupBeforeClass\n");   
        server = new SeleniumServer();
        server.start();
    }

@AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.print("tearDownAfterClass\n");
        server.stop();
    }

4.5.2 可能问题

1. 如果不加入server jar,会出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets错误

2. 如果case中需要打开新窗口,由于firefox禁止弹出,我们可以对Firefox的设置做下修改: Edit-Preferences-Content 将Block pop-up windows 前面的勾去掉


5 参考

http://seleniumhq.org/docs/02_selenium_ide.html

http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html

http://www.51testing.com/?uid-146979-action-viewspace-itemid-242645

http://www.cnblogs.com/wuchaodong/archive/2009/02/16/1391306.html

http://zhidao.baidu.com/question/269941148.html

http://seleniumcn.cn/read.php?tid=1849&page=e

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值