51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3289|回复: 2
打印 上一主题 下一主题

【转】selenium2 WebDriver 在asp.net项目中的应用

[复制链接]
  • TA的每日心情
    无聊
    前天 09:14
  • 签到天数: 938 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2016-12-6 11:10:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    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. var driver = new FirefoxDriver();
    7. 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; }

    27.   //调用示例
    28.   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();
    6.   //调用示例
    7.   var driver = new InternetExplorerDriver();
    8.    driver.Url = "http://www.baidu.com";
    9.    var cookie = new QA.Cookie("name", "test");
    10.    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);
    5.    // 当页面包含iFrames时,选择页面上的第一帧或主文档。
    6.     IWebDriver DefaultContent();
    7.     // 切换到当前具有焦点的元素  
    8.     IWebElement ActiveElement();
    9.     // 切换到此特定驱动程序实例的当前活动模式对话框   
    10.     IAlert Alert();

    11.   //调用示例
    12.   var driver = new InternetExplorerDriver();
    13.    driver.Url = "http://www.baidu.com";
    14.    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);

    7.     /// 调用示例
    8.     var driver = new InternetExplorerDriver();
    9.     driver.Url = "http://www.baidu.com";
    10.     driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));
    复制代码
    8. 执行 javascript
    测试时也有这样的需求,我们需要在某个时机插入一段待执行的 js代码,可以通过下面的方法实现
    1. public object ExecuteScript(string script, params object[] args);
    2. /// 调用示例
    3. var driver = new InternetExplorerDriver();
    4. driver.Url = "http://www.baidu.com";
    5. driver.ExecuteScript("alert('111');");
    复制代码
    常用的功能基本都介绍完了,功能还是比较强大的,大家有没有发现,它不仅在测试领域可以帮助我们,利用其强大的功能我们也可以应用在非测试的项目中。
    比如 对WEB页面数据的抓取, 也可以做 数据抓取程序 的解析引擎, 想想看用它做个自动登陆的机器人是不是要少写好多代码啊!

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2024-4-15 08:56
  • 签到天数: 1179 天

    连续签到: 1 天

    [LV.10]测试总司令

    3#
    发表于 2016-12-6 21:17:22 | 只看该作者
    好帖子,必须顶!
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-28 02:36 , Processed in 0.068933 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表