你怎麼能從C#應用程序自動化Firefox?




automation (6)

您可以使用Selenium WebDriver for 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, "");
        }
    }
}
}

從使用C#應用程序捕獲Firefox中最簡單的任務開始。 它似乎使用user32.dll Windows API函數不會像在IE中捕獲URL的方法一樣工作。


Answer #1

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


Answer #2

它似乎是beta-ey,但是有人為mozrepl構建了一個.net連接器 。 實際上,mozrepl codebase剛剛轉移到github 。 但是mozrepl可以讓你向Firefox的XUL環境下發命令。


Answer #3

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



Answer #5

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

對於更複雜的任務,我會使用Greasemonkey或iMacros擴展,也許由類似的鍵盤快捷鍵觸發。





automation