簡體   English   中英

如何從C#應用程序自動化Firefox?

[英]How can you automate Firefox from C# application?

從最簡單的任務開始,從C#應用程序中捕獲Firefox中的URL。 它似乎使用user32.dll Windows API函數將無法正常工作,因為它是在IE中捕獲URL的方法。

我是否需要使用AutoHotkey捕獲URL,例如,我會發送Ctrl + L(將焦點放在地址欄中並突出顯示內容)和Ctrl + C(將選擇復制到剪貼板)。 然后你只需閱讀剪貼板即可獲得信息。

對於更復雜的任務,我會使用Greasemonkey或iMacros擴展,可能是由類似的鍵盤快捷鍵觸發的。

WatiN支持Firefox。

WebAii可以自動化FireFox,包括設置和檢索URL

它似乎非常beta-ey,但有人為mozrepl構建了一個.net連接器 實際上,mozrepl代碼庫剛剛轉移到github 但是mozrepl允許您向Firefox的XUL環境發出命令。

您可以將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, "");
        }
    }
}
}

試試Selenium(Google測試引擎 - http://seleniumhq.org/ )您可以在Firefox中記錄任務(與Web頁面相關的UI),並將錄制內容轉換為C#源代碼:)

我碰到的一個Microsoft工具:

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

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

我的電腦上沒有用於查詢Firefox的UI Spy,所以我不知道這是否有助於解決你的user32.dll問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM