`

JavaWeb function test / autotest / Testlink / TestNG / Selenium WebDriver

阅读更多

 

Selenium Automation Tools v1.7

Exception:

the eclipse executable launcher was unable to locate its companion share libary

解决:

 

 

 

 

http://www.iteye.com/news/search?query=Selenium

 

开源测试工具/社区(Selenium /OpenQA.org)——今非昔比

http://blog.csdn.net/kerryzhu/article/details/2901517

和我一起学 Selenium WebDriver(1)——入门篇

http://my.oschina.net/dyhunter/blog/94090

 

 

开源测试工具/社区(Selenium /OpenQA.org)——今非昔比

http://blog.csdn.net/kerryzhu/article/details/2901517

开源测试工具Selenium或者说开源测试社区OpenQA.org已是今非昔比,两年多前,我曾经介绍过Selenium IDE、Selenium Core和Selenium RC(强大的Web开源测试工具—Selenium)。如今,这个家族又增添了不少新成员,例如

 

  • 分布式测试环境——Selenium Grid
  • 强大的功能测试框架和平台-CubicTest
  • 单元测试Java库-Floyd
  • 测试管理工具——Bromine
  • 纯Java、轻量级应用的测试工具——Frankenstein
  • Rails应用测试方法——Selenium on Rails
  • C#/ Ruby脚本录制工具——Watir Maker
  • 基于Watir 的Web测试工具——WET
  • 构建Java单元测试的一套工具——JTC(Java Testing Commons)

 

 

 

1.分布式测试环境——Selenium Grid(网格)

 

Selenium Grid能充分地利用测试环境中的机器,允许同时并行地、在不同的环境上运行多个测试任务,极大地加快Web应用的功能测试.

 

基于优秀的Web测试工具Selenium,Selenium Grid可以并行地运行多个Selenium Remote Control(RC,远程控制)的实例,而且可以在一台机器上就能控制它们,而不要在乎实际的物理环境。这样,Selenium Grid可以大大地节省测试执行的时间。

 

对于Selenium RC,又提供了很好的灵活性,测试脚本可以由Ruby, Java, Python, C#, PHP, ...等不同的语言来开发。

 

最后,告诉大家,Selenium Grid容易安装,也容易使用.

 


 

 

 

 

 

 

 

 

2. 强大的功能测试框架和平台-CubicTest

 

CubicTest成为领先的Web功能测试的综合开发环境,虽然现在有很多不同的测试框架,但是CubicTest 具有协调各种相关的Web元素之间的相互作用而独树一帜。不必作为一个程序员去理解CubicTest,唯一要关注的就是要测试Web页面上所能看到的界面元素,而不是HTML源代码。

 

CubicTest 强力支持 “不重复自己(Don't Repeat Yourself,DRY)”和“测试优先” 两大原则。DRY原则通过子测试(subtests,被其它测试调用的测试)实现通用化(Commons)、参数化(parameterization)和抽象等特性,从而使自动化功能测试变得容易。通过图形化建模和HTML输出等特性,CubicTest实现了测试优先的策略。

 

 CubicTest借助像elenium 和Watir的优秀测试框架来驱动测试,而且是图形化方法实现,从而使人们非常容易理解和使用框架。直到最近发布的1.8.6版本,CubicTest才充分发挥了Selenium的所有潜力,并在不久的将来,也能很好支持Watir。

 

CubicTest 1.8.6的新特性:

 

  • 支持多帧(multi-frames)结构
  • 国际化(I18N)支持,多国语言的站点
  • 自定义步骤,具有调试的能力
  • 捕获各种HTML和图像的问题

 

 

 

 

 

3. 单元测试JavaFloyd

 

Floyd是为了更容易实施Web应用的自动化测试而建立的Java库.它提供了对像Mozilla和IE这样标准的浏览器的完全控制能力,以实现Java单元测试。通过调用Floyd's Java API,可以实现浏览器的交互和页面装载,这样可以模拟用户对真实的web浏览器的交互作用。

 

对于Java web应用,可以采用内嵌式Web服务器来提高测试能力,如访问服务器的session,获取向服务器发出的请求和服务器的响应。Floyd不是对JUnit类的扩张,所以它可以应用于各种单元测试之中,不局限于JUnitTestNG

 

 

 

4. 测试管理工具——Bromine

 

Bromine是集成了Selenium Core 和 Selenium Remote Control、完全成熟的QA工具,从而使从事Web应用测试的测试经理和测试工程师的工作变得轻松和有效。借助Bromine,我们可以非常容易(点点鼠标)跟踪和管理测试项目、需求、测试计划、测试用例和缺陷,也可以管理Selenium RC的节点以及像手工测试那样一步一步地执行测试脚本。例如测试人员可以执行所录制的、绑定Selenium IDE格式的脚本,并监控缺陷的提交和将缺陷分派给相应的开发人员。

 

 

 

 

 

 

  • 完全成熟的QA工具
  • 创建项目
  • 将需求和项目绑定起来
  • 将测试和需求绑定起来
  • 容易组织和执行测试
  • 浏览和分析测试结果
  • 提交缺陷报告
 

 

 

 

 

 

5.最后以一个不是笑话的笑话结束

 

你们知道上面几个测试工具(Selenium、Bromine等)的名字为什么取得那么难认?

 

Selenium是硒的英文名称,硒是天然的解毒剂,对有害重金属离子的天然解毒作用,可以解除汞(Mercury)毒,实验表明汞暴露水平越高,硒对汞毒性的拮抗作用越明显,所以说硒是汞的克星, Selenium是Mercury的克星,你知道Mercury的测试工具吗?做过自动化测试的人都知道:)

 

溴(Bromine)是非金属单质里面唯一的液体,也是一种很好的微量元素,矿泉水中就有一项质量指标,即溴化物≥1.0 ,溴能调节中枢神经系统活动,具有安定情绪和镇静作用。在医院里普遍使用的镇静剂,有一类就是用溴的化合物制成。

 

有了Selenium工具,就不需要花费几十万甚至几百万钞票去买Mercury的产品;有了Bromine 调节,我们就不会着急,可以轻松自如地管理软件测试,享受工作和生活。

 

 

 

参考:

 

 

和我一起学 Selenium WebDriver(1)——入门篇

http://my.oschina.net/dyhunter/blog/94090

zTree 东西不多,我也一直使用着原始的人工测试手段,随着内容的不断增多,测试起来就越发的繁杂,而且经常犯懒,这样就会忽略很多本该发现的问题,而且也容易出 现旧的bug 反复出现的情况,这都是测试不规范造成的。要做好东西就要更加规范和严格,于是乎决定要学习一下 Selenium WebDriver,也就是原先的  Selenium v2 了,这方面整体的文章并不多,所以一边学着,自己一边整理吧。

    对于这个可以自动化测试的工具( Selenium WebDriver)我就不做过多描述了,去 google、baidu 搜索一下即可。 我这里只记录学习  Selenium WebDriver 的过程,尤其是运行时可能出现的问题,当然了,我是做java的,我只学习 java 与  Selenium WebDriver 配合的方法。

一、下载文件

先要去官网(http://seleniumhq.org/download/)下载必需的文件:

  • Selenium IDE (专门用于 FireFox 测试的独立界面,可以录制测试步骤,但我更倾向于写代码做标准的功能测试)
  • Selenium Server (可以输入指令控制、可以解决跨域的 js 问题,等到后面学到了再讲吧)
  • The Internet Explorer Driver Server (专门用于IE测试的)
  • Selenium Client Drivers (可以找到你熟悉的语言,例如我选择的 Java)
  • Third Party Browser Drivers NOT SUPPORTED/DEVELOPED by seleniumhq(第三方开发的 Selenium 插件,第一个就是 Chrome 的,否则你就没办法测试 Chrome 了)
  • 其他的,就根据你自己的需要寻找吧,目前这些足够我用了。


二、安装 & 运行

貌似摆弄新东西时,只有 “Hello World” 蹦出来以后,我们这些初学者才会感到情绪稳定,那就赶紧开始吧。

对于初期打算直接用编程方式制作测试用例的情况来说,Selenium IDE、Selenium Server 都可以不用安装执行。
英语好的朋友可以直接看官网的文档(http://seleniumhq.org/documentation/)就能够开始使用了。
看中文的,就继续听我唠叨:

【1. 建立 Maven 工程】
Selenium 支持 maven 工程,这会让你的工作更加简便。

用 Eclipse 建个 Maven 的工程,建成后,直接修改 pom.xml,(参考:http://seleniumhq.org/docs/03_webdriver.html#setting-up-a-selenium-webdriver-project

 

 

 

01 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
02     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
03     <modelVersion>4.0.0</modelVersion>
04     <groupId>Selenium2Test</groupId>
05     <artifactId>Selenium2Test</artifactId>
06     <version>1.0</version>
07     <dependencies>
08         <dependency>
09             <groupId>org.seleniumhq.selenium</groupId>
10             <artifactId>selenium-java</artifactId>
11             <version>2.25.0</version>
12         </dependency>
13         <dependency>
14             <groupId>com.opera</groupId>
15             <artifactId>operadriver</artifactId>
16         </dependency>
17     </dependencies>
18     <dependencyManagement>
19         <dependencies>
20             <dependency>
21                 <groupId>com.opera</groupId>
22                 <artifactId>operadriver</artifactId>
23                 <version>0.16</version>
24                 <exclusions>
25                     <exclusion>
26                         <groupId>org.seleniumhq.selenium</groupId>
27                         <artifactId>selenium-remote-driver</artifactId>
28                     </exclusion>
29                 </exclusions>
30             </dependency>
31         </dependencies>
32     </dependencyManagement>
33 </project>

 

 

 

pom.xml 修改保存后,eclipse 会自动把需要的 jar 包下载完成。


【2. 测试 FireFox】
Selenium 最初就是在 FireFox 上做起来的插件,所以我们先来搭建 FireFox 的环境。
确保你正确安装了 FireFox 后,就可以直接编写 java 代码测试喽。

在 lesson1 目录下建立 ExampleForFireFox.java
(因为国内不少朋友访问 google 的时候会出问题,所以我就把代码中的 google 变成 baidu 了)

01 package lesson1;
02  
03 import org.openqa.selenium.By;
04 import org.openqa.selenium.WebDriver;
05 import org.openqa.selenium.WebElement;
06 import org.openqa.selenium.firefox.FirefoxDriver;
07 import org.openqa.selenium.support.ui.ExpectedCondition;
08 import org.openqa.selenium.support.ui.WebDriverWait;
09  
10 public class ExampleForFireFox  {
11     public static void main(String[] args) {
12         // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
13 //      System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
14         // 创建一个 FireFox 的浏览器实例
15         WebDriver driver = new FirefoxDriver();
16  
17         // 让浏览器访问 Baidu
18         driver.get("http://www.baidu.com");
19         // 用下面代码也可以实现
20         // driver.navigate().to("http://www.baidu.com");
21  
22         // 获取 网页的 title
23         System.out.println("1 Page title is: " + driver.getTitle());
24  
25         // 通过 id 找到 input 的 DOM
26         WebElement element = driver.findElement(By.id("kw"));
27  
28         // 输入关键字
29         element.sendKeys("zTree");
30  
31         // 提交 input 所在的  form
32         element.submit();
33          
34         // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
35         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
36             public Boolean apply(WebDriver d) {
37                 return d.getTitle().toLowerCase().endsWith("ztree");
38             }
39         });
40  
41         // 显示搜索结果页面的 title
42         System.out.println("2 Page title is: " + driver.getTitle());
43          
44         //关闭浏览器
45         driver.quit();
46     }
47 }


普通情况下,直接运行代码就可以看到会自动弹出 FireFox 窗口,访问 baidu.com,然后输入关键字并查询,一切都是自动完成的。

错误提醒:
1)Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量  "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。

2)Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request

出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1  localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢? 

 

经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)

【3. 测试 Chrome】
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java ,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。

错误如下:
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin

设置路径后还是会报错:
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment.
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome 的第三方插件 ChromeDriver,去看第一个错误中提示给你的 网址:http://code.google.com/p/selenium/wiki/ChromeDriver
按照人家给的例子来修改我们的测试代码吧:

01 package lesson1;
02  
03 import java.io.File;
04 import java.io.IOException;
05  
06 import org.openqa.selenium.By;
07 import org.openqa.selenium.WebDriver;
08 import org.openqa.selenium.WebElement;
09 import org.openqa.selenium.chrome.ChromeDriverService;
10 import org.openqa.selenium.remote.DesiredCapabilities;
11 import org.openqa.selenium.remote.RemoteWebDriver;
12 import org.openqa.selenium.support.ui.ExpectedCondition;
13 import org.openqa.selenium.support.ui.WebDriverWait;
14  
15 public class ExampleForChrome {
16     public static void main(String[] args) throws IOException {
17         // 设置 chrome 的路径
18         System.setProperty(
19                 "webdriver.chrome.driver",
20                 "C:\\Documents and Settings\\sq\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe");
21         // 创建一个 ChromeDriver 的接口,用于连接 Chrome
22         @SuppressWarnings("deprecation")
23         ChromeDriverService service = new ChromeDriverService.Builder()
24                 .usingChromeDriverExecutable(
25                         new File(
26                                 "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe"))
27                 .usingAnyFreePort().build();
28         service.start();
29         // 创建一个 Chrome 的浏览器实例
30         WebDriver driver = new RemoteWebDriver(service.getUrl(),
31                 DesiredCapabilities.chrome());
32  
33         // 让浏览器访问 Baidu
34         driver.get("http://www.baidu.com");
35         // 用下面代码也可以实现
36         // driver.navigate().to("http://www.baidu.com");
37  
38         // 获取 网页的 title
39         System.out.println("1 Page title is: " + driver.getTitle());
40  
41         // 通过 id 找到 input 的 DOM
42         WebElement element = driver.findElement(By.id("kw"));
43  
44         // 输入关键字
45         element.sendKeys("zTree");
46  
47         // 提交 input 所在的 form
48         element.submit();
49  
50         // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
51         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
52             public Boolean apply(WebDriver d) {
53                 return d.getTitle().toLowerCase().endsWith("ztree");
54             }
55         });
56  
57         // 显示搜索结果页面的 title
58         System.out.println("2 Page title is: " + driver.getTitle());
59  
60         // 关闭浏览器
61         driver.quit();
62         // 关闭 ChromeDriver 接口
63         service.stop();
64  
65     }
66 }


运行一下看看,是不是一切OK了?

【2012.12.06补充】

 

上一个 Demo 中无法正常使用 new ChromeDriver(),今天在做进一步学习时看到一篇文章(http://qa.blog.163.com/blog/static/19014700220122231779/),恍然大悟,原来只需要把 ‘webdriver.chrome.driver’ 的值设置为 chromedriver 的路径就可以了。

 

 

 

01 package lesson1;
02  
03 import java.io.IOException;
04  
05 import org.openqa.selenium.By;
06 import org.openqa.selenium.WebDriver;
07 import org.openqa.selenium.WebElement;
08 import org.openqa.selenium.chrome.ChromeDriver;
09 import org.openqa.selenium.support.ui.ExpectedCondition;
10 import org.openqa.selenium.support.ui.WebDriverWait;
11  
12 public class ExampleForChrome2 {
13     public static void main(String[] args) throws IOException {
14         // 设置 chrome 的路径
15         System.setProperty(
16                 "webdriver.chrome.driver",
17                 "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe");
18         // 创建一个 ChromeDriver 的接口,用于连接 Chrome
19         // 创建一个 Chrome 的浏览器实例
20         WebDriver driver = new ChromeDriver();
21  
22         // 让浏览器访问 Baidu
23         driver.get("http://www.baidu.com");
24         // 用下面代码也可以实现
25         // driver.navigate().to("http://www.baidu.com");
26  
27         // 获取 网页的 title
28         System.out.println("1 Page title is: " + driver.getTitle());
29  
30         // 通过 id 找到 input 的 DOM
31         WebElement element = driver.findElement(By.id("kw"));
32  
33         // 输入关键字
34         element.sendKeys("zTree");
35  
36         // 提交 input 所在的 form
37         element.submit();
38  
39         // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
40         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
41             public Boolean apply(WebDriver d) {
42                 return d.getTitle().toLowerCase().endsWith("ztree");
43             }
44         });
45  
46         // 显示搜索结果页面的 title
47         System.out.println("2 Page title is: " + driver.getTitle());
48  
49         // 关闭浏览器
50         driver.quit();
51  
52         // element = driver.findElement(By.id("kw"));
53         // // element.clear();
54         // element.click();
55         // element.clear();
56         // element.sendKeys("zTree");
57         // element.submit();
58     }
59 }


【4. 测试 IE】
想逃避 IE 吗?? 作为前端开发,IE 你是必须要面对的,冲吧!
其实你会发现, Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox 的代码修改为 IE 的,那是相当的容易,只需要简单地两步:
1)把 ExampleForFireFox.java 另存为 ExampleForIE.java 
2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver driver = new InternetExplorerDriver();
3)一般大家的 IE都是默认路径吧,所以也就不用设置 property 了

01 package lesson1;
02  
03 import org.openqa.selenium.By;
04 import org.openqa.selenium.WebDriver;
05 import org.openqa.selenium.WebElement;
06 import org.openqa.selenium.ie.InternetExplorerDriver;
07 import org.openqa.selenium.support.ui.ExpectedCondition;
08 import org.openqa.selenium.support.ui.WebDriverWait;
09  
10 public class ExampleForIE {
11     public static void main(String[] args) {
12         // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
13         // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
14         // 创建一个 FireFox 的浏览器实例
15         WebDriver driver = new InternetExplorerDriver();
16  
17         // 让浏览器访问 Baidu
18         driver.get("http://www.baidu.com");
19         // 用下面代码也可以实现
20         // driver.navigate().to("http://www.baidu.com");
21  
22         // 获取 网页的 title
23         System.out.println("1 Page title is: " + driver.getTitle());
24  
25         // 通过 id 找到 input 的 DOM
26         WebElement element = driver.findElement(By.id("kw"));
27  
28         // 输入关键字
29         element.sendKeys("zTree");
30  
31         // 提交 input 所在的 form
32         element.submit();
33  
34         // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
35         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
36             public Boolean apply(WebDriver d) {
37                 return d.getTitle().toLowerCase().endsWith("ztree");
38             }
39         });
40  
41         // 显示搜索结果页面的 title
42         System.out.println("2 Page title is: " + driver.getTitle());
43  
44         // 关闭浏览器
45         driver.quit();
46     }
47 }


运行一下,是不是 so easy?

入门工作完成,现在完全可以利用 java 代码,让 Selenium 自动执行我们设置好的测试用例了,不过.....这仅仅是个开始。

 

利用Selenium集成TestLink做自动化测试

http://senvon.iteye.com/blog/1484051

 

 

 

end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics