selenium2 WebDriver是一款跨平台的 自动化测试工具,它可以操纵浏览器,模拟用户行为,非常方便用户进行自动化测试。

.net项目使用它,首先要通过 Visual Studio 的 nugit 包管理器在项目中安装 Selenium WebDirver

它提供了 火狐、chrome、IE、HtmlUnit 浏览器的驱动,用来操作浏览器。

注意,启动浏览器需要相应的dirver ,放到测试程序运行目录里。
火狐浏览器dirver  下载地址  https://github.com/mozilla/geckodriver/releases   
chrome dirver 下载地址    http://chromedriver.storage.googleapis.com/index.html   
IE dirver 下载地址        http://selenium-release.storage.googleapis.com/index.html?path=3.0/

使用 HtmlUnit Driver不会实际打开浏览器,启动速度会比打开浏览器快的多。

1. 启动浏览器代码

  1. //注意,启动浏览器需要相应的dirver
  2. //火狐浏览器dirver 下载地址 https://github.com/mozilla/geckodriver/releases new FirefoxDriver()
  3. //chrome dirver 下载地址 http://chromedriver.storage.googleapis.com/index.html new QA.Chrome.ChromeDriver();
  4. //IE dirver 下载地址 http://selenium-release.storage.googleapis.com/index.html?path=3.0/ new InternetExplorerDriver()
  5. //HtmlUnit浏览器 new HtmlUnitDriver();
  6.  
  7. var driver = new FirefoxDriver();
  8. driver.Url = "https://www.baidu.com/index.php";

我在使用 new InternetExplorerDriver() 时测试程序报错

Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

我们只要改变IE浏览器安全设置就可以了, 打开 internet 选项,进入安全标签 ,把 Internate 、本地Internate  、可信站点 、受限站点  的“启用保护模式” 设置一致,既 都勾选,或都不勾选。


selenium2 WebDriver 它提供了一系列操作浏览器的API

 2. 元素选择相关

  1. public IWebElement FindElementById(string id);
  2. public ReadOnlyCollection<IWebElement> FindElementsById(string id);
  3. public IWebElement FindElementByClassName(string className);
  4. public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className);
  5. public IWebElement FindElementByLinkText(string linkText);
  6. public ReadOnlyCollection<IWebElement> FindElementsByLinkText(string linkText);
  7. public IWebElement FindElementByPartialLinkText(string partialLinkText);
  8. public ReadOnlyCollection<IWebElement> FindElementsByPartialLinkText(string partialLinkText);
  9. public IWebElement FindElementByName(string name);
  10. public ReadOnlyCollection<IWebElement> FindElementsByName(string name);
  11. public IWebElement FindElementByTagName(string tagName);
  12. public ReadOnlyCollection<IWebElement> FindElementsByTagName(string tagName);
  13. public IWebElement FindElementByXPath(string xpath);
  14. public ReadOnlyCollection<IWebElement> FindElementsByXPath(string xpath);
  15. public IWebElement FindElementByCssSelector(string cssSelector);
  16. public ReadOnlyCollection<IWebElement> FindElementsByCssSelector(string cssSelector);
  17.  
  18.   //调用示例
      driver.FindElementById("kw").SendKeys("测试");

3. 用户操作模拟,及控件状态获取

获取控件后进行操作

  1. /// 清空,只对 INPUT 、 TEXTAREA 有效
  2. void Clear();
  3. /// 模拟输入
  4. void SendKeys(string text);
  5. /// 提交表单
  6. void Submit();
  7. /// 点击
  8. void Click();
  9. /// 获取控件 attribute 属性
  10. string GetAttribute(string attributeName);
  11. /// 获取控件 CSS property of this element.
  12. string GetCssValue(string propertyName);
  13. /// 获取控件 tag name
  14. string TagName { get; }
  15. /// 获取控件 innerText
  16. string Text { get; }
  17. /// 获取控件是否启用.
  18. bool Enabled { get; }
  19. /// 获取控件是否被选中 (仅适用、单选、多选、下拉框)
  20. bool Selected { get; }
  21. /// 获取控件在窗口的位置
  22. Point Location { get; }
  23. /// 获取控件尺寸信息
  24. Size Size { get; }
  25. /// 获取控件是否显示.
  26. bool Displayed { get; }
  1.   //调用示例
      driver.FindElementById("kw").SendKeys("测试");

4. cookie 操作

cookie 的操作通过调用 driver.Manage().Cookies  下面的相关方法 ↓ 来操作。

  1. void AddCookie(Cookie cookie);
  2. Cookie GetCookieNamed(string name);
  3. void DeleteCookie(Cookie cookie);
  4. void DeleteCookieNamed(string name);
  5. void DeleteAllCookies();
  1.   //调用示例
      var driver = new InternetExplorerDriver();
       driver.Url = "http://www.baidu.com";
       var cookie = new QA.Cookie("name", "test");
    driver.Manage().Cookies.AddCookie(cookie);

5.窗口切换

通过  driver.SwitchTo() 的以下方法可以实现窗口切换 ↓

  1. IWebDriver Frame(int frameIndex);
  2. IWebDriver Frame(string frameName);
  3. IWebDriver Frame(IWebElement frameElement);
  4. IWebDriver Window(string windowName);
       // 当页面包含iFrames时,选择页面上的第一帧或主文档。
  5. IWebDriver DefaultContent();
    // 切换到当前具有焦点的元素
    IWebElement ActiveElement();
    // 切换到此特定驱动程序实例的当前活动模式对话框
    IAlert Alert();
  1.   //调用示例
      var driver = new InternetExplorerDriver();
       driver.Url = "http://www.baidu.com";
       driver.SwitchTo().DefaultContent();

6. 浏览器操作

通过  driver.Navigate() 的以下方法↓  可以实现窗口前进、后退、打开窗口、刷新 操作

  1. /// 回退
  2. void Back();
  3. /// 前进
  4. void Forward();
  5. /// 打开新窗口
  6. void GoToUrl(string url); ///打开新窗口
  7. void GoToUrl(Uri url);
  8. /// 刷新
  9. void Refresh();

7. 页面等待

当页面有异步加载的数据时,如果服务器响应比较慢,我们要做测试的元素可能还没有加载出来,这时候我们需要做相应的等待。

我们可以使用  driver.Manage().Timeouts()  下面的方法 ↓

  1. /// 指定驱动程序在搜索时应等待的时间量
  2. ITimeouts ImplicitlyWait(TimeSpan timeToWait);
  3. /// 指定在异步执行JavaScript时,驱动程序应等待的时间。
  4. ITimeouts SetScriptTimeout(TimeSpan timeToWait);
  5. /// 页面加载时等待的时间
  6. ITimeouts SetPageLoadTimeout(TimeSpan timeToWait);
  1. /// 调用示例
    var driver = new InternetExplorerDriver();
        driver.Url = "http://www.baidu.com";
      driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));

8. 执行 javascript

测试时也有这样的需求,我们需要在某个时机插入一段待执行的 js代码,可以通过下面的方法实现

  1. public object ExecuteScript(string script, params object[] args);
  1. /// 调用示例
    var driver = new InternetExplorerDriver();
    driver.Url = "http://www.baidu.com";
    driver.ExecuteScript("alert('111');");

常用的功能基本都介绍完了,功能还是比较强大的,大家有没有发现,它不仅在测试领域可以帮助我们,利用其强大的功能我们也可以应用在非测试的项目中。

比如 对WEB页面数据的抓取, 也可以做 数据抓取程序 的解析引擎, 想想看用它做个自动登陆的机器人是不是要少写好多代码啊!

selenium2 WebDriver 在asp.net项目中的应用的更多相关文章

  1. win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件

    win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...

  2. ASP.NET项目中引用全局dll

    在ASP.NET项目中,有些dll是全局dll,也就是说,没有放在单个项目的引用中.它们一般存放在如下目录C:\Windows\assembly中 这个时候,我们需要在单个项目中引用他们,应该如何做呢 ...

  3. 如何在ASP.NET 项目中使用Silverlight页面

    闲来无事,想写个网站玩玩,比较懒,不想写太多的样式来美化,看中了Silverlight,样式布局比较省事,但是又不想全部都用Silverlight 来写,所以才有此一文. 其实Silverlight最 ...

  4. VS的ASP.NET项目中cshtml关键词出错 红线,当前上下文中不存在名称

    [参考]VS的ASP.NET项目中cshtml突然出错,当前上下文中不存在名称“ViewBag” 原因:web.config 配置错误 这种情况是因为两个web.config文件版本不匹配,需要进行修 ...

  5. 在ASP.NET项目中使用CKEditor +CKFinder实现图片上传功能

    前言 之前的项目中一直使用的是FCKeditor,昨天突然有个想法:为什么不试一下新的CKEditor呢?于是花了大半天的时间去学习它的用法,现在把我的学习过程与大家分享一下. 谈起FCKeditor ...

  6. ASP.NET项目中使用CKEditor +CKFinder 实现上传图片

    CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 在ASP.NET工程中添加CKEdito ...

  7. 向asp.net项目中添加控件AspNetPager

    1.打开项目,把.dll文件放入项目中: 2.在工具栏中添加一个自定义选项卡

  8. 在ASP.NET项目中使用CKEditor

    CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 在ASP.NET工程中添加CKEdito ...

  9. ASP.MVC 项目中使用 UEditor 文本编辑器

    1.下载UEditor 源文件,并导入项目中 2.添加项目中需要使用的CSS和JS //Ueditor 文本编辑器必备的StyleBundle和ScriptBundle StyleBundle ued ...

随机推荐

  1. 关于uboot和kernel的一些理解

    经过多次的修改和实验,终于能够在mini2440开发板上进行各种uboot和kernel的挂载实验了,在此期间学习到了很多知识,也理解了一些知识1->分区uboot和kernel的分区表要一致u ...

  2. (转)深入理解JavaScript 模块模式

    深入理解JavaScript 模块模式 (原文)http://www.cnblogs.com/starweb/archive/2013/02/17/2914023.html 英文:http://www ...

  3. Genymotion--最快的安卓模拟器 测试与模拟APP应用必备

    命令行工具,Eclipse插件,多操作系统 1 易于安装,易于运行 超过10个虚拟设备 您很匆忙?您想测试市场的主要设备?使用我们的虚拟设备! 2 控制功能强大的传感器来测试您的应用程序 自定义你的测 ...

  4. AngularJS之手动加载模块app和controller

    使用ng的页面中一般都是使用模块自动加载,页面的结构一般是这样的 加载angularjs脚本 加载业务代码脚本(或者写在script标签中) html结构代码(带有ng指令) 就像这样 app.htm ...

  5. Python 爬虫学习 网页图片下载

    使用正则表达式匹配 # coding:utf-8 import re import urllib def get_content(url): """ Evilxr, &q ...

  6. QString转换为char* (转)

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  7. cookie,session,sessionid

    cookie,session,sessionid http协议是无状态的,意思是每次请求的状态不会保存.因此,产生了cookie,session之类保存会话状态的机制.1.什么是cookiecooki ...

  8. python 使用openpyxl来写数据到excel表格

    使用openpyxl写execl确实很方便.我先介绍用到的相关模块与函数 Workbook:工作簿模块,在内存创建一个工作簿. ExcelWriter:使用它向exel中写数据. get_column ...

  9. osx 文本编辑工具下载地址Sublime Text 3

    下载地址: http://www.sublimetext.com/3 Sublime Text 是一个代码编辑器(Sublime Text 3是收费软件,但可以无限期试用),也是HTML和散文先进的文 ...

  10. Java加密算法 RSA

    Java加密算法 RSA 2015-06-06 08:44 511人阅读 评论(0) 收藏 举报  分类: JAVA(57)  公钥加密也称为非对称加密.速度慢.加密和解密的钥匙不相同,某一个人持有私 ...