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 ...
随机推荐
- hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce th ...
- BeanUtil体会
把字符串(非纯数字组成的字符串,带有字符的那种)拷贝到int属性中,int属性值设为0 把字符串(纯数字组成的),赋值给double类型,可以直接转换,int类型也可以直接转换成double类型 但是 ...
- Http协议(一)
Http是一种无状态,面向连接的协议.是客户端与服务端进行超文本传输协议(HTTP)的一种通信协议.目前我们使用的是Http/1.1版本. Cookie是解决http无状态,相当于一个只有一天记忆的人 ...
- 基于Twemproxy的Redis集群搭建以及想法
基于Twemproxy的Redis集群方案(转) redis3.0 已经发布了几个月了,但是我这等菜鸟到网上还是没有找到很好的关于搭建redis3.0集群的文章,而且好像很多公司的redis版本还保持 ...
- JavaScript闭包学习笔记
此文都是大牛们关于闭包的观点,在此只是总结. 闭包应用的两种情况即可——函数作为返回值,函数作为参数传递. 1 深入理解javascript原型和闭包 判断一个变量是不是对象非常简单.值类型的类型判断 ...
- MySQL you *might* want to use the less safe log_bin_trust_function_creators variable
因为在打开日志文件情况下执行以前建立的 自定义函数报错详细分析如下: 1 .调用自定义函数 mysql> select sp_function_dbdh_three(); #以前自定义的函数 ...
- BlackJack Strategy
GAME SPEC: 2-deck, 104 cards total. Bellagio has 2-deck and 6-deck games. based on hard 17, dealer h ...
- Spring MVC 拦截 js,css,png 等资源
springMVC的<mvc:resources mapping="***" location="***">标签是在spring3.0.4出现的,主 ...
- java io系列14之 DataInputStream(数据输入流)的认知、源码和示例
本章介绍DataInputStream.我们先对DataInputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解. 转载请注明出处:http://www.cnblogs. ...
- animate.css 一些常用的CSS3动画效果
大家已经开始在项目中使用一些CSS3动画效果了吧,这让网站在高端浏览器上看起来很上流.animate.css是一个老外做的各种CSS3动画的合集,比较全,也很炫,大家可以参考学习一下. 项目主页:ht ...