﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-大话人生-随笔分类-Selenium Test</title><link>http://www.cnitblog.com/stomic/category/8928.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 05 Mar 2014 02:52:12 GMT</lastBuildDate><pubDate>Wed, 05 Mar 2014 02:52:12 GMT</pubDate><ttl>60</ttl><item><title>Selenium介绍及原理解析</title><link>http://www.cnitblog.com/stomic/archive/2014/03/04/89291.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Tue, 04 Mar 2014 05:32:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2014/03/04/89291.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/89291.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2014/03/04/89291.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/89291.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/89291.html</trackback:ping><description><![CDATA[<strong style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">一、什么是Selenium？</strong><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium是ThoughtWorks公司的一个强大的开源Web功能测试工具系列，采用Javascript来管理整个测试过程，包括读入测试套件、执行测试和记录测试结果。它采用Javascript单元测试工具JSUnit为核心，模拟真实用户操作，包括浏览页面、点击链接、输入文字、提交表单、触发鼠标事件等等，并且能够对页面结果进行种种验证。也就是说，只要在测试用例中把预期的用户行为与结果都描述出来，我们就得到了一个可以自动化运行的功能测试套件。（Selenium的核心是Javascript写的，他和浏览器进行通信，把测试用例的信息发送给浏览器执行，从而达到自动化测试的目的。）</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium系列主要包括4中产品，如下：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1. Selenium Core</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium Core是Selenium的核心，是有Javascript和Html文件组成的，它是Selenium IDE和Selenium RC的核心引擎。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. Selenium IDE</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium IDE提供了一个浏览器的插件模式，在浏览器的该插件的模式下，可以录制和运行脚本。目前只支持Firefox一种浏览器插件。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">3. Selenium Remote Control</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium RC支持用程序预研编写测试用例，比如Ruby、Java、C#等，这样做的好处是，可以Selenium和其他测试框架集成，比如.NET环境下，可以把Selenium和NUnit集成，用Selenium来编写测试用例，用NUnit来实现测试用例的自动化运行。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium RC比其他包要大，主要包括以下几个部分：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">A、Selenium Core</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">B、Selenium Server运行时；</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">C、Selenium提供的针对各种开发语言的类库。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium Core和Selenium Server 运行时都是放在Selenium-server.jar中，Selenium RC提供的应用程序类库重要包括下面几种语言：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Java、Ruby、Php、Phthon、Perl、.NET。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">4. Selenium Grid</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium Grid是Selenium的一个扩展，也是开源的，他把管理一小撮Selenium-RC的事情和为了运行一个测试而连接一个SeleniumServer的事情分开了，实际上，Selenium Grid提供了一个hub，像个用于控制测试的远程控制器，但是是显示的将请求发送到一个或多个机器上的某个有效的Selenium-RC实例上。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">注意：Grid只是提供了一个脚本运行的环境，至于脚本以什么样的方式去跑（并行的方式或者是多环境的方式），要看脚本本身和和脚本的Runner的配置决定的。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><strong style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">二、选择合适的Selenium 工具</strong><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">既然Selenium工具有这么多，那么到底如何选择呢？？</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/0fe9341ba8e7806b42a9ad74.jpg" width="758" height="195" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1. 需要远程安装</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">即是否需要在被测网站的服务器端安装，这里只有Selenium Core需要，因为出于浏览器的同源策略的原因（同源策略后面会讲到），这个也是Selenium Core的一个限制，如果你要测试www.google.com网站，那你还需要在google网站的服务器上安装一个Selenium Core；</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">而Selenium IDE和Selenium Core HTA不会同源策略所限制，因为他们对浏览器扩展了。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium RC提供了一个代理服务器，保证了Selenium JS文件时来自同一个远程服务器的，从而符合同源策略。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. 将测试结果写到磁盘</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">只有Selenium Core不能将任何测试结果写到磁盘上（因为它是用javascript写的，它不允许向磁盘写数据），其解决方案是当然你可以将测试结果发送到另外一台服务器保存。这也是Selenium Core的一大限制。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">OK，现在我们来研究下到底该使用哪款工具开展测试！</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1.Selenium IDE支持并且只支持Firefox浏览器，支持的浏览器太少，而依附于Firefox也不便于日后开展自动化测试，但是，它的录制快捷好用！并且有代码转换功能，可以把Selenium语言测试案例转为C#,Java等语言的测试案例，我建议使用Selenium IDE + FireBug进行测试案例的编写，然后转为其他语言的测试案例后，再调用Selenium RC运行测试案例。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. Selenium Core，它的优点是编写测试案例简单，并且支持绝大多数的浏览器，但缺点也同样明显，Selenium Core需要远程安装，Selenese语言也限制了复杂案例的可能性，并且没有良好的外部扩展，这是些都会是致命的问题。因为一个款测试工具不可能 100%满足你测试需求的，当它不能满足你测试需求时候，它必须有一个扩展机制可以让你可以使用其他的方式满足你需求，否则这款测试软件即使功能强大，也请三思慎用，否则当投入大量资源后才发现某些问题不能解决，那时候已经晚了，这是我的切身体会。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">3.Selenium RC是我推荐使用的工具，它支持很多浏览器，可以使用C#，Java等语言编写测试案例，易于维护，同时提供了很好的扩展性，所以后续的文档我会以Selenium RC作为默认的测试工具。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><strong style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">三、Selenium RC组成</strong><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">下表一个简单的架构图：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/9b93800a49da291ee82488ab.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium RC由两部分组成：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">（1）Selenium Server</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium Server主要控制浏览器行为，总的来说，Selenium Server由三部分组成：Launcher、Http Proxy、Selenium Core，其中Selenium Core是在Selenium Server启动浏览器时，嵌入到浏览器中的。Selenium Core是一堆Javascript函数集合，当Selenium RC通过网络与 Selenium Server通讯时（通过简单的Http Get或Post请求），Selenium Server解析客户端请求为Selenese Command，然后就会调用相应的JS函数来控制浏览器操作。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">（2）Selenium Libraries</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">写测试案例时主要控制Selenium Server库，客户端提供了编程接口，允许你可以通过编程运行Selenium命令，Client Libraries为每一种编程语言提供了与Selenium RC 服务器通讯的编程接口。一个Selenium客户端类库实际上时提供一套函数集合，你可以通过运行你的测试程序来运行Selenium Command，类库中的每一个接口都是和Selenium Command一一对应的。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">客户端类库通过发送一个Selenium Command与Selenium Server通讯，从而使被测应用程序执行相应的操作。客户端类库接收到Selenium Command命令返回的结果，并把结果返回给应用程序并赋值给相应的变量。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">注：可以通过Selenium IDE生成Selenium-RC代码。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/0a98c5a5e8d7febbd04358b4.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><strong style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">四、Selenium-RC架构</strong><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1. 什么是同源策略？</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">同源策略，它是由Netscape提出的一个著名的安全策略，现在所有的可支持javascript的浏览器都会使用这个策略。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">为什么需要同源策略，这里举个例子：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">假设现在没有同源策略，会发生什么事情呢？大家知道，JavaScript可以做很多东西，比如：读取/修改网页中某个值。恩，你现在打开了浏览器，在一个tab窗口中打开了银行网站，在另外一个tab窗口中打开了一个恶意网站，而那个恶意网站挂了一个的专门修改银行信息的JavaScript，当你访问这个恶意网站并且执行它JavaScript时，你的银行页面就会被这个JavaScript修改，后果会非常严重！而同源策略就为了防止这种事情发生，看下图：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/82bd2689da3311519f2fb4b6.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">比如说，浏览器的两个tab页中分别打开了http://www.baidu.com/index.html和http://www.google.com/index.html，其中，JavaScript1和JavaScript3是属于百度的脚本，而JavaScript2是属于谷歌的脚本，当浏览器的tab1要运行一个脚本时，便会进行同源检查，只有和www.baidu.com同源的脚本才能被执行，所谓同源，就是指域名、协议、端口相同。所以，tab1只能执行JavaScript1和JavaScript3脚本，而JavaScript2不能执行，从而防止其他网页对本网页的非法篡改。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. Selenium RC运行模式：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">Selenium RC有两种运行模式，分别是Heightened Privileges Browsers和Proxy Injection。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">A、Heightened Privileges Browsers（高特权模式）</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/1bab0d80f2e67cbef703a6b0.jpg" width="758" height="728" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">这种工作模式与代理模式的工作流程是很相似的，唯一的不同时的是，在Heightened Privileges Browsers模式下，Browser对Web的请求没有经过Http Proxy，所以返回的Web页面就有可能和Selenium Core不同源了(一般的情况下，Open都应该只获取&#8220;源&#8221;下的某个子页面，用Open获取其他&#8220;源&#8221;的页面在测试中应该是极少数的，因为在这种情况 下，我们还应该再new一个新的ISelenium selenium进行处理，虽然用Open获取其他&#8220;源&#8221;的页面出现的机会极少，但如果真的需要这样的话，也只能启动 -proxyInjectionMode模式了，虽然这样的效率会低一些)。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">B、Proxy Injection（代理模式）</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/f9bf1547c3b2e31186947342.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(1).测试案例（Testcase）通过Client Lib的接口向Selenium Server发送Http请求，要求和Selenium Server建立连接。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢？从上文可以看出，Selenium Server是一个独立的中间服务器（确切地说是代理服务器），它可以架设在其他机器上！所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(2).Selenium Server的Launcher启动浏览器，把Selenium Core加载入浏览器页面当中，并把浏览器的代理设置为Selenium Server的Http Proxy。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(3).测试案例通过Client Lib的接口向Selenium Server发送Http请求，Selenium Server对请求进行解析，然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(4).Selenium Core接收到指令后，执行操作。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(5).浏览器收到新的页面请求信息（因为在(4)中，Selenium Core的操作可能引发新的页面请求），于是发送Http请求，请求新的Web页面。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">由于Selenium Server在启动浏览器时做了手脚，所以Selenium Server会接收到所有由它启动的浏览器发送的请求。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(6).Selenium Server接收到浏览器的发送的Http请求后，自己重组Http请求，获取对应的Web页面。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">(7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">因为浏览器存在同源策略，所以Selenium RC中的Selenium Server需要以这种代理模式运行。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><strong style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">五、Selenium Server解析</strong><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1. 首先启动Selenium服务：（以交互模式启动Selenium服务）</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/a8f756114500b42c5baf53b8.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">先说一下Firefox启动问题，在本地目录 C:\Documents and Settings\Administrator\Application Data\Mozilla\Firefox中包含了Firefox浏览器的一些配置信息，profiles.ini文件内容如下图所示：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/ca409915c06d8f2f4b90a7b8.jpg" width="758" height="297" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">大家可以看到，现在我机器上一共配置了两个Profile，关闭所有Firefox浏览器，在命令行中输入firefox.exe &#8211;profilemanager，可以选择浏览器所使用的Profile，之后选择&#8220;打开&#8221;，浏览器就会以指定额度配置文件打开，同时还可以新建和删除Profile文件。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">而Selenium Server服务启动时，如果不指定-firefoxProfile参数，那么Selenium Server就会打开一个全新的Firefox浏览器（不包含任何扩展与附件组件），而指定此参数后，表面上我们看到Selenium Server会使用指定的配置文件打开浏览器，而实际上Selenium Server仍然会在目录C:\Documents and Settings\Administrator\Local Settings\Temp中生成一个新的配置文件，profile所在文件夹的名字是自动生成的，命名规则是&#8221; customProfileDir&#8221;+sessionId。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. 我们在命令行中输入：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">cmd=getNewBrowserSession&amp;1=*firefox &amp;2=http://www.google.com</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/9c993b33bf8e02865fdf0ebf.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">1.Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&amp;1=*firefox&amp;2=http://www.google.com</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">我们所编写的测试案例，其实是通过发送Http请求实现对Selenium Server的控制，而测试案例所发送的请求就正是：---&gt; Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&amp;1=*iexplore&amp;2=http://www.google.com。我们可以再打开一个firefox浏览器，在地址栏输入：http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&amp;1=*iexplore&amp;2=http://www.google.com，回车！看，Selenium Server又为此产生了一个Session了！呵呵:&gt;</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">2. 这里，Selenium Server为上面的请求随机生成了一个Session ID：3ba90f6a4d1b4b8994b343cea0bac639，同时会在临时目录C:\Documents and Settings\Administrator\Local Settings\Temp 中产生新的配置文件夹customProfileDir3ba90f6a4d1b4b8994b343cea0bac639 。</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><span style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;">3. 如果一切正常，Selenium Server最后会出现Get Result Ok的字样，并出现如下两个框框：</span><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><br style="color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;" /><img small="0" src="http://hiphotos.baidu.com/liyunluck/pic/item/acbdcb0a09f22bbb0b7b8285.jpg" style="border: 0px; margin-bottom: 8px; clear: both; max-width: 758px; vertical-align: top; color: #454545; font-family: tahoma, helvetica, arial; font-size: 14px; line-height: 21px;"  alt="" /><img src ="http://www.cnitblog.com/stomic/aggbug/89291.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2014-03-04 13:32 <a href="http://www.cnitblog.com/stomic/archive/2014/03/04/89291.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)Unit Testing and Test Automation in VS2012 Part 2: Integrating Selenium with Visual Studio</title><link>http://www.cnitblog.com/stomic/archive/2013/07/30/88213.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Tue, 30 Jul 2013 04:04:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/30/88213.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/88213.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/30/88213.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/88213.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/88213.html</trackback:ping><description><![CDATA[<div><div> 						<p>In <a href="http://dotnet.kapenilattex.com/?p=1057">the first part of this series</a>  we discussed about integrating NUnit with Visual Studio 2012. One of  NUnit's strong points is its extensibility which has been used to expand  unit testing even further -- as far up as the user interface in fact.  This is where <a href="http://seleniumhq.org/">Selenium</a> comes in.</p> <p>Selenium runs on top of NUnit so that it can run tests against  instances of web browsers. With the new testing features of Visual  Studio 2012 we can now use it to test web applications.</p> <p>If you haven't read the first part of this series and haven't  followed the steps given there, I suggest reading it now and following  the steps to <a href="http://dotnet.kapenilattex.com/?p=1057">install NUnit on Visual Studio 2012 first</a>. They are a prerequisite to the next steps in this article.</p> <p><strong>Integating Selenium into Visual Studio 2012</strong></p> <p>Since we've already integrated NUnit to our Visual Studio solution,  why not go all the way and use Selenium as well? Selenium uses the NUnit  Framework for its tests anyway -- it's just a matter of adding several  more components to allow Selenium's Webdriver to fire up a web browser  and start executing tests.</p> <p>To do this we need to extend our project a bit:</p> <ol><li>Open up the Manage NuGet Packages window again (right-click on  References under the project -&gt; Manage NuGet Packages) and in the  search box type "Selenium"</li><li>Select "Selenium Webdriver" and click Install to add Selenium references to your project.</li><li>Select "Selenium Webdriver Support Classes" and click Install --  these are some additional references necessary to run Selenium tests in  your Visual Studio solution.<br /> <a href="http://dotnet.kapenilattex.com/wp-content/uploads/2012/09/Selenium-NuGet-Packages.png"><img src="http://dotnet.kapenilattex.com/wp-content/uploads/2012/09/Selenium-NuGet-Packages.png" alt="" title="Selenium NuGet Packages" size-full=""  wp-image-1106"="" height="564" width="843" /></a> </li><li>Download IE Webdriver from the <a href="http://seleniumhq.org/download/">Selenium download page</a> (choose the appropriate 32 or 64 bit version) and unzip.</li><li>Righ-click on the project name and click "Add existing item..."</li><li>Browse to the folder containing IEDriverServer.exe and choose that file to add to the project</li><li>Under the project tree right-click on the file and click Properties</li><li>Set the value of the field "Copy to Output Directory" to "Copy if newer"</li></ol> <p>The steps regarding the web driver ensure that the exe required to  open up a browser is always copied to the /bin/Debug folder, from where  it will in turn be used to call on the browser executables and open up  the browser.</p> <p>We can use the project we already set up above to test this. Add a  new class and set it to have the following code. Note the additional  OpenQA using declarations, aside from the NUnit.Framework that we've  used before:</p> <div><div id="highlighter_320607"  csharp"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div><div number3="" index2=""  alt2"="">3</div><div number4="" index3=""  alt1"="">4</div><div number5="" index4=""  alt2"="">5</div><div number6="" index5=""  alt1"="">6</div><div number7="" index6=""  alt2"="">7</div><div number8="" index7=""  alt1"="">8</div><div number9="" index8=""  alt2"="">9</div><div number10="" index9=""  alt1"="">10</div><div number11="" index10=""  alt2"="">11</div><div number12="" index11=""  alt1"="">12</div><div number13="" index12=""  alt2"="">13</div><div number14="" index13=""  alt1"="">14</div><div number15="" index14=""  alt2"="">15</div><div number16="" index15=""  alt1"="">16</div><div number17="" index16=""  alt2"="">17</div><div number18="" index17=""  alt1"="">18</div><div number19="" index18=""  alt2"="">19</div><div number20="" index19=""  alt1"="">20</div><div number21="" index20=""  alt2"="">21</div><div number22="" index21=""  alt1"="">22</div><div number23="" index22=""  alt2"="">23</div><div number24="" index23=""  alt1"="">24</div><div number25="" index24=""  alt2"="">25</div><div number26="" index25=""  alt1"="">26</div><div number27="" index26=""  alt2"="">27</div><div number28="" index27=""  alt1"="">28</div><div number29="" index28=""  alt2"="">29</div><div number30="" index29=""  alt1"="">30</div><div number31="" index30=""  alt2"="">31</div><div number32="" index31=""  alt1"="">32</div><div number33="" index32=""  alt2"="">33</div><div number34="" index33=""  alt1"="">34</div><div number35="" index34=""  alt2"="">35</div><div number36="" index35=""  alt1"="">36</div><div number37="" index36=""  alt2"="">37</div><div number38="" index37=""  alt1"="">38</div><div number39="" index38=""  alt2"="">39</div><div number40="" index39=""  alt1"="">40</div><div number41="" index40=""  alt2"="">41</div><div number42="" index41=""  alt1"="">42</div><div number43="" index42=""  alt2"="">43</div><div number44="" index43=""  alt1"="">44</div><div number45="" index44=""  alt2"="">45</div><div number46="" index45=""  alt1"="">46</div><div number47="" index46=""  alt2"="">47</div><div number48="" index47=""  alt1"="">48</div><div number49="" index48=""  alt2"="">49</div><div number50="" index49=""  alt1"="">50</div><div number51="" index50=""  alt2"="">51</div><div number52="" index51=""  alt1"="">52</div><div number53="" index52=""  alt2"="">53</div><div number54="" index53=""  alt1"="">54</div></td><td><div><div number1="" index0=""  alt2"=""><code keyword"="">using</code> <code plain"="">System;</code></div><div number2="" index1=""  alt1"=""><code keyword"="">using</code> <code plain"="">System.Collections.Generic;</code></div><div number3="" index2=""  alt2"=""><code keyword"="">using</code> <code plain"="">System.Linq;</code></div><div number4="" index3=""  alt1"=""><code keyword"="">using</code> <code plain"="">System.Text;</code></div><div number5="" index4=""  alt2"=""><code keyword"="">using</code> <code plain"="">NUnit.Framework;</code></div><div number6="" index5=""  alt1"=""><code keyword"="">using</code> <code plain"="">OpenQA.Selenium;</code></div><div number7="" index6=""  alt2"=""><code keyword"="">using</code> <code plain"="">OpenQA.Selenium.IE;</code></div><div number8="" index7=""  alt1"=""><code keyword"="">using</code> <code plain"="">OpenQA.Selenium.Support.UI;</code></div><div number9="" index8=""  alt2"="">&nbsp;</div><div number10="" index9=""  alt1"=""><code keyword"="">namespace</code> <code plain"="">TestAutomation</code></div><div number11="" index10=""  alt2"=""><code plain"="">{</code></div><div number12="" index11=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">[TestFixture]</code></div><div number13="" index12=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">class</code> <code plain"="">Driver</code></div><div number14="" index13=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">{</code></div><div number15="" index14=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">IWebDriver driver;</code></div><div number16="" index15=""  alt1"="">&nbsp;</div><div number17="" index16=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">[SetUp]</code></div><div number18="" index17=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">void</code> <code plain"="">Setup()</code></div><div number19="" index18=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">{</code></div><div number20="" index19=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Create a new instance of the Firefox driver</code></div><div number21="" index20=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">driver = </code><code keyword"="">new</code> <code plain"="">InternetExplorerDriver();</code></div><div number22="" index21=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number23="" index22=""  alt2"="">&nbsp;</div><div number24="" index23=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">[TearDown]</code></div><div number25="" index24=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">void</code> <code plain"="">Teardown()</code></div><div number26="" index25=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">{</code></div><div number27="" index26=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">driver.Quit();</code></div><div number28="" index27=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number29="" index28=""  alt2"="">&nbsp;</div><div number30="" index29=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">[Test]</code></div><div number31="" index30=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">void</code> <code plain"="">GoogleSearch()</code></div><div number32="" index31=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">{</code></div><div number33="" index32=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">//Navigate to the site</code></div><div number34="" index33=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">driver.Navigate().GoToUrl(</code><code string"="">"<a href="http://www.google.com">http://www.google.com</a>"</code><code plain"="">);</code></div><div number35="" index34=""  alt2"="">&nbsp;</div><div number36="" index35=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Find the text input element by its name</code></div><div number37="" index36=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">IWebElement query = driver.FindElement(By.Name(</code><code string"="">"q"</code><code plain"="">));</code></div><div number38="" index37=""  alt1"=""><code spaces"="">&nbsp;</code>&nbsp;</div><div number39="" index38=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Enter something to search for</code></div><div number40="" index39=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">query.SendKeys(</code><code string"="">"Selenium"</code><code plain"="">);</code></div><div number41="" index40=""  alt2"=""><code spaces"="">&nbsp;</code>&nbsp;</div><div number42="" index41=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Now submit the form</code></div><div number43="" index42=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">query.Submit();</code></div><div number44="" index43=""  alt1"=""><code spaces"="">&nbsp;</code>&nbsp;</div><div number45="" index44=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Google's search is rendered dynamically with JavaScript.</code></div><div number46="" index45=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">// Wait for the page to load, timeout after 5 seconds</code></div><div number47="" index46=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">WebDriverWait wait = </code><code keyword"="">new</code> <code plain"="">WebDriverWait(driver, TimeSpan.FromSeconds(5));</code></div><div number48="" index47=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">wait.Until((d) =&gt; { </code><code keyword"="">return</code> <code plain"="">d.Title.StartsWith(</code><code string"="">"selenium"</code><code plain"="">); });</code></div><div number49="" index48=""  alt2"=""><code spaces"="">&nbsp;</code>&nbsp;</div><div number50="" index49=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code comments"="">//Check that the Title is what we are expecting</code></div><div number51="" index50=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">Assert.AreEqual(</code><code string"="">"selenium - Google Search"</code><code plain"="">, driver.Title);</code></div><div number52="" index51=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number53="" index52=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number54="" index53=""  alt1"=""><code plain"="">}</code></div></div></td></tr></tbody></table></div></div> <p>Run the tests again. The test should open Internet Explorer and then  open up Google. The result in the Test Explorer would look like this,  just like our NUnit test:</p> <p><a href="http://dotnet.kapenilattex.com/wp-content/uploads/2012/09/Selenium-Test-Result.png"><img src="http://dotnet.kapenilattex.com/wp-content/uploads/2012/09/Selenium-Test-Result.png" alt="" title="Selenium Test Result" size-full=""  wp-image-1117"="" height="562" width="249" /></a></p> <p><strong>Conclusion</strong></p> <p>With the capability to tightly integrate NUnit and Selenium in Visual  Studio 2012 solutions and projects, Microsoft redeems itself by  rectifying the ghosts of MSTest: it brings unit testing much closer to  coding, it finally allows test-first/test driven development, and it  opens up the capability for third-party unit testing frameworks to run  in Visual Studio as first class citizens. This allows other open source  testing frameworks like xUnit.net, QUnit/Jasmine, and MbUnit to run  seamlessly with Visual Studio 2012.</p> <p><strong>References</strong></p> <p>I'd like to point out <a href="http://anoopjshetty.wordpress.com/2012/02/08/creating-test-automation-framework-using-c-selenium-and-nunit/">Anoop Shetty's blog post on Selenium integration</a>  from which I took the steps and code for the Selenium test used in this  post. While his post was applicable to Visual Studio 2010 the steps  were practically the same for Visual Studio 2012.</p> 											</div></div><img src ="http://www.cnitblog.com/stomic/aggbug/88213.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-30 12:04 <a href="http://www.cnitblog.com/stomic/archive/2013/07/30/88213.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)Selenium keyDown,keyUp,keyPress,shiftKeyDown,shiftKeyUp</title><link>http://www.cnitblog.com/stomic/archive/2013/07/22/88093.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Mon, 22 Jul 2013 03:13:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/22/88093.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/88093.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/22/88093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/88093.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/88093.html</trackback:ping><description><![CDATA[<div><p>前几天一直在研究用shiftKeyDown 这个命令，因为经常要用来在table里面多选记录。</p> <p>一开始就用shiftKeyDown, click ,shiftKeyUp 去做。一直没效果。</p> <p>后来觉得在简单的textfield里试，用shiftKeyDown, keyPress,shiftKeyUP.</p> <p>来试验看输入的字母是不是大写的。如果大写的就说明shift key 是有效果的。</p> <p>结果好久都没搞定。尝试了各种方式。</p> <p>最后不知怎么的把 keypress 改成了type 结果竟然有效了。输入可以大写了。也就是说shift key 是有效的。</p> <p>然后再反过来研究前面的多选情况。试着把click改成mouseDown。结果就这样问题解决了。哈哈。</p> <p>其实这个原先碰到过的，选择的时候，有时候 click没用的，只能用mouseDown. 就是在这个问题上面一直没想起来是这个click的问题，一直以为问题出现在shift key 的功能上面。</p> <p><br /> 研究了一下KeyDown,KeyPress 和KeyUp ,发现之间还是有点学问的。</p> <p>让我们带着如下问题来说明，如果你看到这些问题你都知道，那么这篇文章你就当复习吧：）</p> <p>1.这三个事件的顺序是怎么样的？</p> <p>2.KeyDown 触发后，KeyUp是不是一定触发？</p> <p>3.三个事件的定义</p> <p>4.KeyDown、KeyUp 和KeyPress 之间的区别</p> <p>5.如何区分是否按的是小键盘？</p> <p>6.PrScrn按键三个事件能够捕获吗？</p> <p>7.{菜单键}和{开始键}KeyPress能够捕获吗？</p> <p>8.如何捕获系统的组合键？</p> <p>9.通过什么方式捕获PrScrn按键事件？</p> <p>（一）键事件按下列顺序发生：</p> <p>KeyDown</p> <p>KeyPress</p> <p>KeyUp</p> <p>&nbsp;<wbr>（二）KeyDown触发后，不一定触发KeyUp，当KeyDown 按下后，拖动鼠标，那么将不会触发KeyUp事件。</p> <p>&nbsp;<wbr>（三）定义</p> <p>KeyDown：在控件有焦点的情况下按下键时发生。</p> <p>KeyPress：在控件有焦点的情况下按下键时发生。(下面会说和KeyDown 的区别)</p> <p>KeyUp：在控件有焦点的情况下释放键时发生。</p> <p>&nbsp;<wbr>（四）KeyPress 和KeyDown 、KeyPress之间的区别</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 1.KeyPress主要用来捕获数字(注意：包括Shift+数字的符号)、字母（注意：包括大小写）、小键盘等除了F1-12、SHIFT、Alt、 Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单键}、{开始 键}和方向键外的ANSI字符<br />  &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> KeyDown 和KeyUp 通常可以捕获键盘除了PrScrn所有按键(这里不讨论特殊键盘的特殊键）</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 2.KeyPress 只能捕获单个字符</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> KeyDown 和KeyUp 可以捕获组合键。</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 3.KeyPress 可以捕获单个字符的大小写</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 4.KeyDown和KeyUp 对于单个字符捕获的KeyValue 都是一个值，也就是不能判断单个字符的大小写。</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 5.KeyPress 不区分小键盘和主键盘的数字字符。</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> KeyDown 和KeyUp 区分小键盘和主键盘的数字字符。</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 6.其中PrScrn 按键KeyPress、KeyDown和KeyUp 都不能捕获。</p> <p>&nbsp;<wbr>（五）系统组合键的判定</p> <p>在使用键盘的时候，通常会使用到CTRL+SHIFT+ALT 类似的组合键功能。对于此，我们如何来判定？</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 通过KeyUp 事件能够来处理（这里说明一下为什么不用KeyDown，因为在判定KeyDown的时候，CTRL、SHIFT和ALT 属于一直按下状态，然后再加另外一个键是不能准确捕获组合键，所以使用KeyDown 是不能准确判断出的，要通过KeyUp 事件来判定 ）</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 这里简单的列举出CTRL+其它键的组合判定代码：</p> <p>&nbsp;<wbr></p> <p>Code<br /> private void Form3_KeyUp(object sender, KeyEventArgs e)<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> {<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> if (e.Control)<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> {<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> MessageBox.Show("KeyUp:Ctrl+" + e.KeyValue.ToString());<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }<br /> &nbsp;<wbr></p> <p>&nbsp;<wbr>（六）捕获PrScrn按键事件<br /> &nbsp;<wbr>通过一种钩子的方式可以判定PrScrn 按键事件，钩子可以获取任何键盘事件。</p> <p> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 这里给出CodeProject 上面的开源代码的解决方案。大家有兴趣自己研究吧：）。</p></div><img src ="http://www.cnitblog.com/stomic/aggbug/88093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-22 11:13 <a href="http://www.cnitblog.com/stomic/archive/2013/07/22/88093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)在selenium中处理showModalDiaog弹窗和_blank新窗口</title><link>http://www.cnitblog.com/stomic/archive/2013/07/16/88011.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Tue, 16 Jul 2013 09:41:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/16/88011.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/88011.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/16/88011.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/88011.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/88011.html</trackback:ping><description><![CDATA[<div><div style="text-indent: 2em"><span style="color: #1f497d;"><div>[warn] Link has target '_blank', which is not supported in Selenium! Randomizing target to be: selenium_blank4795<br /><br /><div>Link has target  '_blank'这个好像是这样的：当Link有这个属性时，会打开新浏览器窗口，如果target是&#8220;_blank&#8221;的话，Selenium不能找到打 开的新窗口，如果target有一个明确的名字的话，selenium就能够找到这个打开的新窗口并在其上进行操作了。<br /> &#8220;_blank&#8221;的话，selenium不能保证一定找到找准这个窗口，所以给出警告，如果只有这样的一个窗口和主窗口，selenium通过一些变通的方法也能找到该窗口，但是不保证。<br /> 这是selenium本身限制的问题，暂时不晓得怎么解决，只要不影响测试就不用管这个警告。<br /> &#8220;selenium_blank4795 &#8221;这个应该是OpenQA里提给selenium的bug号吧？！</div></div><br /><br />处理弹出窗口是selenium新手常遇到的问题，其中最麻烦是<span style="color: #1f497d">showModalDialog和_blank</span></span></div> <div style="text-indent: 2em">&nbsp;<wbr></div> <div style="text-indent: 2em"><span style="color: #1f497d">showModalDialog是javascript的一种生成弹出窗口的方法，它和一般弹窗不同，</span><span style="color: #1f497d">showModalDialog</span><span style="font-family: simsun">新窗口弹出后，会将父窗口的所有资源（包括</span>selenium<span style="font-family: simsun">）挂起，直至新窗口处理完毕才能返回父窗口。</span></div> <div><span style="font-family: simsun">解决方法是用</span> storeEval<span style="font-family: simsun">注入以下</span>javascript<span style="font-family: simsun">代码，将</span><span style="color: #1f497d">showModalDialog</span><span style="font-family: simsun">方法重写，改成用</span>window.open<span style="font-family: simsun">打开新窗口，一个页面注入一次即可</span></div> <div><span style="color: #1f497d">if(selenium.browserbot.getCurrentWindow().showModalDialog){selenium.browserbot.getCurrentWindow().showModalDialog = function(sURL,vArguments,sFeatures){selenium.browserbot.getCurrentWindow().open(sURL, 'modal', sFeatures);};}</span></div> <div>&nbsp;<wbr></div> <div><span style="color: #1f497d">_blank新窗口</span></div> <div><span style="color: #1f497d">当链接中指定了target="_blank"属性的时候，此时点击链接会在新窗口打开页面，这种新窗口通常没有名字，因此很多人不知如何定位</span></div> <div><span style="color: #1f497d">其实selenium IDE能够自动处理这种情况，当点击一个target="_blank"链接的时候，IDE会自动赋予新窗口一个_blank的名字，这样你就可以用这个名字来定位窗口</span></div> <div><span style="color: #1f497d">以下是一个例子：</span></div> <span style="color: #1f497d"><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=7424a02601014jk3&amp;url=http://s1.sinaimg.cn/orignal/7424a026gc27043baf630" target="_blank"><img name="image_operate_88001339687386233" src="http://s1.sinaimg.cn/middle/7424a026gc27043baf630&amp;690" alt="在selenium中处理showModalDiaog弹窗和_blank新窗口" title="在selenium中处理showModalDiaog弹窗和_blank新窗口" height="93" width="407" /></a></span></div><img src ="http://www.cnitblog.com/stomic/aggbug/88011.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-16 17:41 <a href="http://www.cnitblog.com/stomic/archive/2013/07/16/88011.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)使用Xpath对XML进行模糊查询</title><link>http://www.cnitblog.com/stomic/archive/2013/07/12/87958.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Fri, 12 Jul 2013 06:42:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/12/87958.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87958.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/12/87958.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87958.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87958.html</trackback:ping><description><![CDATA[<div><h2> 			<br /> 		</h2> 		 		<div id="cnblogs_post_body"><p>&nbsp; 如果要对XML文件进行模糊查找的话是一个比较麻烦的事情，Xpath表达式中没有像文件系统中的&#8220;*&#8221;或"?" 或者有像SQL表达式中的"%"，这样的模糊查找的通配符。但是还好，在Xpath的函数中提供了像contains和match这样的函数。</p> <p><strong>contains</strong>是一个字符串查找函数<br /> 　　语法是：fn:contains(string1,string2)，表示如果 string1 包含 string2，则返回 true，否则返回 false。<br /> 　　例如：contains('XML','XM')，结果：true。 </p> <p><strong>match</strong>是一个匹配正则表达式的函数<br /> 　　语法是：fn:matches(string,pattern)，表示如果 string 参数匹配指定的模式，则返回 true，否则返回 false。<br /> 　　例如：matches("12", "[0-9]{1,2}"), 结果：true。</p> <p>　对于Xpath的一些基础知识可以参考：<a href="http://www.cnblogs.com/cbcye/archive/2009/03/09/1407387.html">BizTalk开发系列(三十四) Xpath</a>　这样就可以大概知道如何对XML进行模糊查找了。以下我们还是按照之前那个实例来做几个测试。<br /> </p> <p>&nbsp;</p> <p>测试使用的XML<br /> </p> <div><div><a title="复制代码"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" /></a></div><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff">&lt;</span><span style="color: #800000">Root</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Person </span><span style="color: #ff0000">ID</span><span style="color: #0000ff">="1001"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Name </span><span style="color: #ff0000">lang</span><span style="color: #0000ff">="zh-cn"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000">张城斌</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Email </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="www.quicklearn.cn"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000"> cbcye@live.com </span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Email</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Blog</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">http://cbcye.cnblogs.com</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Blog</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Person</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Person </span><span style="color: #ff0000">ID</span><span style="color: #0000ff">="1002"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Name </span><span style="color: #ff0000">lang</span><span style="color: #0000ff">="en"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000">Gary Zhang</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Email </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="www.quicklearn.cn"</span>&nbsp;<span style="color: #0000ff">&gt;</span><span style="color: #000000"> GaryZhang@cbcye.com</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Email</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Blog</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">http://www.quicklearn.cn</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Blog</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Person</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br /> <img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Root</span><span style="color: #0000ff">&gt;</span><div><a title="复制代码"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" /></a></div></div> <p>&nbsp;</p> <p>使用工具:<strong>XMLSpy</strong>, 注意之前提到了一个开源的Xpath表达式编辑工具:<span style="font-size: 12pt"><span style="background-color: white; font-family: 宋体"><span style="font-size: 12pt"><a href="http://pgfearo.googlepages.com/">SketchPath</a> 在执行查询语句时不能正确的显示查询结果。因此建议使用XMLSpy做以下测试。</span></span></span></p> <p>1.查询所有Blog节点值中带有 cn 字符串的Person节点</p> <p>Xpath表达式：/Root//Person[contains(Blog,'cn')]</p> <p>结果：<br /> &nbsp;<a href="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_16.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="image" src="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_thumb_7.png" border="0" height="142" width="334" /></a> <br /> </p> <p>2.查询所有Blog节点值中带有 cn 字符串并且属性ID值中有01的Person节点</p> <p>Xpath表达式：/Root//Person[contains(Blog,'cn') and contains(@ID,'01')]</p> <p><a href="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_8.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="image" src="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_thumb_3.png" border="0" height="120" width="334" /></a> </p> <p>3.查询受命名空间约束的Email节点的值中带有&#8220;live&#8221;字符串，并且Blog节点值中还带有cn字符串。</p> <p>Xpath表达式：/Root/Person//*[local-name()='Email' and contains(text(),'live')]/parent::Person</p> <p>结果：</p> <p><a href="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_12.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="image" src="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_thumb_5.png" border="0" height="127" width="422" /></a> </p> <p>4.受命名空间约束的节点与不受命名空间约束的节点及属性的混合查询</p> <p>Xpath表达式:/Root/Person//*[local-name()='Email' and contains(lower-case(text()),'live')][contains(../Blog,'cn')][contains(<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#46;&#46;&#47;&#78;&#97;&#109;&#101;&#47;&#64;&#108;&#97;&#110;&#103;&#44;&#37;&#50;&#55;&#122;&#104;&#45;&#99;&#110;&#37;&#50;&#55;&#37;&#50;&#57;&#93;&#47;&#112;&#97;&#114;&#101;&#110;&#116;&#58;&#58;&#80;&#101;&#114;&#115;&#111;&#110;">../Name/@lang,'zh-cn')]/parent::Person</a></p> <p>结果：</p> <p><a href="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_9.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" alt="image" src="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_thumb_1.png" border="0" height="138" width="423" /></a> </p> <p>5.查询所有节点中值符合Email构造的节点</p> <p>Xpath表达式：//*[matches(text(),'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')]</p> <p>结果:</p> <p><a href="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_13.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" alt="image" src="http://images.cnblogs.com/cnblogs_com/cbcye/WindowsLiveWriter/XpathXML_91B6/image_thumb_2.png" border="0" height="143" width="424" /></a>&nbsp; </p> <p>&nbsp;</p> <p>通过以上测试，通过使用contrains函数和match函数来进行模糊查询基本上可以满足基本的使用需求。另外本篇只是列举了几个基本例子，在具体使用的时候还需根据实际的情况灵活运用函数和轴构造Xpath表达式以满足需求。</p> </div></div><img src ="http://www.cnitblog.com/stomic/aggbug/87958.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-12 14:42 <a href="http://www.cnitblog.com/stomic/archive/2013/07/12/87958.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）Webdriver定位之iframe中的元素定位</title><link>http://www.cnitblog.com/stomic/archive/2013/07/10/87920.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Wed, 10 Jul 2013 07:13:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/10/87920.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87920.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/10/87920.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87920.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87920.html</trackback:ping><description><![CDATA[<div> 最近正在进行webdriver的web级自动化测试代码的编写，遇到一个问题：页面有三个标签，单击一个标签打开一页，要对第二个标签打开的页面进行定 位操作，可是总是定位不到。查看一下代码，发现这个标签打开的是一个新页面，在iframe中嵌套着呢，于是在网上搜了一下相关的定位方法。<br />  &nbsp;<div id="sina_keyword_ad_area2"  "=""><wbr>&nbsp;<wbr>&nbsp;<wbr> 例如，现有HTML代码如下：<br /> <ol start="1"><li> <span>main.html&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br /> </span></li><li><span>&lt;&lt;/span&gt;<span>html&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>head&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>title&gt;FrameTest&lt;/&lt;/span&gt;<span>title&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;/&lt;/span&gt;<span>head&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>body&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>div&nbsp;<wbr>id&nbsp;<wbr>=&nbsp;<wbr>"id1"&gt;this&nbsp;<wbr>is&nbsp;<wbr>a&nbsp;<wbr>div!&lt;/&lt;/span&gt;<span>div&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>iframe&nbsp;<wbr>id&nbsp;<wbr>=&nbsp;<wbr>"frame１"&nbsp;<wbr>&nbsp;<wbr>frameborder="0"&nbsp;<wbr>scrolling="no"&nbsp;<wbr>style="left:0;position:absolute;"&nbsp;<wbr>src&nbsp;<wbr>=&nbsp;<wbr>"frame.html"&gt;&lt;/&lt;/span&gt;<span>iframe&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;/&lt;/span&gt;<span>body&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li><span>&lt;/&lt;/span&gt;<span>html&gt;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br /> </span></span></li><li>&nbsp;<wbr>&nbsp;<wbr></li><li>&nbsp;<wbr>&nbsp;<wbr></li><li>frame.html&nbsp;<wbr><br /></li><li><span>&lt;&lt;/span&gt;<span>html&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>head&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>title&gt;this&nbsp;<wbr>is&nbsp;<wbr>a&nbsp;<wbr>frame!&lt;/&lt;/span&gt;<span>title&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;/&lt;/span&gt;<span>head&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>body&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>div&nbsp;<wbr>id&nbsp;<wbr>=&nbsp;<wbr>"div1"&gt;this&nbsp;<wbr>is&nbsp;<wbr>a&nbsp;<wbr>div，too!&lt;/&lt;/span&gt;<span>div&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>label&gt;input:&lt;/&lt;/span&gt;<span>label&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;&lt;/span&gt;<span>input&nbsp;<wbr>id&nbsp;<wbr>=&nbsp;<wbr>"input1"&gt;&lt;/&lt;/span&gt;<span>input&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></span></li><li> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span>&lt;/&lt;/span&gt;<span>body&gt;&nbsp;<wbr>&nbsp;<wbr></span></span></li><li><span>&lt;/&lt;/span&gt;<span>html&gt;</span><br /></span></li></ol> <br /> 1.iFrame有ID 或者 name的情况<br /> //进入id="frame1"的frame中，定位id="div1"的div和id="input1"的输入框。<br /> dr.switchTo().frame("frame1");<br /> dr.findElement(By.id("div1"));<br /> dr.findElement(By.id("input1"))<br /> <br /> 2.如果一个iFrame既没有id，也没有name,通用情况<br /> // 定位frame位置，并选取frame<br /> WebElement frame=driver.findElement(By.xpath( "/html/body/div[2]/div[8]/div[2]/div[3]/div/div[2]/div/iframe" ));<br /> driver.switchTo().frame(frame);<br /> <br /> 3.跳出iFrame<br /> //跳出frame,进入default content;重新定位id="id1"的div<br /> dr.switchTo().defaultContent();<br /> dr.findElement（By.id("id１")）</div></div><img src ="http://www.cnitblog.com/stomic/aggbug/87920.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-10 15:13 <a href="http://www.cnitblog.com/stomic/archive/2013/07/10/87920.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）selenium之WebDriver - 元素定位</title><link>http://www.cnitblog.com/stomic/archive/2013/07/10/87919.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Wed, 10 Jul 2013 06:26:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/10/87919.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87919.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/10/87919.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87919.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87919.html</trackback:ping><description><![CDATA[<div><strong><span style="color:#e53333;">1、什么情况下选用WebDriver？ </span><br /> </strong> <span style="color:#009900;">（1）Selenium-1.0不支持的浏览器功能。 </span> <br />  <span style="color:#009900;">（2）multiple frames, multiple browser windows, popups, and alerts. </span> <br />  <span style="color:#009900;">（3）页面导航。 </span> <br />  <span style="color:#009900;">（4）下拉。 </span> <br />  <span style="color:#009900;">（5）基于AJAX的UI元素。</span>  <br />  <br />  <strong><span style="color:#e53333;">2、WebDriver的几个实现：</span></strong>  <br /> Name of driver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Available on which OS?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class to instantiate  <br /> HtmlUnit Driver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.openqa.selenium.htmlunit.HtmlUnitDriver  <br /> Firefox Driver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; All&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.openqa.selenium.firefox.FirefoxDriver  <br /> Internet Explorer Driver&nbsp;&nbsp;&nbsp;Windows&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;org.openqa.selenium.ie.InternetExplorerDriver  <br /> Chrome Driver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; All&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;org.openqa.selenium.chrome.ChromeDriver  <br />  <br />  <span style="color:#009900;">（1）HtmlUnit Driver是纯java的驱动器，全部在内存中运行，不会实际打开浏览器，但其速度很快。 </span> <br />  <span style="color:#009900;">（2）Firefox Driver：速度慢。</span>  <br />  <br />  <strong><span style="color:#e53333;">3、WebDriver常用的函数接口（以HtmlUnitDriver为例）</span></strong>  <br /> WebDriver driver = new HtmlUnitDriver();  <br />  <em><strong><span style="color:#e53333;">（1）页面导航</span></strong></em>：  <br /> driver.get( "http://www.google.com" ); 完全装载页面后将控制返回给测试脚本。  <br /> 注意：当页面又很多AJAX调用时，该接口无法确认页面是否完全装载，这是需要使用"waits"。  <br />  <em><strong><span style="color:#e53333;">（2）与页面交互 </span><br /> </strong></em>例如：&lt;input type= "text" name= "passwd" id= "passwd-id" /&gt;  <br /> WebElement element;  <br /> element = driver.findElement(By.id( "passwd-id" ));  <br /> element = driver.findElement(By.name( "passwd" ));  <br /> element = driver.findElement(By.xpath( "//input[@id=&#8217;passwd-id&#8217;]" ));  <br /> （XPATH返回第一个匹配到的元素，如果没有匹配到，抛出NoSuchElementException）  <br /> element.sendKeys( "some text" );  <br /> element.sendKeys( " and some" , Keys.ARROW_DOWN); （按向下箭头）  <br /> （任何页面元素都可以调用sendKeys，例如可以测试一些快捷方式键，键盘的每次输入都是附加在文本串的后面）  <br /> element.clear(); 清除文本域内容  <br />  <br />  <em><strong><span style="color:#e53333;">4、填写表单 </span><br /> </strong></em>（1）处理SELECT标签：  <br /> WebElement select = driver.findElement(By.xpath( "//select" ));  <br /> List&lt;WebElement&gt; allOptions = select.findElements(By.tagName( "option" ));  <br /> for (WebElement option : allOptions) {  <br /> System.out.println(String.format( "Value is: %s" , option.getValue()));  <br /> option.setSelected();  <br /> }  <br /> （2）使用Select类处理SELECT标签：  <br /> Select select = new Select(driver.findElement(By.xpath( "//select" )));  <br /> select.deselectAll();  <br /> select.selectByVisibleText( "Edam" );  <br />  <br />  <em><strong><span style="color:#e53333;">5、提交：</span></strong></em>  <br /> （1）driver.findElement(By.id( "submit" )).click();  <br /> （2）element.submit();（要求element必须在表单中，否则抛出NoSuchElementException）  <br />  <br />  <em><strong><span style="color:#e53333;">6、检查元素是否在页面上（并且可以获取元素的尺寸和位置，拖拉元素）： </span><br /> </strong></em>WebElement plain = driver.findElement(By.name( "q" ));  <br /> RenderedWebElement element = (RenderedWebElement) element;（例如HtmlUnit Driver就无法显示出内容）  <br />  <br /> 对元素拖拉：  <br /> RenderedWebElement element = (RenderedWebElement) driver.findElement(By.name( "source" ));  <br /> RenderedWebElement target = (RenderedWebElement) driver.findElement(By.name( "target" ));  <br /> element.dragAndDropOn(target);  <br />  <br />  <em><strong><span style="color:#e53333;">7、在窗口和frame之间移动</span></strong></em>  <br /> （1）在命名的窗口之间移动：switchTo  <br /> driver.switchTo().window( "windowName" ); （可以通过js或者打开窗口的链接获取窗口名称）  <br /> （2）也可通过窗口句柄切换：  <br /> for (String handle : driver.getWindowHandles()) {  <br /> driver.switchTo().window(handle);  <br /> }  <br /> （3）frame之间切换或者切换到iframe：  <br /> driver.switchTo().frame( "frameName" );  <br /> 通过索引访问subframe：  <br /> driver.switchTo().frame( "frameName.0.child" );（frameName的第一个subframe的明价child的frame）  <br />  <br />  <strong><em><span style="color:#e53333;">8、弹出对话框：</span></em></strong>  <br /> 窗口弹出后，可以使用如下语句访问到弹出窗口：  <br /> Alert alert = driver.switchTo().alert();  <br /> 获取到alert窗口后，可进行各种操作。  <br />  <br />  <em><strong>9、页面导航：</strong></em>  <br /> navigate().to()和get()功能完全一样。  <br /> driver.navigate().to( "http://www.example.com" );  <br />  <br /> 浏览器的前进和后退  <br /> driver.navigate().forward();  <br /> driver.navigate().back();  <br /> （注意这两个方法取决于浏览器的实现，可以会出现某些调用失败）  <br />  <br />  <br />  <em><strong><span style="color:#e53333;">10、Cookies（必须首先在目标url域上） </span><br /> </strong></em>// Go to the correct domain  <br /> driver.get( "http://www.example.com" );  <br /> // Now set the cookie. This one&#8217;s valid for the entire domain  <br /> Cookie cookie = new Cookie( "key" , "value" );  <br /> driver.manage().addCookie(cookie);  <br /> // And now output all the available cookies for the current URL  <br /> Set&lt;Cookie&gt; allCookies = driver.manage().getCookies();  <br /> for (Cookie loadedCookie : allCookies) {  <br /> System.out.println(String.format( "%s -&gt; %s" , loadedCookie.getName(), loadedCookie.getValue()));  <br /> }  <br />  <br />  <em><strong><span style="color:#e53333;">12、WebDriver支持的浏览器不如Selenium RC多：</span></strong></em>  <br /> 为了支持更多的浏览器并且使用WebDriver API，可以使用SeleneseCommandExecutor  <br /> Capabilities capabilities = new DesiredCapabilities()  <br /> capabilities.setBrowserName( "safari" );  <br /> CommandExecutor executor = new SeleneseCommandExecutor(  "http:localhost:4444/" , "http://WebDriver driver = new  RemoteWebDriver(executor, capabilities);  <br /> 这种方法的限制条件：目前findElements方法不能使用，并且首先js沙盒。  </div><img src ="http://www.cnitblog.com/stomic/aggbug/87919.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-10 14:26 <a href="http://www.cnitblog.com/stomic/archive/2013/07/10/87919.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转).net下Selenium2使用方法总结 </title><link>http://www.cnitblog.com/stomic/archive/2013/07/10/87917.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Wed, 10 Jul 2013 03:42:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/10/87917.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87917.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/10/87917.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87917.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87917.html</trackback:ping><description><![CDATA[<div><div id="article_content">  <div id="cnblogs_post_body"> <p>一、Selenium简介 <br /> 1.Selenium1(Selenium RC)&nbsp;&nbsp; Selenium2(Selenium WebDriver)&nbsp; Selenium2.0 = Selenium1.0 + WebDriver（也就是说Selenium2.0合并了这两个项目） <br /> 2.WebDriver支持 Firefox (FirefoxDriver)、IE (InternetExplorerDriver)、Opera (OperaDriver) 和Chrome (ChromeDriver) </p> <p>二、日常使用方法总结 <br /> 1.生成一个web对象 <br /> IWebDriver driver; <br /> driver = new FirefoxDriver(); </p> <p>2.跳转到指定页面 <br /> driver.Navigate().GoToUrl(baseURL + "/"); <br /> driver.title 取得当前页的title <br /> driver.url 取得当前页的url </p> <p>3.执行js脚本 <br /> ((IJavaScriptExecutor) driver).ExecuteScript("js") </p> <p>4.定位元素 <br /> &nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("cp1_btnModify")) </p> <p>&nbsp;&nbsp;&nbsp; By.ClassName(className))&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.CssSelector(selector)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.Id(id)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.LinkText(linkText)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.Name(name)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.PartialLinkText(linkText) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.TagName(name)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; By.Xpath(xpathExpression) </p> <p>5.定位frame中元素 <br /> &nbsp;&nbsp;&nbsp; (1)进入frame <br /> &nbsp;&nbsp;&nbsp; driver.SwitchTo().Frame("frame"); </p> <p>&nbsp;&nbsp;&nbsp; (2)定位元素 <br /> &nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("div1")); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("input1")); </p> <p>&nbsp;&nbsp;&nbsp; (3)退出frame <br /> &nbsp;&nbsp;&nbsp; driver.SwitchTo().DefaultContent(); </p> <p>6.如何得到弹出窗口 <br /> &nbsp;&nbsp;&nbsp; (1)当前窗口的句柄 <br /> &nbsp;&nbsp;&nbsp; driver.CurrentWindowHandle </p> <p>&nbsp;&nbsp;&nbsp; (2)所有窗口的句柄 <br /> &nbsp;&nbsp;&nbsp; driver.WindowHandles </p> <p>&nbsp;&nbsp;&nbsp; (3)根据句柄得到窗口 <br /> &nbsp;&nbsp;&nbsp; if driver.CurrentWindowHandle=driver.WindowHandles[i] <br /> &nbsp;&nbsp;&nbsp; IWebDriver window=driver.SwitchTo().Window(driver.WindowHandles[i]) </p> <p>&nbsp;&nbsp;&nbsp; (4)根据窗口得到title,url <br /> &nbsp;&nbsp;&nbsp; window.Title <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.Url </p> <p>7.如何处理alert、confirm、prompt对话框 <br /> &nbsp;&nbsp;&nbsp; (1)取得alert框信息 <br /> &nbsp;&nbsp;&nbsp; Html代码： <br /> &nbsp;&nbsp;&nbsp; &lt;input id = "alert" value = "alert" type = "button" onclick = "alert('欢迎！请按确认继续！');"/&gt;&nbsp; <br /> &nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("alert")).Click(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IAlert alert = driver.SwitchTo().Alert(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(alert.Text); <br /> &nbsp;&nbsp;&nbsp; confirm.Dismiss(); //点弹框关闭 </p> <p>&nbsp;&nbsp;&nbsp; (2)取得输出对话框上面的文字 <br /> &nbsp;&nbsp;&nbsp; Html代码： <br /> &nbsp;&nbsp;&nbsp; &lt;input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定吗？');"/&gt; </p> <p>&nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("confirm")).Click(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IAlert confirm = driver.SwitchTo().Alert(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(confirm.Text); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; confirm.Accept(); //点击确定 </p> <p>&nbsp;&nbsp;&nbsp; (3)点击按钮，输入名字，然后点击确认 <br /> &nbsp;&nbsp;&nbsp; Html代码： <br /> &nbsp;&nbsp;&nbsp; &lt;input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('请输入你的名字:','请输入&nbsp; <br /> &nbsp;&nbsp;&nbsp; 你的名字'); document.write(name) "/&gt; </p> <p>&nbsp;&nbsp;&nbsp; driver.FindElement(By.Id("prompt")).Click(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IAlert prompt = driver.SwitchTo().Alert(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(prompt.Text); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prompt.SendKeys("Hello"); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prompt.Accept(); //点击确定 </p> <p>8.如何来处理select下拉框 <br /> &nbsp;&nbsp;&nbsp; SelectElement selectCity=new SelectElement(driver.FindElement(By.Id("City"))); </p> <p>&nbsp;&nbsp;&nbsp; (1)通过下拉框的索引选中第二项 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; selectCity.SelectByIndex(2); <br /> &nbsp;&nbsp;&nbsp; (2)通过下拉列表中的选项的value属性选中 <br /> &nbsp;&nbsp;&nbsp; selectCity.SelectByValue("10"); </p> <p>&nbsp;&nbsp;&nbsp; (3)通过下拉列表中的选项的Text属性选中 <br /> &nbsp;&nbsp;&nbsp; selectCity.SelectByText("北京"); </p> <p>&nbsp;&nbsp;&nbsp; (4)遍历一下下拉列表所有选项，用click进行选中选项 <br /> &nbsp;&nbsp;&nbsp; foreach(IWebElement e in selectCity.Options) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.Click(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p> <p>9.如何操作cookie <br /> &nbsp;&nbsp;&nbsp; (1)增加一个name = "name",value="value"的cookie <br /> &nbsp;&nbsp;&nbsp; Cookie cookie=new Cookie("name","value"); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; driver.Manage().Cookies.AddCookie(cookie); </p> <p>&nbsp;&nbsp;&nbsp; (2)得到页面下所有的cookies,输入它的所在域、name、value、有效日期、路径 <br /> &nbsp;&nbsp;&nbsp; ICookieJar cookies=driver.Manage().Cookies; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cookie co = cookies.GetCookieNamed("name"); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(co.Domain); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(co.Name); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(co.Value); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(co.Expiry); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(co.Path); <br /> &nbsp;&nbsp;&nbsp; (3)删除cookie三种方法 <br /> &nbsp;&nbsp;&nbsp; a)通过cookie的name <br /> &nbsp;&nbsp;&nbsp; driver.Manage().Cookies.DeleteCookieNamed("CookieName"); </p> <p>&nbsp;&nbsp;&nbsp; b)通过cookie对象 <br /> &nbsp;&nbsp;&nbsp; driver.Manage().Cookies.DeleteCookie(cookie); </p> <p>&nbsp;&nbsp;&nbsp; c)全部删除 <br /> &nbsp;&nbsp;&nbsp; driver.Manage().Cookies.DeleteAllCookies(); <br /> 10.如何等待页面元素加载完成 <br /> &nbsp;&nbsp;&nbsp; (1)明确等待 <br /> &nbsp;&nbsp;&nbsp; (2)隐形等待 </p> <p>11.如何利用Selenium-webdriver截图 <br /> &nbsp;&nbsp;&nbsp; Thread.Sleep(5000); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot(); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; screenShotFile.SaveAsFile("test",ImageFormat.Jpeg); </p> <p>12.如何取得table中的内容 <br /> &nbsp;&nbsp;&nbsp; (1)通过行得到列的方法 <br /> &nbsp;&nbsp;&nbsp; private IWebElement GetCell(IWebElement row,int cell) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IList&lt;IWebElement&gt; cells; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IWebElement target = null; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //列里面有"&lt;th&gt;"、"&lt;td&gt;"两种标签，所以分开处理 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(row.FindElements(By.TagName("th")).Count&gt;0) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cells = row.FindElements(By.TagName("th")); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; target = cells[cell]; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(row.FindElements(By.TagName("td")).Count&gt;0) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cells = row.FindElements(By.TagName("td")); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; target = cells[cell]; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return target; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p> <p>&nbsp;&nbsp;&nbsp; (2)通过By得到行的方法 <br /> &nbsp;&nbsp;&nbsp; public String GetCellText(By by,String tableCellAddress) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //得到table元素 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IWebElement table = driver.FindElement(by); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //对所要查找的单元格位置字符进行分解，得到对应的行、列 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int index = tableCellAddress.Trim().IndexOf('.'); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int row = Convert.ToInt32(tableCellAddress.Substring(0, index)); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int cell = Convert.ToInt32(tableCellAddress.Substring(index + 1)); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //得到table表中所有行对象，并得到所要查询的行对象 <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IList&lt;IWebElement&gt; rows = table.FindElements(By.TagName("tr")); <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IWebElement theRow = rows[row]; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return GetCell(theRow, cell).Text; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p> <p>&nbsp;&nbsp;&nbsp; (3)通过参数得到对应行列的内容 <br /> &nbsp;&nbsp;&nbsp; Console.WriteLine(GetCellText(By.Id("mytable"),"0.2")); //得到id="mytable"中的第一行第三列的表格内容</p> </div>  </div></div><img src ="http://www.cnitblog.com/stomic/aggbug/87917.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-10 11:42 <a href="http://www.cnitblog.com/stomic/archive/2013/07/10/87917.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）selenium 学习（7）--webdriver </title><link>http://www.cnitblog.com/stomic/archive/2013/07/04/87838.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Thu, 04 Jul 2013 07:29:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/04/87838.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87838.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/04/87838.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87838.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87838.html</trackback:ping><description><![CDATA[<div>首先看看《<a rel="nofollow" href="http://www.aosabook.org/en/index.html">开源应用架构（The Architecture of Open Source Applications）</a>》<a rel="nofollow" href="http://www.infoq.com/cn/news/2011/06/selenium-arch">http://www.infoq.com/cn/news/2011/06/selenium-arch</a>&nbsp;中有关webdriver的介绍  <p>  </p><table border="1" cellpadding="1" cellspacing="1" width="80%">  <tbody>  <tr>  <td>&nbsp;从 较高的层次看，Selenium由三种工具组成。第一个工具Selenium  IDE，是Firefox的扩展插件，支持用户录制和回访测试。录制/回访模式?存在局限性，对许多用户来说并不适合，因此第二个工具&#8212;&#8212; Selenium  WebDriver提供了各种语言环境的API来支持更多控制权和编写符合标准软件开发实践的应用程序。最后一个工具&#8212;&#8212;Selenium  Grid帮助工程师使用Selenium  API控制分布在一系列机器上的浏览器实例，支持并发运行更多测试。在项目内部，它们分别被称为&#8220;IDE&#8221;、&#8220;WebDriver&#8221;和&#8220;Grid&#8221;。</td></tr></tbody></table>&nbsp;&nbsp;&nbsp;而现在我们大家能看到的selenium2.0 =selenium1.0+ webdriver  <p>---------------------以上是要事先弄明白的问题，不然还以为webdriver是selenium的一个辅助工具呢（我当初就这么想的）</p>  <p><strong>什么是WebDriver.</strong></p>  <p>  </p><table border="1" cellpadding="1" cellspacing="1" width="80%">  <tbody>  <tr>  <td>&nbsp;WebDriver  is a tool for automating testing web applications, and in particular to  verify that they work as expected. It aims to provide a friendly API  that&#8217;s easy to explore and understand, easier to use than the  Selenium-RC (1.0) API, which will help make your tests easier to read  and maintain. It&#8217;s not tied to any particular test framework, so it can  be used equally well in a unit testing or from a plain old &#8220;main&#8221;  method. </td></tr></tbody></table>&nbsp;&nbsp;&nbsp;webDriver 不与任何测试框架绑定，可以和junit/ TestNG或者其他平台一起工作。（甚至我想，完全可以把它当作一个独立的工具，和selenium没啥关系的工具）  <p><strong>什么时候用webDriver，用webDriver还要不要selenium server了呢？</strong></p>  <p>在以下的情况用webDriver：</p>  <ul><li>1. 多浏览器测试，selenium中不支持的浏览器   </li><li>2. 多框架，多浏览器窗口，弹出框，警告窗口   </li><li>3. 页面导航   </li><li>4. 拖动   </li><li>5. 基因Ajax的UI元素。</li></ul>  <p>有了webDriver还要不要selenium server呢？或许在某种情况下你更本就用启动server&nbsp;&nbsp;&nbsp;&nbsp;   </p><table border="1" cellpadding="1" cellspacing="1" width="80%">  <tbody>  <tr>  <td>&nbsp;You  may, or may not, need the Selenium Server, depending on how you intend  to use Selenium. If you will be strictly using the WebDriver API you do  not need the Selenium Server. The Selenium Server provides Selenium-RC  functionality, which is primarily used for Selenium 1.0 backwards  compatability. Since WebDriver uses completely different technology to  interact with the browsers, the Selenium Server is not needed.  Selenium-WebDriver makes direct calls to the browser using each  browser&#8217;s native support for automation. Selenium-RC however requires  the Selenium- Server to inject javascript into the browser and to then  translate messages from your test program&#8217;s language-specific Selenium  client library into commands that invoke the javascript commands which  in turn, automate the AUT from within the browser. In short, if you&#8217;re  using Selenium-WebDriver, you don&#8217;t need the Selenium-Server.   <p>Another  reason for using the Selenium-Server is if you are using Selenium-Grid  for distributed exectution of your tests. Finally, if you are using  Selenium-backed Web-Driver (the WebDriver API but with back-end Selenium  technology) you will also need the Selenium Server. These topics are  described in more detail later in this chapter.</p></td></tr></tbody></table>&nbsp;&nbsp;&nbsp;用不用server完全取决于你怎么用selenium。  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. 如果你严格只有webDriver的API，那么就不用启动server。</p>  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2. 如果你用Selenium-Grid来分布式执行你的用例，那么就要用server。</p>  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3. 如果你用的webDriver的API，但这个API又用了selenium的某些基层的技术，那么还是要启动server</p>  <p>&nbsp;</p>  <p><strong>WebDriver的几种实现以及区别</strong></p>  <p>  </p><table style="width: 757px; height: 509px;" border="1" cellpadding="1" cellspacing="1" width="757">  <tbody>  <tr>  <td>&nbsp;</td>  <td>&nbsp;特点</td>  <td>&nbsp;执行速度</td></tr>  <tr>  <td>&nbsp;HtmlUnit Driver</td>  <td>&nbsp;   <ul><li>Fastest implementation of WebDriver   </li><li>A pure Java solution and so it is platform independent.   </li><li>Supports JavaScript </li></ul></td>  <td>&nbsp;   <li>Emulates other browsers&#8217; JavaScript behaviour </li></td></tr>  <tr>  <td>&nbsp;Firefox Driver</td>  <td>&nbsp;   <ul><li>Runs in a real browser and supports JavaScript   </li><li>Faster than the <a rel="nofollow" href="http://seleniumhq.org/docs/03_webdriver.html#internet-explorer-driver">Internet Explorer Driver</a> </li></ul></td>  <td>&nbsp;   <li>Slower than the <a rel="nofollow" href="http://seleniumhq.org/docs/03_webdriver.html#htmlunit-driver">HtmlUnit Driver</a> </li></td></tr>  <tr>  <td>&nbsp;IE Driver</td>  <td>&nbsp;   <li>&nbsp; Runs in a real browser and supports JavaScript </li></td>  <td>&nbsp;   <ul><li>Obviously the <a rel="nofollow" href="http://seleniumhq.org/docs/03_webdriver.html#internet-explorer-driver">Internet Explorer Driver</a> will only work on Windows!   </li><li>Comparatively slow (though still pretty snappy :) </li></ul></td></tr>  <tr>  <td>&nbsp;Chrome Driver</td>  <td>&nbsp;   <ul><li>Runs in a real browser and supports JavaScript   </li><li>Because Chrome is a Webkit-based browser, the <a rel="nofollow" href="http://seleniumhq.org/docs/03_webdriver.html#chrome-driver">Chrome Driver</a>  may allow you to verify that your site works in Safari. Note that since  Chrome uses its own V8 JavaScript engine rather than Safari&#8217;s Nitro  engine, JavaScript execution may differ. </li></ul></td>  <td>&nbsp;   <li>Slower than the <a rel="nofollow" href="http://seleniumhq.org/docs/03_webdriver.html#htmlunit-driver">HtmlUnit Driver</a> </li></td></tr></tbody></table>&nbsp;&nbsp;&nbsp;  <p><strong>java 版本的webDriver实践</strong></p>  <p>&nbsp;   </p>      package org.openqa.selenium.example;<br /><br />import org.openqa.selenium.By;<br />import org.openqa.selenium.WebDriver;<br />import org.openqa.selenium.WebElement;<br />import org.openqa.selenium.firefox.FirefoxDriver;<br />import org.openqa.selenium.support.ui.ExpectedCondition;<br />import org.openqa.selenium.support.ui.WebDriverWait;<br /><br />public class Selenium2Example&nbsp; {<br />&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a new instance of the Firefox driver<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Notice that the remainder of the code relies on the interface, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // not the implementation.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebDriver driver = new FirefoxDriver();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // And now use this to visit Google<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; driver.get("http://www.google.com");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Alternatively the same thing can be done like this<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // driver.navigate().to("http://www.google.com");<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Find the text input element by its name<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebElement element = driver.findElement(By.name("q"));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Enter something to search for<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; element.sendKeys("Cheese!");<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Now submit the form. WebDriver will find the form for us from the element<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; element.submit();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Check the title of the page<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Page title is: " + driver.getTitle());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Google's search is rendered dynamically with JavaScript.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Wait for the page to load, timeout after 10 seconds<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (new WebDriverWait(driver, 10)).until(new ExpectedCondition&lt;Boolean&gt;() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Boolean apply(WebDriver d) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return d.getTitle().startsWith("cheese!");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Should see: "cheese! - Google Search"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Page title is: " + driver.getTitle());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Close the browser<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; driver.quit();<br />&nbsp;&nbsp;&nbsp; }<br />}</div><img src ="http://www.cnitblog.com/stomic/aggbug/87838.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-04 15:29 <a href="http://www.cnitblog.com/stomic/archive/2013/07/04/87838.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）Selenium IDE实践（使用Selenium录制）</title><link>http://www.cnitblog.com/stomic/archive/2013/07/04/87837.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Thu, 04 Jul 2013 04:08:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/07/04/87837.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87837.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/07/04/87837.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87837.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87837.html</trackback:ping><description><![CDATA[<div><p>Selenium 是一个很好用的Web自动化测试工具。Aaron很久以前使用过Selenium，不过仅仅用了其提供的API来写测试代码，也在blog上发过一个简单 的代码示例。近来有好几个博友加我MSN问我有关Selenium录制的问题，可惜以前没有使用过，爱莫能助。鉴于此，昨天晚上磨叽了一阵子终于让脚本给 跑起来了。Aaron希望对于那些博友及其他人会有帮助。</p>                         <h3>Selenium IDE简介和安装</h3>                         <p> Selenium录制功能是由Selenium IDE实现的。根据官方网站当前的介绍：</p>                         <p> Selenium IDE is a Firefox add-on that records clicks,                            typing, and other actions to make a test, which you                            can play back in the browser.</p>                         <p> 上面已经写的很清楚了，Selenium IDE可以帮助我们记录下点击，输入等行为并可以支持在浏览器中回放。</p>                         <p> 当然还有一点想必读者已经看出来了，Selenium  IDE是一个Firefox插件，所以Selenium当前的版本（2009-1-18）是不支持IE的录制的，当然这对于脚本的使用影响并不是很大&#8212;&#8212; 除非你是想录制之后直接使用，那你就会失望了，selenium当前的版本并没有达到那种强悍的程度。</p>                         <p> Selenium IDE的安装很简单，如果使用Firefox 2.0，可以直接将官网上下载的文件（例如selenium-ide-1.0-beta-2.xpi）置于Firefox安装路径下的extensions文件夹（默认安装路径C:\Program                            Files\Mozilla Firefox\extensions，而Firefox1.5的默认路径在C:\Program                            Files\Firefox Plus\App\firefox\extensions）下，然后关闭当前打开的Firefox窗口，重新打开即可执行自动安装。Aaron在使用过程中并未出现过安装失败的情况。</p>                         <p>如果你使用的是Firefox 3.0，恭喜你中奖了:你会失败，原因也很简单，selenium IDE目前支持的最高版本还没有达到我们3.0的高度。(更正：感谢一楼提醒，Aaron已验证1.0                            beta 2版本的Selenium IDE可以在Firefox3中安装成功，给大家造成的误导Aaron表示诚挚的歉意)</p>                         <p>另外，需要提醒的是：使用最新版本的Selenium IDE录制可能会导致录制失败（selenium-ide-1.0-beta-1.xpi，selenium-ide-1.0-beta-2.xpi版本在Aaron使用过程中均会出现错误，提示：table                            view is not available in this format），这个bug已经被人提交到Selenium论坛上面去了，提交者使用的是Win                            XP，而Aaron使用的是Win 2003，因此为了避免大家浪费时间&#8212;&#8212;尽管只是一种可能性的浪费，Aaron推荐暂时使用我试验中使用的0.8.7版本。</p>                         <p>声明：因为随着版本的变化，本文中的部分内容与最新版本的使用可能会不一致，因此Aaron介绍的内容适用于于2009-1-18之前的selenium版本：                          </p>                         <table style="border-collapse: collapse" id="table2" border="1" cellpadding="0" cellspacing="0">                           <tbody><tr>                              <td style="border-right: black 1pt solid; padding-right: 5.4pt; border-top: black 1pt solid; padding-left: 5.4pt; padding-bottom: 0cm; border-left: black 1pt solid; width: 142pt; padding-top: 0cm; border-bottom: black 1pt solid; background-color: transparent" valign="top" width="189">                                <p align="center"><strong> Project</strong></p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p align="center"><strong> Release Date</strong></p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p align="center"><strong> Version</strong></p></td>                           </tr>                           <tr>                              <td style="border-right: black 1pt solid; padding-right: 5.4pt; border-top: black 1pt solid; padding-left: 5.4pt; padding-bottom: 0cm; border-left: black 1pt solid; width: 142pt; padding-top: 0cm; border-bottom: black 1pt solid; background-color: transparent" valign="top" width="189">                                <p> Selenium Core</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> Jan 12, 2009</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> 1.0 beta 2</p></td>                           </tr>                           <tr>                              <td style="border-right: black 1pt solid; padding-right: 5.4pt; border-top: black 1pt solid; padding-left: 5.4pt; padding-bottom: 0cm; border-left: black 1pt solid; width: 142pt; padding-top: 0cm; border-bottom: black 1pt solid; background-color: transparent" valign="top" width="189">                                <p> Selenium IDE</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> June 3</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> 2008 1.0 beta 2</p></td>                           </tr>                           <tr>                              <td style="border-right: black 1pt solid; padding-right: 5.4pt; border-top: black 1pt solid; padding-left: 5.4pt; padding-bottom: 0cm; border-left: black 1pt solid; width: 142pt; padding-top: 0cm; border-bottom: black 1pt solid; background-color: transparent" valign="top" width="189">                                <p> Selenium RC</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> Jan 12, 2009</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> 1.0 beta 2</p></td>                           </tr>                           <tr>                              <td style="border-right: black 1pt solid; padding-right: 5.4pt; border-top: black 1pt solid; padding-left: 5.4pt; padding-bottom: 0cm; border-left: black 1pt solid; width: 142pt; padding-top: 0cm; border-bottom: black 1pt solid; background-color: transparent" valign="top" width="189">                                <p> Selenium Grid</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> Nov 30, 2008</p></td>                             <td style="border-right: #ffffff; padding-right: 5.4pt; border-top: #ffffff; padding-left: 5.4pt; padding-bottom: 0cm; border-left: #ffffff; width: 142.05pt; padding-top: 0cm; border-bottom: #ffffff; background-color: transparent" valign="top" width="189">                                <p> 1.0.3</p></td>                           </tr>                         </tbody></table>                         <h3>Selenium IDE脚本录制</h3>                         <p>好了，安装完成了，接下来我们就直接使用IDE来录制吧。启动Firefox浏览器，在Firefox菜单栏中单击&#8220;工具&#8221;菜单，我们会看到Selenium                            IDE是其子菜单：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium1.jpg" alt="" height="315" width="558" /></p>                         <p>单击Selenium IDE项我们可以看到弹出Selenium IDE窗口：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium2.jpg" alt="" height="131" width="556" /></p>                         <p>然后我们就可以使用Selenium IDE进行录制了。至于录制的详细过程我就不详细介绍了，不过还是提醒大家一下，我们可以使用Selenium                            IDE的菜单栏&#8220;Options&#8221;菜单中的&#8220;Format&#8221;子菜单将脚本转化为各自所需的语言类型。</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium3.jpg" alt="" height="201" width="557" /></p>                         <p>Aaron在文章接下来的部分使用C#作为示例语言。</p>                         <h3>编辑Selenium IDE脚本</h3>                         <p> Aaron录制的脚本工作流程是：打开Firefox浏览器-&gt;打开Google首页-&gt;在google搜索框中输入&#8220;google&#8221;-&gt;左键单击&#8220;google                            搜索&#8221;按钮-&gt;在新页面选中&#8220;图片、新闻搜索&#8221;以验证：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium4.jpg" alt="" height="365" width="604" /></p>                         <p>最后得到的C#脚本如下：</p>                         <table border="0" cellpadding="0" cellspacing="0" width="90%">                           <tbody><tr>                             <td bgcolor="F9F9F9"><span>using<span style="color: #000000">&nbsp;System;<br />                               </span></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;<span>System.Text;<br />                               <span style="color: #0000ff">using<span style="color: #000000">&nbsp;System.Text.RegularExpressions;<br />                               </span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;System.Threading;<br />                               </span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;NUnit.Framework;<br />                               </span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;Selenium;<br />                               <br />                               </span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;SeleniumTests<br />                               {<br />                               &nbsp;&nbsp;&nbsp;&nbsp;[TestFixture]<br />                               &nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span>&nbsp;<span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;NewTest<br />                               &nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;ISelenium&nbsp;selenium;<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;StringBuilder&nbsp;verificationErrors;<br />                               <br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[SetUp]<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span>&nbsp;<span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;SetupTest()<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium&nbsp;=&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;DefaultSelenium(</span><span style="color: #800000">"localhost"</span><span style="color: #000000">,&nbsp;</span><span style="color: #800080">4444</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">"*firefox"</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">"http://www.google.com"</span><span style="color: #000000">);<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.Start();<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verificationErrors&nbsp;=&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;StringBuilder();<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               <br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[TearDown]<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span>&nbsp;<span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;TeardownTest()<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span></span></span><span style="color: #000000"><br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.Stop();<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-size: small; color: #0000ff">catch</span><span style="color: #000000">&nbsp;(Exception)<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//&nbsp;Ignore&nbsp;errors&nbsp;if&nbsp;unable&nbsp;to&nbsp;close&nbsp;the&nbsp;browser</span><span style="color: #008000"><br />                               </span> <span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(</span><span style="color: #800000">""</span><span style="color: #000000">,&nbsp;verificationErrors.ToString());<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               <br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Test]<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span>&nbsp;<span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;TheNewTest()<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.Open(</span><span style="color: #800000">"http://www.google.cn/"</span><span style="color: #000000">);<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(</span><span style="color: #800000">"Google"</span><span style="color: #000000">,&nbsp;selenium.GetTitle());<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.Type(</span><span style="color: #800000">"q"</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">"google"</span><span style="color: #000000">);<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.Click(</span><span style="color: #800000">"btnG"</span><span style="color: #000000">);<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selenium.WaitForPageToLoad(</span><span style="color: #800000">"30000"</span><span style="color: #000000">);<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(</span><span style="color: #800000">"google&nbsp;-&nbsp;Google&nbsp;搜索"</span><span style="color: #000000">,&nbsp;selenium.GetTitle());<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000"><br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.IsTrue(selenium.IsTextPresent(</span>"图片、新闻搜索"<span style="color: #000000">));<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">catch</span>&nbsp;(AssertionException&nbsp;e)<br />                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verificationErrors.Append(e.Message);<br />                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />                                &nbsp;&nbsp;&nbsp;&nbsp;}<br />                               }</td>                           </tr>                         </tbody></table>                         <p>一般情况下，这些录制的脚本在Selenium IDE中会重新运行成功，但如果我们将脚本直接拿出来在我们自己的IDE下会怎么样呢？</p>                         <p>为了更方便编辑我们录制的脚本，将这段代码拷贝到VS中：新建一个类库项目TestSeleniumSimple，并将类库项目下的class1.cs文件中的内容用录制的脚本覆盖。编译我们的类库项目TestSeleniumSimple，很遗憾我们看到了</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium5.jpg" alt="" height="304" width="568" /></p>                         <p>不用惊讶，因为我们仔细看一看代码就知道了，原来录制的脚本中引用了一些内容：</p>                         <p> using NUnit.Framework;</p>                         <p>using Selenium; </p>                         <p>对于第一个我们需要安装NunitFramework，这个可以到Nunit官网上下载，如果你 同时下载了Selenium-RC，你可以在\Selenium-RC\selenium-remote-control-1.0-beta-2- dist\selenium-remote-control-1.0-beta-2\selenium-dotnet-client-driver- 1.0-beta-2文件夹下找到它，同时也可以找到我们&#8220;using                             Selenium&#8221;所需要的ThoughtWorks.Selenium.Core.dll，添加对这两个dll的引用，然后再编译。这个时候就可以生成 成功了。</p>                         <h3>在测试框架中回放脚本</h3>                         <p>  好事总是多磨，我们编译成功的脚本很可惜，不能运行。在编译完脚本后出现Nunit不能使用的问题，不知道是偶然还是有必然因素。还好Aaron的脚本是 在虚拟机中录制的，所以Aaron将编译成功的脚本直接拿到了物理机上运行。打开Nunit（Aaron使用的是NUnit-2.4.3-net-2.0 版本），然后导入TestSeleniumSimple.dll(TestSeleniumSimple类库的产品)，点击运行，又出错了：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium6.jpg" alt="" height="437" width="558" /></p>                         <p>原来是服务器，还记得我们刚才引用了Selenium-RC中的两个DLL吗？Selenium  RC中RC是Remote                             Client的意思，既然有Client那么一定是有Server了，实际上还真有这样一个Selenium-server，一个用来启动web浏览器的 家伙。我们找到Selenium-RC的目录下的Server子目录A：</p>                         <p>\Selenium-RC\selenium-remote-control-1.0-beta-2-dist\selenium-remote-control-1.0-beta-2\selenium-server-1.0-beta-2</p>                         <p>里面由一个selenium-server.jar文件，我们需要在命令行下启动这个server。</p>                         <p>在命令行下定位到子目录A处，接着输入：</p>                         <p>Java &#8211;jar selenium-server.jar</p>                         <p>这个时候我们可以看到我们顺利启动了Selenium Server了，试着运行一下刚才失败的测试脚本，终于绿了：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium7.jpg" alt="" height="341" width="558" /></p>                         <p>我们还可以注意到命令行工具中也有运行的信息：</p>                         <p><img src="http://www.uml.org.cn/Test/images/o_Selenium8.jpg" alt="" height="805" width="598" /></p>                         <p>比如我们在上图的倒数第四条信息中看到了：</p>                         <p>13:04:57.406 INFO - Command request: isTextPresent[图片、新闻搜索,                            ] on session</p>                         <p>f68a3d7d0d7b4de8bfdb95ae1c553e6b</p>                         <p>等很详细的内容。</p>                         <h3>总结</h3>                         <p>其实我们也可以不使用NUnit，而直接使用VSTS中Test  Edition组件来运行我们的测试脚本，为了使脚本更稳定或者运行更快，我们也可以编辑脚本对时间做一些处理。我们也可以编辑脚本已增加更多的断言以确 保待测页面显示了我们想要的内容或者没有显示我们不想要的内容等等。</p>                         <p>对于Selenium IDE录制脚本，Aaron稍微总结一下：</p>                         <p>1， 使用Firefox</p>                         <p>2， 编译之前添加对于NUnit.Framework.dll和ThoughtWorks.Selenium.Core.dll的引用</p>                         <p>3， 记得启动Selenium Server</p>                         <p>4， 为提高脚本质量以满足测试稳定性等需求，应该对录制的脚本进行编辑</p>                         <p>  当然，在使用Selenium的过程中，我们还会碰到其他各种奇怪的问题而导致脚本出现问题。限于篇幅和时间问题，Aaron就不继续讨论了。另外，我们 也可以写一些程序来帮助我们更好的使用Selenium（比如自动启动selenium-server，自动运行脚本等等），这些内容Aaron也留给大 家自己去实践~</p>                                                                                                                                         </div><img src ="http://www.cnitblog.com/stomic/aggbug/87837.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-07-04 12:08 <a href="http://www.cnitblog.com/stomic/archive/2013/07/04/87837.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Selenium 2.0 WebDriver with Visual Studio, C#, &amp; IE</title><link>http://www.cnitblog.com/stomic/archive/2013/06/28/87804.html</link><dc:creator>大话人生</dc:creator><author>大话人生</author><pubDate>Fri, 28 Jun 2013 10:03:00 GMT</pubDate><guid>http://www.cnitblog.com/stomic/archive/2013/06/28/87804.html</guid><wfw:comment>http://www.cnitblog.com/stomic/comments/87804.html</wfw:comment><comments>http://www.cnitblog.com/stomic/archive/2013/06/28/87804.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stomic/comments/commentRss/87804.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stomic/services/trackbacks/87804.html</trackback:ping><description><![CDATA[<div><p> 原文：http://www.joecolantonio.com/2012/07/31/getting-started-using-selenium-2-0-webdriver-for-ie-in-visual-studio-c/</p> <p><br /></p> <p><strong>Download Selenium C# Client Drivers</strong><br /> The first step is to download the C# dlls from Selenium's web site.You'll also want to download the Internet Explorer Driver Server:<a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s7.sinaimg.cn/orignal/b6142fb4gd86bef16fe56" target="_blank"><img src="http://s7.sinaimg.cn/mw690/b6142fb4gd86bef16fe56&amp;690" name="image_operate_45791363858180207" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="603" width="499"></a></p> <p><br /> <strong>Configure Visual Studio to work with Selenium</strong><br /></p> <p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s14.sinaimg.cn/orignal/b6142fb4gd86bfaefdb0d" target="_blank"><img src="http://s14.sinaimg.cn/mw690/b6142fb4gd86bfaefdb0d&amp;690" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="339" width="483"></a><br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s14.sinaimg.cn/orignal/b6142fb4gd86bfb7e2f7d" target="_blank"><img src="http://s14.sinaimg.cn/mw690/b6142fb4gd86bfb7e2f7d&amp;690" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="148" width="298"></a><br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s15.sinaimg.cn/orignal/b6142fb4gd86bfc89ad1e" target="_blank"><img src="http://s15.sinaimg.cn/mw690/b6142fb4gd86bfc89ad1e&amp;690" name="image_operate_97941363858339696" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="375" width="379"></a></p> <p><strong>Time to Code!</strong><br /> <br /> Cool!! Now that we have the Selenium dlls referenced in our project, it's time to test the configuration by running a quick test.<br /> <br /> At the top of your project code after the last 'using' namespace add the following Selenium namespaces:<br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s7.sinaimg.cn/orignal/b6142fb4gd86bfcb0d846" target="_blank"><img src="http://s7.sinaimg.cn/mw690/b6142fb4gd86bfcb0d846&amp;690" name="image_operate_6081363858371104" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="324" width="568"></a><br /> Run the test. joecolantonio.com should appear.<br /> <br /> <strong>IE Developer Toolbar</strong><br /> <br /> If you are going to be scripting against IE you'll want to install the Internet Explorer Developer Toolbar. (Those familiar with QTP are aware that this is similar QTP's spy tool)<br /> <br /> Using the IE Dev's Arrow feature allows you to spy on an element's attributes. You can then use those attributes in your Selenium code to help identify fields in you web application.<br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s6.sinaimg.cn/orignal/b6142fb4gd86bfcd71de5" target="_blank"><img src="http://s6.sinaimg.cn/mw690/b6142fb4gd86bfcd71de5&amp;690" name="image_operate_6971363858388308" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="242" width="527"></a></p> <p>For example if we want to navigate to www.joecolantonio.com/HpSupport.html and select a value from the 'Select your tool &amp; Version' we would point the IE Dev arrow to the field to see its attributes:<br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s5.sinaimg.cn/orignal/b6142fb4gd86bfd057f04" target="_blank"><img src="http://s5.sinaimg.cn/mw690/b6142fb4gd86bfd057f04&amp;690" name="image_operate_20301363858451801" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="383" width="540"></a></p> <p><strong>WTF's to look out for</strong><br /> <br /> If you getting the following error:<br /> <br /> "InvalidOperationExceptio<wbr>n was unhandled &#8211; unexpected error launching Internet Explorer. Protected Mode setting are not the same for all zones.<br /> <br /> Enable Protected Mode must be set to the same value (enabled or disabled) for all zones"<br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s2.sinaimg.cn/orignal/b6142fb4gd86bfd11bf51" target="_blank"><img src="http://s2.sinaimg.cn/mw690/b6142fb4gd86bfd11bf51&amp;690" name="image_operate_45931363858427446" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="114" width="424"></a><br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s14.sinaimg.cn/orignal/b6142fb4gd86bfd742c3d" target="_blank"><img src="http://s14.sinaimg.cn/mw690/b6142fb4gd86bfd742c3d&amp;690" name="image_operate_21871363858495410" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="385" width="353"></a></p> <p>If you are getting the following error:<br /> <br /> "DriverServiceNotFoundExc<wbr>eption was unhandled: The file &#8230; \IEDriverServer.exe does not exist. The driver can be downloaded at"<br /> <br /> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=b6142fb401017oo6&amp;url=http://s1.sinaimg.cn/orignal/b6142fb4gd86bfd96e180" target="_blank"><img src="http://s1.sinaimg.cn/mw690/b6142fb4gd86bfd96e180&amp;690" name="image_operate_44431363858540573" alt="Selenium&nbsp;<wbr />2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" title="Selenium&nbsp;<wbr>2.0&nbsp;<wbr>WebDriver&nbsp;<wbr>with&nbsp;<wbr>Visual&nbsp;<wbr>Studio,&nbsp;<wbr>C#,&nbsp;<wbr>&amp;&nbsp;<wbr>IE" height="106" width="443"></a></p> Make sure that you downloaded the IEDriverServer.exe and also that you have the correct path specified in your code</div><img src ="http://www.cnitblog.com/stomic/aggbug/87804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stomic/" target="_blank">大话人生</a> 2013-06-28 18:03 <a href="http://www.cnitblog.com/stomic/archive/2013/06/28/87804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>