selenium2 WebDriver 在asp.net项目中的应用
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. 启动浏览器代码
//注意,启动浏览器需要相应的dirver
//火狐浏览器dirver 下载地址 https://github.com/mozilla/geckodriver/releases new FirefoxDriver()
//chrome dirver 下载地址 http://chromedriver.storage.googleapis.com/index.html new QA.Chrome.ChromeDriver();
//IE dirver 下载地址 http://selenium-release.storage.googleapis.com/index.html?path=3.0/ new InternetExplorerDriver()
//HtmlUnit浏览器 new HtmlUnitDriver(); var driver = new FirefoxDriver();
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. 元素选择相关
public IWebElement FindElementById(string id);
public ReadOnlyCollection<IWebElement> FindElementsById(string id);
public IWebElement FindElementByClassName(string className);
public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className);
public IWebElement FindElementByLinkText(string linkText);
public ReadOnlyCollection<IWebElement> FindElementsByLinkText(string linkText);
public IWebElement FindElementByPartialLinkText(string partialLinkText);
public ReadOnlyCollection<IWebElement> FindElementsByPartialLinkText(string partialLinkText);
public IWebElement FindElementByName(string name);
public ReadOnlyCollection<IWebElement> FindElementsByName(string name);
public IWebElement FindElementByTagName(string tagName);
public ReadOnlyCollection<IWebElement> FindElementsByTagName(string tagName);
public IWebElement FindElementByXPath(string xpath);
public ReadOnlyCollection<IWebElement> FindElementsByXPath(string xpath);
public IWebElement FindElementByCssSelector(string cssSelector);
public ReadOnlyCollection<IWebElement> FindElementsByCssSelector(string cssSelector); //调用示例
driver.FindElementById("kw").SendKeys("测试");
3. 用户操作模拟,及控件状态获取
获取控件后进行操作
/// 清空,只对 INPUT 、 TEXTAREA 有效
void Clear();
/// 模拟输入
void SendKeys(string text);
/// 提交表单
void Submit();
/// 点击
void Click();
/// 获取控件 attribute 属性
string GetAttribute(string attributeName);
/// 获取控件 CSS property of this element.
string GetCssValue(string propertyName);
/// 获取控件 tag name
string TagName { get; }
/// 获取控件 innerText
string Text { get; }
/// 获取控件是否启用.
bool Enabled { get; }
/// 获取控件是否被选中 (仅适用、单选、多选、下拉框)
bool Selected { get; }
/// 获取控件在窗口的位置
Point Location { get; }
/// 获取控件尺寸信息
Size Size { get; }
/// 获取控件是否显示.
bool Displayed { get; }
//调用示例
driver.FindElementById("kw").SendKeys("测试");
4. cookie 操作
cookie 的操作通过调用 driver.Manage().Cookies 下面的相关方法 ↓ 来操作。
void AddCookie(Cookie cookie);
Cookie GetCookieNamed(string name);
void DeleteCookie(Cookie cookie);
void DeleteCookieNamed(string name);
void DeleteAllCookies();
//调用示例
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() 的以下方法可以实现窗口切换 ↓
IWebDriver Frame(int frameIndex);
IWebDriver Frame(string frameName);
IWebDriver Frame(IWebElement frameElement);
IWebDriver Window(string windowName);
// 当页面包含iFrames时,选择页面上的第一帧或主文档。
IWebDriver DefaultContent();
// 切换到当前具有焦点的元素
IWebElement ActiveElement();
// 切换到此特定驱动程序实例的当前活动模式对话框
IAlert Alert();
//调用示例
var driver = new InternetExplorerDriver();
driver.Url = "http://www.baidu.com";
driver.SwitchTo().DefaultContent();
6. 浏览器操作
通过 driver.Navigate() 的以下方法↓ 可以实现窗口前进、后退、打开窗口、刷新 操作
/// 回退
void Back();
/// 前进
void Forward();
/// 打开新窗口
void GoToUrl(string url); ///打开新窗口
void GoToUrl(Uri url);
/// 刷新
void Refresh();
7. 页面等待
当页面有异步加载的数据时,如果服务器响应比较慢,我们要做测试的元素可能还没有加载出来,这时候我们需要做相应的等待。
我们可以使用 driver.Manage().Timeouts() 下面的方法 ↓
/// 指定驱动程序在搜索时应等待的时间量
ITimeouts ImplicitlyWait(TimeSpan timeToWait);
/// 指定在异步执行JavaScript时,驱动程序应等待的时间。
ITimeouts SetScriptTimeout(TimeSpan timeToWait);
/// 页面加载时等待的时间
ITimeouts SetPageLoadTimeout(TimeSpan timeToWait);
/// 调用示例
var driver = new InternetExplorerDriver();
driver.Url = "http://www.baidu.com";
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));
8. 执行 javascript
测试时也有这样的需求,我们需要在某个时机插入一段待执行的 js代码,可以通过下面的方法实现
public object ExecuteScript(string script, params object[] args);
/// 调用示例
var driver = new InternetExplorerDriver();
driver.Url = "http://www.baidu.com";
driver.ExecuteScript("alert('111');");
常用的功能基本都介绍完了,功能还是比较强大的,大家有没有发现,它不仅在测试领域可以帮助我们,利用其强大的功能我们也可以应用在非测试的项目中。
比如 对WEB页面数据的抓取, 也可以做 数据抓取程序 的解析引擎, 想想看用它做个自动登陆的机器人是不是要少写好多代码啊!
selenium2 WebDriver 在asp.net项目中的应用的更多相关文章
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...
- ASP.NET项目中引用全局dll
在ASP.NET项目中,有些dll是全局dll,也就是说,没有放在单个项目的引用中.它们一般存放在如下目录C:\Windows\assembly中 这个时候,我们需要在单个项目中引用他们,应该如何做呢 ...
- 如何在ASP.NET 项目中使用Silverlight页面
闲来无事,想写个网站玩玩,比较懒,不想写太多的样式来美化,看中了Silverlight,样式布局比较省事,但是又不想全部都用Silverlight 来写,所以才有此一文. 其实Silverlight最 ...
- VS的ASP.NET项目中cshtml关键词出错 红线,当前上下文中不存在名称
[参考]VS的ASP.NET项目中cshtml突然出错,当前上下文中不存在名称“ViewBag” 原因:web.config 配置错误 这种情况是因为两个web.config文件版本不匹配,需要进行修 ...
- 在ASP.NET项目中使用CKEditor +CKFinder实现图片上传功能
前言 之前的项目中一直使用的是FCKeditor,昨天突然有个想法:为什么不试一下新的CKEditor呢?于是花了大半天的时间去学习它的用法,现在把我的学习过程与大家分享一下. 谈起FCKeditor ...
- ASP.NET项目中使用CKEditor +CKFinder 实现上传图片
CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 在ASP.NET工程中添加CKEdito ...
- 向asp.net项目中添加控件AspNetPager
1.打开项目,把.dll文件放入项目中: 2.在工具栏中添加一个自定义选项卡
- 在ASP.NET项目中使用CKEditor
CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 在ASP.NET工程中添加CKEdito ...
- ASP.MVC 项目中使用 UEditor 文本编辑器
1.下载UEditor 源文件,并导入项目中 2.添加项目中需要使用的CSS和JS //Ueditor 文本编辑器必备的StyleBundle和ScriptBundle StyleBundle ued ...
随机推荐
- CentOS6.4安装Hadoop2.0.5 alpha - 3-Node Cluster
1.在第2个个节点上重复http://www.cnblogs.com/littlesuccess/p/3361497.html文章中的第1-5步 2.修改第1个节点上的hdfs-site.xml中的配 ...
- jQueryMobile控件之展开与合并
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 应用容器Application container
应用容器是最基本的组件,用于布局的容器. 属性 样式 事件 默认白边各24像素,默认为浏览器大小可以设置整体背景 边距等. 根应用文件就是第一个加载的文件.
- node.js学习笔记【1】
http://howtonode.org/how-to-install-nodejs http://www.mcclean-cooper.com/valentino/cygwin_install/
- solr异常解决
使用solr1.4跟solr3.1时,配置了个唯一id类型是sint 或者int,启动时在tomcat后台就会抛出这个异常: org.apache.solr.common.SolrException: ...
- Python 基礎 - bytes數據類型
三元運算 什麼是三元運算?請看下圖說明 透過上圖說明後,可以得出一個三元運算公式: result = 值1 if 條件 else 值2, 如果鯈件為真: result = 值1 如果鯈件為假: res ...
- C++ 中的对象布局
C++中的涉及到虚表时,类对象的布局分为:虚表与数据成员,子类包含派生类布局,假设下面一个程序: #include <iostream> using namespace std; clas ...
- Map/Reduce 工作机制分析 --- 作业的执行流程
前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...
- 华为HG255D路由器使用OH3C进行中大校园网认证
之前用的上海贝尔RG100A-AA路由器,被我无情地摧残了,电源按钮挂了,只能换个路由器.由于在校内,使用OP还是比较方便的,网上淘了这款华为HG255D,店主已刷好OP,无线速率300M,想想也是值 ...
- Neo4j 两种索引Legacy Index与Schema Index区别
Legacy Indexes 在Neo4j 2.0版本之前,Legacy index被称作indexes.这个索引是通过外部图存储在外的Lucene实现,允许“节点”和“联系”以key:value键值 ...