Selenium webdriver 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文。为了用IDE还下载了Firefox浏览器。后来接触过两个项目都需要selenium,一个采用selenium webdirver+junit4 +java,另外一个是采用 robot+selenium2library 。总体感觉就是开源、简单、使用范围广、是网页测试必备单品。
关于selenium的好的学习资料:
- 官方User Guide: http://seleniumhq.org/docs/
- 官方API: http://selenium.googlecode.com/git/docs/api/java/index.html
- 中文API: http://download.csdn.net/detail/testingba/3811354
- 虫师博客: http://www.cnblogs.com/fnng/category/349036.html
- github: https://github.com/SeleniumHQ/selenium/
- selenium github docs: http://seleniumhq.github.io/selenium/docs/api/py/
- 乙醇的各种语言的selenium https://github.com/easonhan007/webdriver_guide/blob/master/README.md
- 易百的webdriver: http://www.yiibai.com/selenium/selenium_webdriver.html
一个简单的小例子
首先,让我们还是从一个小例子开始,这样可以有一个总体的了解。废话一句,一般下载东西我都喜欢从官网下载,虽然速度慢了点,但是好处多啊,第一放心,第二没啥乱七八糟的捆绑东西,第三顺便还可以瞅瞅官方文档,多好。
下载并安装(我的是Linux环境):
1. JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html
我的环境是JDK 1.7。在终端输入命令"java -version",如果现实类似如下信息,说明JDK已经安装成功。
$ java -version
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.3.el6-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
2. Eclipse:http://www.eclipse.org/downloads/
JAVA编辑器,简单,好用,强大。下载解压即可。
3. Selenium: http://code.google.com/p/selenium/downloads/list
有两个东西需要下载:selenium-server-standalone-2.44.0.jar 和selenium-java-2.44.0.zip。将selenium-java-2.44.0.zip解压之后获得:selenium-java- 2.44.0.jar selenium-java-2.44.0-srcs.jar。将这三个jar文件放到一个文件夹里。
4.可选 Firebug。
一款很经典的定位网页元素以及查看网页源码的插件。比如说下面的例子里我怎么知道”百度一下“的id是”su“呢?当然就是通过Firebug定位咯。安装方法是在火狐的附加组件里搜索”Firebug",然后我这边选择的是“Firebug 2.0.8",安装即可。
正式开始
1. New-> Java Project , 输入工程名“selenium_demo”。
2. 添加selenium jar包。有两种方式,人选其一即可:
1)直接添加External JARs。在工程名上右键,选择 “Properties” -> “Java Build Path” -> “Libraries” -> “Add External JARs”,添加进去上面的3个 selenium jar包。
2)自己新建一个库,然后将我们下载好的jar文件放进去。个人觉得这一种比较好,因为导入进去之后selenium库是单独的,而且其他项目也可 以利用。在eclipse新建用户库,方法是打开“Windows"->"Preferences"-> "User Libraries" ->"New",输入库名(”Selenium_Library"),点击”OK",再次选中“Selenium_Library",点击”Add External JARs...",选择上述的3个selenium jar包路径,点击“打开”用户库新建完成。最后是在本项目中导入用户库,方法是在项目名上右键,选择"Properties"->"Java Build Path"->"Libraries"->"Add Library..."->"User Library",选中刚刚新建的用户库(Selenium_Library),点击”Finish"即可。
3. 新建一个包 “com.selenium.demo” , 新建一个类 “TestSelenium.java” ,在类中输入如下内容:
package com.selenium.demo; import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class TestSelenium {
WebDriver driver=null; @Before
public void setUp() throws Exception {
driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
} @After
public void tearDown() throws Exception {
driver.quit();
} @Test
public void test() throws InterruptedException {
driver.findElement(By.id("kw")).sendKeys("Selenium");
driver.findElement(By.id("su")).click();
Thread.sleep(1000);
assertTrue(driver.getTitle().contains("Selenium"));
}
}
4. 在类名上右键-->Run As-->JUnit Test 就可以看到效果了。selenium会打开火狐浏览器,打开百度,搜索“Selenium",然后关掉。
5.查看结果。在下方的JUnit可以看到是否pass。如果找不到JUnit窗口,可以通过 "Windows"->"Show Views"->"Other"->"JUnit" 打开。
Selenium webdriver 开始的更多相关文章
- Selenium WebDriver Code
Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...
- 使用httpclient 调用selenium webdriver
结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- Selenium Webdriver java 积累一
Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...
- Selenium Webdriver下click失效问题解决
最近在使用Selenium Webdriver(Selenium2.0)进行界面自动化测试的时候发现单击事件无效,通过driver.findElement的方式是可以找到click元素的,但是就是cl ...
- 如何用selenium webdriver 捕获js error
### 问题 捕捉页面上js error ### 解决办法 从Selenium webdriver log 中解析 # -*- coding:utf8 -*- import unittest from ...
- Selenium WebDriver 之 PageObjects 模式 by Example
目录 1. 项目配置 2. 一个WebDriver简单例子 3. 使用Page Objects模式 4. 总结 5. Troubleshooting 6. 参考文档 本篇文章通过例子来阐述一下Sele ...
- Selenium webdriver 操作日历控件
一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如: 但是,有的日期控件是readonly的 比如1 ...
- Selenium WebDriver 处理cookie
在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆. 首先了解一下Java Cookie类的一些方法. 在jsp中处理cookie数据的常用方法: ...
随机推荐
- EasyUI-datagrid获取编辑行的数据
可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法 最后这样写才搞定 var arr=$(‘#d ...
- (转)ThinkPHP Where 条件中使用表达式
转之--http://www.cnblogs.com/martin1009/archive/2012/08/24/2653718.html Where 条件表达式格式为: $map['字段名'] = ...
- DataTable去重复方法
//去掉重复行 DataTable table=new DataTable(); DataView dv = table.DefaultView; table = dv.ToTable(true, n ...
- MVC3 展示数据含有html代码处理,配合上篇发布的StringHelper
@Html.Raw(@StringHelper.SubstringToHTML(Content,30)) StringHelper 地址:http://www.cnblogs.com/Jiawt/p/ ...
- Hadoop配置项整理(mapred-site.xml)【转】
本文转自:http://slaytanic.blog.51cto.com/2057708/1101360 name value Description hadoop.job.history.locat ...
- Protocol Buffer Xcode 正确使用思路 成功安装 Xcode7.1
1. 下载protobuf编译工具 序列化是将数据转换为一个特定的类 http://pan.baidu.com/s/1qWrxHxU 下载解压,它不是用来放在你的项目里 2.打开终端 依次输入并等待指 ...
- `~!$^*()[]{}\|;:'",<>/?在英文怎么读?
`~!$^*()[]{}\|;:'",<>/?在英文怎么读? 'exclam'='!' 'at'='@' 'numbersign'='#' 'dollar'='$' 'perce ...
- UICollectionView设置item(cell)之间间距为0(紧挨在一起的效果)
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; self.layout = layout ...
- 12 Integer to Roman(int转罗马数字Medium)
题目意思:1-3999转罗马数字 思路:从大往小减 ps:这题有点蛋疼 class Solution { public: string intToRoman(int num) { string a[] ...
- javascript特殊运算符(in,instanceof,typeof,delete,void,逗号)
in运算符 in运算符要求其左边的运算数是一个字符串,或可以被转换为字符串,右边的运算数十一个对象或数组.如果该 运算符左边的值是右边对象的一个属性名,则返回true, ...