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() 時測試程序報錯

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頁面數據的抓取, 也可以做 數據抓取程序 的解析引擎, 想想看用它做個自動登陸的機器人是不是要少寫好多代碼啊!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章