2016-11-01 52 views
0

C#,Visual Studio 2015,.NET 4.x Framework,Internet Explorer 11(或最新的Chrome),Windows 8.1 Pro工作站。基於C#的測試自動化連接到現有瀏覽器

出於測試目的,使用用C#編寫的Windows窗體或控制檯應用程序,我需要自動運行在Windows 8或10系統上運行的現有瀏覽器實例。

我創建了一個Windows窗體應用程序,我可以自動進行,我開始使用WebBrowser控件使用應用程序中的Navigate(...)方法和不喜歡的東西點擊在JavaScript彈出按鈕瀏覽器,登錄使用一個用戶名和密碼,從datagridview中選擇一個項目並點擊與該項目關聯的「編輯」按鈕。

但是,一旦單擊「編輯」按鈕,就會創建額外的瀏覽器窗口,這些窗口現在在WebBrowser控件的「範圍」之外運行。

Web應用程序中打開使用window.open(...,_blank);

我已經試過與NewWindow事件工作的瀏覽器的新實例,但我似乎並沒有能夠抓住任何形式的「處理」或這樣的新打開的窗戶。事件觸發了,但是我在事件內部調試時看到的只是關於當前正在處理的窗口(而不是新生成的窗口)的信息。

我試過的其他東西是SeleniumWatIn

對於這兩個例子,我都在www.google.com的Windows 8.1 Pro工作站上運行了Internet Explorer 11實例。

通常,這些示例似乎表明,對於瀏覽器的「附加到現有實例」的瀏覽器,第一次觸發示例。我試圖使用這兩個庫連接到現有的瀏覽器,但我沒有成功。

我試過使用RemoteWebDriver(...) Selenium,使用InternetExplorer驅動程序。另一個Stack Overflow post表示我不需要運行服務器組件,因爲用於測試的瀏覽器和應用程序位於同一臺計算機上。我的代碼如下:

private void doSeleniumStuff() 
{ 
    DesiredCapabilities desired = DesiredCapabilities.InternetExplorer(); 

    using (IWebDriver driver = new RemoteWebDriver(new Uri("http://www.google.com/wd/hub"), desired)) 
    { 
     IWebElement query = driver.FindElement(By.Name("q")); 
     query.SendKeys("Cheese"); 
     query.Submit(); 
     var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
     wait.Until(d => d.Title.StartsWith("cheese", StringComparison.OrdinalIgnoreCase)); 
     Console.WriteLine("Page title is: " + driver.Title); 
    } 
} 

我對RemoteWebDriver構造函數中使用的URL有些困惑。文檔似乎沒有很好地描述這種用法。什麼是「/ wd/hub」用法?

它failes有:

{"Unexpected error. <!DOCTYPE html>\r\n<html lang=en>\r\n <meta charset=utf-8>\r\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\r\n <title>Error 404 (Not Found)!!1</title>\r\n <style>\r\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\r\n </style>\r\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\r\n <p><b>404.</b> <ins>That’s an error.</ins>\r\n <p>The requested URL <code>/wd/hub/session</code> was not found on this server. <ins>That’s all we know.</ins>\r\n"}

我已經使用華廷的AttachTo(...)方法嘗試。

[STAThread] 
private void doWatNStuff() 
{ 
    using (IE myIE = Browser.AttachTo<IE>(Find.Any)) 
    { 
     DomContainer dc = myIE.DomContainer; 
    } 
} 

using無法與

{「無法找到一個IE窗口匹配約束:任何搜索 '30' 秒後過期 。「}

提供華廷的示例代碼,該代碼先創建IE瀏覽器的一個實例,然後連接到它。我不禁想,華廷還可以連接到瀏覽器的運行實例,但華廷首先必須創建一個實例。

這不會滿足我的需求。

我的最後一次嘗試是使用System.Windows.Automation得到一個打開的Internet Explorer窗口,並嘗試與它的工作。雖然我得到的窗口,所有我可以訪問的是WindowsTransform模式。因此,我可能會自動調整瀏覽器的大小w​​i ndow並關閉它。但是,我無法獲得DOM或任何有用的東西。

有一些關於使用Interop與MSHTML或SHDocVw,但沒有什麼超級有用的文章。

我會很感激任何人都可以提供的指導,使用.NET C#Windows窗體或控制檯應用程序的任何可能的工具來以某種方式連接到同一Windows計算機上的獨立打開的瀏覽器窗口並自動執行它。

回答

1

我一直在爲此使用WatiN成功。與Program.cs一個控制檯應用程序看起來像下面的工作對我來說:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using WatiN.Core; 

namespace WatinTest 
{ 
    class Program 
    { 
    [STAThread] 
    static void Main(string[] args) 
    { 
     var ie = IE.AttachTo<IE>(Find.ByTitle(new Regex(".*"))); 
     foreach (var div in ie.Divs) 
     { 
     Console.WriteLine(div.IdOrName); 
     } 
     Console.ReadLine(); 
    } 
    } 
} 

這是與Windows 10和華廷2.1.0。

相關問題