喜欢是淡淡的爱,爱是深深的喜欢!!

【转】Selenium自动化工具工作原理

上一篇 / 下一篇  2012-03-16 11:20:14 / 个人分类:selenium

原文作者信息如下,需要更多信息请去原作者博客查看:

作者:hyddd
出处:http://www.cnblogs.com/hyddd/
本文版权归作者所有,欢迎转载,演绎或用于商业目的,但是必须说明本文出处(包含链接)。


==============================================================================================


Selenium简介》 中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python。在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编 译:>,试想如果你有1000个测试案例,每个都要编译,那会给编译服务器很大的压力,而且案例修改后,还得重新编译才能运行:<。但在本系 列的文章中,我还是打算使用C#编写示范例子。

Selenium RC下载:http://seleniumhq.org/download/

写Selenium RC的测试案例

     上一篇《Selenium IDE的使用》中,提到了Selenium IDE可以把录制的脚本转为其他语言的脚本,所以我继续用上一篇的脚本为例子,下面是把脚本语言转换为C#后的代码:

  1. using System;  
  2. using System.Text;  
  3. using System.Text.RegularExpressions;  
  4. using System.Threading;  
  5. using NUnit.Framework;  
  6. using Selenium;  
  7.   
  8. namespace SeleniumTests  
  9. {  
  10.     [TestFixture]  
  11.     public class NewTest  
  12.     {  
  13.         private ISelenium selenium;  
  14.         private StringBuilder verificationErrors;  
  15.           
  16.         [SetUp]  
  17.         public void SetupTest()  
  18.         {  
  19.             selenium = new DefaultSelenium("localhost", 4444, "*chrome""http://change-this-to-the-site-you-are-testing/");  
  20.             selenium.Start();  
  21.             verificationErrors = new StringBuilder();  
  22.         }  
  23.           
  24.         [TearDown]  
  25.         public void TeardownTest()  
  26.         {  
  27.             try  
  28.             {  
  29.                 selenium.Stop();  
  30.             }  
  31.             catch (Exception)  
  32.             {  
  33.                 // Ignore errors if unable to close the browser  
  34.             }  
  35.             Assert.AreEqual("", verificationErrors.ToString());  
  36.         }  
  37.           
  38.         [Test]  
  39.         public void TheNewTest()  
  40.         {  
  41.             selenium.Open("/");  
  42.             selenium.Type("kw""hyddd");  
  43.             selenium.Click("sb");  
  44.             selenium.WaitForPageToLoad("30000");  
  45.             try  
  46.             {  
  47.                 Assert.IsTrue(selenium.IsTextPresent("hyddd - 博客园"));  
  48.             }  
  49.             catch (AssertionException e)  
  50.             {  
  51.                 verificationErrors.Append(e.Message);  
  52.             }  
  53.             selenium.Click("//table[@id='1']/tbody/tr/td/a/font");  
  54.         }  
  55.     }  
  56. }  



在这里,转换后的脚本使用了NUnit测试框架,为了简化,我用VS的Test Project代替(当然你也可以用Console Application建立测试工程的),步骤如下:
1.建立Test Project

2.导入DLL引用

    把selenium-dotnet-client-driver-1.0-beta-2目录中的ThoughtWorks.Selenium.Core.dllThoughtWorks.Selenium.IntegrationTests.dllThoughtWorks.Selenium.UnitTests.dll加入项目:

3.把上面自动生成的代码再改一下

  1. using System;  
  2. using System.Text;  
  3. using System.Collections.Generic;  
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  5. using Selenium;  
  6.   
  7. namespace SeleniumTest  
  8. {  
  9.     [TestClass]  
  10.     public class UnitTest1  
  11.     {  
  12.         [TestMethod]  
  13.         public void Test()  
  14.         {  
  15.             //127.0.0.1为Selenium测试服务器位置。  
  16.             //4444为Selenium测试服务器监听端口。  
  17.             //*iexplore为启动浏览器类型,我把它改为了IE浏览器。  
  18.             //http://www.baidu.com为源地址。  
  19.             ISelenium selenium = new DefaultSelenium("127.0.0.1", 4444, "*iexplore""http://www.baidu.com");  
  20.             selenium.Start();  
  21.             selenium.Open("/");  
  22.             selenium.Type("kw""hyddd");  
  23.             selenium.Click("sb");  
  24.             selenium.WaitForPageToLoad("30000");  
  25.             Assert.IsTrue(selenium.IsTextPresent("hyddd - 博客园"));  
  26.             selenium.Click("//table[@id='1']/tbody/tr/td/a/font");  
  27.       selenium.Close();  
  28.             selenium.Stop();  
  29.         }  
  30.     }  
  31. }  



4.启动Selenium测试服务器
    打开cmd进入selenium-server-1.0-beta-2目录,输入“java -jar selenium-server.jar”
(需要先安装JRE),启动Selenium测试服务器。

5.运行测试案例
(1).运行测试案例:

(2).测试结果:


恩,案例Pass了,如果案例失败的话,Error Meesage会说明失败的原因。
(注意:和Firefox一样,IE下也有屏蔽弹出网页功能,修改设置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings里面配置。)




   前一篇已经比较详细讲述了如何使用Selenium RC进行Web测试,但到底Selenium RC是什么?或者它由哪几部分组成呢??

一.Selenium RC的组成:

关于这个问题,我拿了官网上的一幅图来说明这个问题。

 

Selenium RC主要由两部分组成:

(1).Selenium Server:

 

Selenium Server负责控制浏览器行为,总的来说,Selenium Server主要包括3个部分:LauncherHttp ProxySelenium Core。其中Selenium Core是被Selenium Server嵌入到浏览器页面中的。其实Selenium Core就是一堆JS函数的集合,就是通过这些JS函数,我们才可以实现用程序对浏览器进行操作。

(2).Client Libraries:

写测试案例时用来控制Selenium Server的库。

 

二.Selenium RC与Testcase的关系

先看下图:

 

(1).测试案例(Testcase)通过Client Lib的接口向Selenium Server发送Http请求,要求和Selenium Server建立连接。

为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢?从上文可以看出,Selenium Server是一个独立的中间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。

(2).Selenium Server的Launcher启动浏览器,把Selenium Core加载入浏览器页面当中,并把浏览器的代理设置为Selenium Server的Http Proxy。

(3).测试案例通过Client Lib的接口向Selenium Server发送Http请求,Selenium Server对请求进行解析,然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。

(4).Selenium Core接收到指令后,执行操作。

(5).浏览器收到新的页面请求信息(因为在(4)中,Selenium Core的操作可能引发新的页面请求),于是发送Http请求,请求新的Web页面。
由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。

(6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。

(7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。

为什么Selenium RC中的Selenium Server需要以这种代理服务器的形式存在呢?下一篇继续介绍:>


继续前一篇的问题,为什么Selenium RC中的Selenium Server需要以这种代理服务器的形式存在?其实,这和浏览器的“同源策略”(The Same Origin Policy)有关。

一.什么是同源策略

   同源策略,它是由Netscape提出的一个著名的安全策略,现在所有的可支持javascript的浏览器都会使用这个策略。

为什么需要同源策略,这里举个例子:
    假设现在没有同源策略,会发生什么事情呢?大家知道,JavaScript可以做很多东西,比如:读取/修改网页中某个值。恩,你现在打开了浏览器,在一 个tab窗口中打开了银行网站,在另外一个tab窗口中打开了一个恶意网站,而那个恶意网站挂了一个的专门修改银行信息的JavaScript,当你访问 这个恶意网站并且执行它JavaScript时,你的银行页面就会被这个JavaScript修改,后果会非常严重!而同源策略就为了防止这种事情发生, 看下图:

 

比如说,浏览器的两个tab页中分别打开http://www.baidu.com/index.html和http://www.google.com/index.html,其中,JavaScript1和JavaScript3是属于百度的脚本,而JavaScript2是属于谷歌的脚本,当浏览器的tab1要运行一个脚本时,便会进行同源检查,只有和www.baidu.com源的脚本才能被执行,所谓同源,就是指域名、协议、端口相同。所以,tab1只能执行JavaScript1和JavaScript3脚本,而JavaScript2不能执行,从而防止其他网页对本网页的非法篡改。


二.Selenium Server为什么以这种代理服务器的形式存在
    上面说了同源策略,那同源策略的Selenium Server有什么关系呢??呵呵,上一篇说过,Selenium Core是一堆JS函数的集合,它是我们操作浏览器的基础。当存在同源策略时,便出现一些问题,看下图:

因为Selenium Core的JS脚本的“源”是localhost,所以浏览器会阻止Selenium Core的JS脚本在测试页面上执行,这就是为什么在本系列第一篇中说,如果只使用Selenium Core进行测试,需要把Selenium Core安装到远程服务器上。

    为了解决上面这个问题,Selenium RC中的Selenium Server就以代理服务器的形式出现了,下图说明它是如何借助代理的身份蒙骗浏览器的:>

Selenium Server以代理的形式存在,通过修改WebSite的源信息,从而达到欺骗浏览器的目的,就这样,Selenium RC就轻松绕过了同源策略。在上图中,浏览器会认为WebSite和Selenium Core来自同一个“源”----代理服务器!


TAG:

 

评分:0

我来说两句

Open Toolbar