2013-10-29 37 views
1

我想從一個C#應用程序發送一個命令來打開Firefox中的網頁,並填寫一個表單,然後單擊按鈕。我怎樣才能自動從Firefox登錄到網站#

我不想使用硒,我能做些什麼來建立自己的?

+2

爲什麼你不想使用硒? –

+0

我不想每次都要創建新的配置文件,selnium以空白配置文件開始,我也不想複製配置文件,因爲我希望能夠在以後繼續使用會話。我不想使用這個testin,但爲一個應用程序 – user1320651

+0

可能的重複[如何從C#應用程序自動化Firefox?](http://stackoverflow.com/questions/137880/how-can-you-automate-firefox-從-C-尖銳的應用程序) – bmm6o

回答

0

像硒工作框架的原因是因爲網站是tempermental和瀏覽器像firefox已經實現了所需的請求和響應和框架像硒建立在此之上。

至於輪廓問題,看here獲得創建自定義配置文件硒使用,然後實例化這樣的驅動程序的一些信息:

司機=新FirefoxDriver(新FirefoxProfile(@」 ... \應用程序數據\漫遊\ Mozilla的\火狐\ Profiles文件\ 7923jt85.default「));

我個人比較喜歡Selenium的API。基本上我使用Firefox中的IDE擴展來記錄和導出C#測試用例,然後使用輸出來找出Selenium如何解析html,然後構建一個庫包裝器以使其可以根據我的需要進行定製。 (導出的測試用例有很多NUnit測試框架屬性,我只是刪除所有這些,並調用給定的方法。)

下面的示例將打開Firefox,搜索谷歌「可愛的毛茸茸的貓」然後點擊圖像選項卡。 如果需要,您可以使用API​​來做更多事情,只需查看文檔中提供的內容即可。 (您可以複製此源代碼,在Visual Studio中創建一個控制檯項目,添加硒引用,並在您眼前執行它。)

另一種替代方法是WatiN,但它與Selenium相似,它是一個測試框架對於構建並希望測試其網站用戶體驗的用戶。

using System; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.Support.UI; 

namespace SeleniumTests 
{ 
    public class Googletest 
    { 
     private IWebDriver driver; 
     private WebDriverWait wait; 
     private StringBuilder verificationErrors; 
     private string baseURL; 
     private bool acceptNextAlert = true; 

     public static void Main(string[] args) 
     { 
      var gt = new Googletest(); 
      gt.SetupTest(); 
      gt.TheGoogleTest(); 
      //gt.TeardownTest(); 
     } 

     public void SetupTest() 
     { 
      driver = new FirefoxDriver(); 
      wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
      baseURL = "https://www.google.com/"; 
      verificationErrors = new StringBuilder(); 
     } 

     public void TeardownTest() 
     { 
      try 
      { 
       driver.Quit(); 
      } 
      catch (Exception) 
      { 
       // Ignore errors if unable to close the browser 
      } 
     } 

     public void TheGoogleTest() 
     { 
      driver.Navigate().GoToUrl(baseURL + "/"); 
      driver.FindElement(By.Id("gbqfq")).Clear(); 
      driver.FindElement(By.Id("gbqfq")).SendKeys("Cute Fluffy Cats"); 
      driver.FindElement(By.Id("gbqfb")).Click(); 
      wait.Until(d => d.FindElement(By.LinkText("Images"))).Click(); 

     } 
     private bool IsElementPresent(By by) 
     { 
      try 
      { 
       driver.FindElement(by); 
       return true; 
      } 
      catch (NoSuchElementException) 
      { 
       return false; 
      } 
     } 

     private bool IsAlertPresent() 
     { 
      try 
      { 
       driver.SwitchTo().Alert(); 
       return true; 
      } 
      catch (NoAlertPresentException) 
      { 
       return false; 
      } 
     } 

     private string CloseAlertAndGetItsText() 
     { 
      try 
      { 
       IAlert alert = driver.SwitchTo().Alert(); 
       string alertText = alert.Text; 
       if (acceptNextAlert) 
       { 
        alert.Accept(); 
       } 
       else 
       { 
        alert.Dismiss(); 
       } 
       return alertText; 
      } 
      finally 
      { 
       acceptNextAlert = true; 
      } 
     } 
    } 
}