基于 C# 的测试自动化附加到现有浏览器

C# Based Testing Automation Attaching to Existing Browser

C#,Visual Studio 2015,.NET 4.x 框架,Internet Explorer 11(或最新 Chrome),Windows 8.1 Pro 工作站。

出于测试目的,使用用 C# 编写的 Windows 表单或控制台应用程序,我需要在 Windows 8 上自动化现有浏览器实例 运行或 10 个系统。

我创建了一个 Windows Forms 应用程序,我能够自动化浏览器,我开始使用 WebBrowser 控件在应用程序中使用 Navigate(...) 方法并执行诸如单击Javascript 弹出窗口中的按钮,使用用户名和密码登录,select datagridview 中的项目,然后单击与该项目关联的 "edit" 按钮。

但是,一旦单击 "edit" 按钮,就会创建其他浏览器 windows,现在 运行 在 WebBrowser 控件的 "scope" 之外。

Web 应用程序使用 window.open(...,_blank);

打开浏览器的新实例

我试过使用 NewWindow 事件,但我似乎无法从新打开的 windows 中获取任何类型的 "handle" 或类似内容。事件触发,但我在事件内部调试时看到的只是关于我当前正在使用的 window 的信息(不是新生成的 window)。

我尝试过的其他东西是 Selenium and WatIn

对于这两个示例,我在 www.google.com 的 Windows 8.1 Pro 工作站上有一个 Internet Explorer 11 运行 实例。

一般来说,示例似乎表明,对于 "attaching to an existing instance" 浏览器,示例首先启动浏览器。我尝试使用这两个库连接到现有浏览器,但没有成功。

我尝试使用 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”的用法是什么?

它失败了:

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

我试过在 WatIn 中使用 AttachTo(...) 方法。

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

using 失败

{"Could not find an IE window matching constraint: Any. Search expired after '30' seconds."}

为 WatIn 提供的示例代码首先创建一个 IE 实例,然后附加到它。我不禁想到 WatIn 可以附加到浏览器的 运行 实例,但 WatIn 必须首先创建该实例。

那不符合我的需要。

我最后的尝试是使用 System.Windows.Automation 打开 Internet Explorer window 并尝试使用它。当我得到 window 时,我只能访问 WindowsTransform 模式。因此,我可能会自动调整浏览器 window 的大小并关闭它。但是,我无法获得 DOM 或任何有用的信息。

有几篇关于将 Interop 与 MSHTML 或 SHDocVw 结合使用的文章,但没有什么特别有用的。

我将不胜感激任何人可以提供任何关于使用 .NET C# 可能的工具的指导 Windows 表单或控制台应用程序以某种方式连接到独立打开的浏览器 window 在同一台 Windows 机器上并使其自动化。

为此我一直在成功使用 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 和 WatiN 2.1.0。