2016-11-01 2 views
0

C#, Visual Studio 2015, .NET 4.x 프레임 워크, Internet Explorer 11 (또는 최신 Chrome), Windows 8.1 Pro 워크 스테이션.C# 기반 테스트 자동화 기존 브라우저에 연결

테스트 목적으로 C#으로 작성된 Windows Forms 또는 Console 응용 프로그램을 사용하여 Windows 8 또는 10 시스템에서 실행중인 기존 브라우저 인스턴스를 자동화해야합니다.

Windows Forms 응용 프로그램을 만들었고 WebBrowser 컨트롤을 사용하여 응용 프로그램 내에서 Navigate(...) 메서드를 사용하여 시작하는 브라우저를 자동화 할 수 있었으며 JavaScript 팝업에서 단추를 클릭하고 사용자 이름과 암호를 입력하고, datagridview에서 항목을 선택하고 해당 항목과 관련된 "편집"버튼을 클릭하십시오.

그러나 일단 "편집"버튼을 클릭하면 WebBrowser 컨트롤의 "범위"외부에서 실행중인 추가 브라우저 창이 생성됩니다.

웹 응용 프로그램 내가 NewWindow 이벤트와 작업을 시도했습니다 window.open(...,_blank);

를 사용하여 브라우저의 새 인스턴스를 엽니 다,하지만 난에 "처리"의 어떤 종류를 잡아 수 또는 수하지 않는 것 새로 열린 창. 이벤트가 발생하지만 이벤트 내부에서 디버깅 할 때 표시되는 것은 현재 작업중인 창 (새로 생성 된 창이 아님)에 대한 정보입니다.

다른 시도는 SeleniumWatIn입니다.

두 경우 모두 www.google.com의 Windows 8.1 Pro 워크 스테이션에서 Internet Explorer 11 인스턴스를 실행하고있었습니다.

일반적으로 예제에서는 브라우저의 기존 인스턴스에 연결하기 위해 브라우저에서 먼저 실행되는 예제를 보여줍니다. 두 라이브러리를 모두 사용하여 기존 브라우저에 연결하려고 시도했지만 성공하지 못했습니다.

InternetExplorer 드라이버를 사용하여 Selenium에 RemoteWebDriver(...)을 사용하려고했습니다. 다른 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"사용법은 무엇입니까? 내가 WatIn에서 AttachTo(...) 방법을 사용하여 시도했다

{"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"}

:와

그것은도 실패한.

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

{로 using에 실패 "는 IE 창 매칭 제약 찾을 수 없습니다 :. 모든 검색 '30'초 후 만료를."}

WatIn에 제공된 예제 코드에는 먼저 Internet Explorer의 인스턴스를 만든 다음 연결하는 코드가 있습니다. 실행중인 브라우저 인스턴스에 WatIn을 연결할 수 있다고 생각하지만 Watin 먼저 인스턴스를 작성해야합니다.

를 내 요구를 충족하지 않습니다.

내 마지막 시도가. 열려있는 Internet Explorer 창을 얻을 System.Windows.Automation를 사용하고 작업을 시도했다 나는 창문을 얻을 수 있지만, 모든 접근 할 수있는 곳은 WindowsTransform입니다. 따라서 브라우저의 크기를 자동으로 조정할 수 있습니다. ndow하고 닫으십시오. 그러나 DOM이나 유용한 것을 얻을 수는 없습니다.

Interop에서 MSHTML 또는 SHDocVw을 사용하는 방법에 대한 몇 가지 기사가 있지만 도움이되는 것은 없습니다.

.NET C# Windows Forms 또는 콘솔 응용 프로그램을 사용하여 동일한 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(); 
    } 
    } 
} 

이 윈도우 10과 WatiN 2.1.0와 함께입니다.