【问题标题】:How can you automate Firefox from C# application?如何从 C# 应用程序自动化 Firefox?
【发布时间】:2010-09-13 08:46:36
【问题描述】:

从最简单的任务开始,即从 C# 应用程序捕获 Firefox 中的 URL。使用 user32.dll Windows API 函数似乎无法像在 IE 中捕获 URL 的方法一样工作。

【问题讨论】:

    标签: c# firefox automation


    【解决方案1】:

    我是否需要使用 AutoHotkey 捕获 URL,例如,我会发送 Ctrl+L(将焦点放在地址栏中并突出显示内容)和 Ctrl+C(将选择复制到剪贴板)。然后,您只需阅读剪贴板即可获取信息。

    对于更复杂的任务,我会使用 Greasemonkey 或 iMacros 扩展,可能由类似的键盘快捷键触发。

    【讨论】:

      【解决方案2】:

      WatiN 支持 Firefox。

      【讨论】:

        【解决方案3】:

        WebAii 可以自动化 FireFox,包括设置和检索 URL

        【讨论】:

          【解决方案4】:

          它看起来非常beta-ey,但有人为mozrepl 构建了一个.net connector。实际上,mozrepl 代码库刚刚移至github。但是 mozrepl 允许您向 Firefox 的 XUL 环境发出命令。

          【讨论】:

            【解决方案5】:

            尝试 Selenium(Google 测试引擎 - http://seleniumhq.org/)您可以记录在 Firefox 中完成的任务(与网页 UI 相关)并将记录转换为 C# 源代码:)

            【讨论】:

              【解决方案6】:

              您可以将 Selenium WebDriver 用于 C#。

              这是一个跨平台 API,可让您使用 Java、C# 等 API 控制各种浏览器。

              使用 Selenium WebDriver 测试的 C# 代码附件。

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using OpenQA.Selenium.Firefox;
              using OpenQA.Selenium;
              using OpenQA.Selenium.Interactions;
              using OpenQA.Selenium.Interactions.Internal;
              using OpenQA.Selenium.Support.UI;
              using OpenQA.Selenium.IE;
              using NUnit.Framework;
              using System.Text.RegularExpressions;
              
              namespace sae_test
              {   class Program
                  {   private static string baseURL;
                      private static StringBuilder verificationErrors;
              
                  static void Main(string[] args)
                  {   // test with firefox
                      IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
                      // test with IE
                      //InternetExplorerOptions options = new InternetExplorerOptions();
                      //options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
                      //IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver(options);
                      SetupTest();
                      driver.Navigate().GoToUrl(baseURL + "Account/Login.aspx");
                      IWebElement inputTextUser = driver.FindElement(By.Id("MainContent_LoginUser_UserName"));
                      inputTextUser.Clear();
                      driver.FindElement(By.Id("MainContent_LoginUser_UserName")).Clear();
                      driver.FindElement(By.Id("MainContent_LoginUser_UserName")).SendKeys("usuario");
                      driver.FindElement(By.Id("MainContent_LoginUser_Password")).Clear();
                      driver.FindElement(By.Id("MainContent_LoginUser_Password")).SendKeys("123");
                      driver.FindElement(By.Id("MainContent_LoginUser_LoginButton")).Click();
                      driver.Navigate().GoToUrl(baseURL + "finanzas/consulta.aspx");
                      // view combo element
                      IWebElement comboBoxSistema = driver.FindElement(By.Id("MainContent_rcbSistema_Arrow"));
                      //Then click when menu option is visible 
                      comboBoxSistema.Click();
                      System.Threading.Thread.Sleep(500);
                      // container of elements systems combo
                      IWebElement listaDesplegableComboSistemas = driver.FindElement(By.Id("MainContent_rcbSistema_DropDown"));
                      listaDesplegableComboSistemas.FindElement(By.XPath("//li[text()='BOMBEO MECANICO']")).Click();
                      System.Threading.Thread.Sleep(500);
                      IWebElement comboBoxEquipo = driver.FindElement(By.Id("MainContent_rcbEquipo_Arrow"));
                      //Then click when menu option is visible 
                      comboBoxEquipo.Click();
                      System.Threading.Thread.Sleep(500);
                      // container of elements equipment combo
                      IWebElement listaDesplegableComboEquipos = driver.FindElement(By.Id("MainContent_rcbEquipo_DropDown"));
              
                      listaDesplegableComboEquipos.FindElement(By.XPath("//li[text()='MINI-V']")).Click();
                      System.Threading.Thread.Sleep(500);
              
                      driver.FindElement(By.Id("MainContent_Button1")).Click();            
                      try
                      {   Assert.AreEqual("BOMBEO MECANICO_22", driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_LabelSistema\"]")).Text);
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      // verify coin format $1,234,567.89 usd
                      try
                      {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelInversionInicial\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      try
                      {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelCostoOpMantto\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      try
                      {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelCostoEnergia\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      try
                      {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelcostoUnitarioEnergia\"]")).Text, "\\$((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})? usd"));
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      // verify number format 1,234,567.89
                      try
                      {   Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.XPath("//*[@id=\"MainContent_RejillaRegistroFinanciero_ctl00_ctl04_labelConsumo\"]")).Text, "((,)*[0-9]*[0-9]*[0-9]+)+(\\.[0-9]{2})?"));
                      }
                      catch (AssertionException e)
                      {   verificationErrors.Append(e.Message);
                      }
                      System.Console.WriteLine("errores: " + verificationErrors);
                      System.Threading.Thread.Sleep(20000);
                      driver.Quit();
                  }
              
                  public static void SetupTest()
                  {   baseURL = "http://127.0.0.1:8081/ver.rel.1.2/";
                      verificationErrors = new StringBuilder();
                  }
              
                  protected static void mouseOver(IWebDriver driver, IWebElement element)
                  {   Actions builder = new Actions(driver);
                      builder.MoveToElement(element);
                      builder.Perform();
                  }
              
                  public static void highlightElement(IWebDriver driver, IWebElement element)
                  {   for (int i = 0; i < 2; i++)
                      {   IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
                          js.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",
                                  element, "color: yellow; border: 2px solid yellow;");
                          js.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);",
                                  element, "");
                      }
                  }
              }
              }
              

              【讨论】:

                【解决方案7】:

                我遇到的一个 Microsoft 工具:

                UI 自动化,作为 .NET 3.5 的一部分 http://msdn.microsoft.com/en-us/library/aa348551.aspx

                这是一个例子: http://msdn.microsoft.com/en-us/library/ms771286.aspx

                我的电脑上没有 UI Spy 来询问 Firefox,所以我不知道这是否有助于解决您的 user32.dll 问题。

                【讨论】:

                  猜你喜欢
                  • 2014-03-25
                  • 2013-09-21
                  • 2021-08-13
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多