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

引用自:https://www.cnblogs.com/fengh/p/6125215.html

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() 时测试程序报错

扫描二维码关注公众号,回复: 5995044 查看本文章

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页面数据的抓取, 也可以做 数据抓取程序 的解析引擎, 想想看用它做个自动登陆的机器人是不是要少写好多代码啊!

猜你喜欢

转载自blog.csdn.net/badaaasss/article/details/89485456
今日推荐