如何从C#应用程序自动化Firefox?
从最简单的任务开始,从C#应用程序捕获Firefox中的URL。使用user32.dll Windows API函数似乎无法像IE中捕获URL的方法那样工作。可以自动化FireFox,包括设置和检索URL如果我需要使用自动热键捕获URL,例如,我将发送Ctrl+L(将焦点放在地址栏中并突出显示内容)和Ctrl+C(将所选内容复制到剪贴板)。然后您只需读取剪贴板即可获得信息如何从C#应用程序自动化Firefox?,c#,firefox,automation,C#,Firefox,Automation,从最简单的任务开始,从C#应用程序捕获Firefox中的URL。使用user32.dll Windows API函数似乎无法像IE中捕获URL的方法那样工作。可以自动化FireFox,包括设置和检索URL如果我需要使用自动热键捕获URL,例如,我将发送Ctrl+L(将焦点放在地址栏中并突出显示内容)和Ctrl+C(将所选内容复制到剪贴板)。然后您只需读取剪贴板即可获得信息 对于更复杂的任务,我会使用Greasemonkey或iMacros扩展,可能由类似的键盘快捷键触发。我遇到的一个Micros
对于更复杂的任务,我会使用Greasemonkey或iMacros扩展,可能由类似的键盘快捷键触发。我遇到的一个Microsoft工具: UI自动化,作为.NET 3.5的一部分 下面是一个例子:
我的电脑上没有用于询问Firefox的UI Spy,因此我不知道这是否有助于解决您的user32.dll问题。这似乎是一个测试版,但有人为其构建了一个。实际上,mozrepl代码库刚刚移动到。但是mozrepl允许您向Firefox的XUL环境发出命令。支持Firefox。y Selenium(谷歌测试引擎-)您可以录制在Firefox中完成的任务(与UI相关的网页),并将录制内容转换为C#source:)您可以使用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, "");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用OpenQA.Selenium.Firefox;
使用OpenQA.Selenium;
使用OpenQA.Selenium.Interactions;
使用OpenQA.Selenium.Interactions.Internal;
使用OpenQA.Selenium.Support.UI;
使用OpenQA.Selenium.IE;
使用NUnit.Framework;
使用System.Text.RegularExpressions;
名称空间sae_测试
{类程序
{私有静态字符串baseURL;
私有静态StringBuilder验证错误;
静态void Main(字符串[]参数)
{//使用firefox进行测试
IWebDriver=new OpenQA.Selenium.Firefox.FirefoxDriver();
//用IE测试
//InternetExploreProptions选项=新建InternetExploreProptions();
//options.introductionInstabilityByIgnoringProtectedModeSettings=true;
//IWebDriver=new OpenQA.Selenium.IE.InternetExplorerDriver(选项);
SetupTest();
driver.Navigate().gotour(baseURL+“Account/Login.aspx”);
IWebElement inputTextUser=driver.FindElement(By.Id(“MainContent\u LoginUser\u UserName”);
inputExtUser.Clear();
driver.FindElement(By.Id(“MainContent\u LoginUser\u UserName”)).Clear();
driver.FindElement(By.Id(“MainContent\u LoginUser\u UserName”)).SendKeys(“usuario”);
driver.FindElement(By.Id(“MainContent\u LoginUser\u Password”)).Clear();
driver.FindElement(By.Id(“MainContent\u LoginUser\u Password”)).SendKeys(“123”);
driver.FindElement(By.Id(“MainContent\u LoginUser\u LoginButton”))。单击();
driver.Navigate().gotour(baseURL+“finanzas/consulta.aspx”);
//视图组合元素
IWebElement comboBoxSistema=driver.FindElement(By.Id(“MainContent\u rcbSistema\u Arrow”);
//然后在菜单选项可见时单击
comboBoxSistema.Click();
系统.线程.线程.睡眠(500);
//元素容器系统组合
IWebElement listaDesplegableComboSistemas=driver.FindElement(By.Id(“MainContent\u rcbSistema\u下拉列表”);
listadespegableCombosistemas.findelelement(By.XPath(“//li[text()='BOMBEO MECANICO']”)。单击();
系统.线程.线程.睡眠(500);
IWebElement ComboxeQuipo=driver.FindElement(By.Id(“MainContent\u rcbEquipo\u Arrow”);
//然后在菜单选项可见时单击
comboBoxEquipo.Click();
系统.线程.线程.睡眠(500);
//元素容器设备组合
IWebElement ListadeSplegableComboeEquipos=driver.findelelement(By.Id(“MainContent\u rcbEquipo\u下拉菜单”);
listadespegableComboeEquipOS.FindElement(By.XPath(“//li[text()='MINI-V']”)。单击();
系统.线程.线程.睡眠(500);
driver.FindElement(By.Id(“MainContent_Button1”))。单击();
尝试
{Assert.AreEqual(“BOMBEO MECANICO_22”,driver.findelelement(By.XPath(“/*[@id=\”main content\u rejillaregistorfinaciero\u ctl00\u ctl04\u labelistema\”))。文本);
}
捕获(断言异常e)
{verificationErrors.Append(e.Message);
}
//验证硬币格式$1234567.89 usd
尝试
{Assert.IsTrue(Regex.IsMatch(driver.findelelement)(By.XPath(“/*[@id=\”MainContent\u rejillaregistrofinaciero\u ctl00\u ctl04\u labelinversionicial\”)))。Text,“\$(,*[0-9]*[0-9]+[0-9]++(\.[0-9]{2})美元”);
}
捕获(断言异常e)
{verificationErrors.Append(e.Message);
}
尝试
{Assert.IsTrue(Regex.IsMatch(driver.findelelement)(By.XPath(“/*[@id=\”main content\u RejillaRegistroFinanciero\u ctl00\u ctl04\u labelcostoopmanto\”))。Text,“\$(,”*[0-9]*[0-9]*[0-9]+)+(\.[0-9]{2})美元”);
}
捕获(断言异常e)
{verificationErrors.Append(e.Message);
}
尝试
{Assert.IsTrue(Regex.IsMatch(driver.findelelement)(By.XPath(“/*[@id=\”MainContent\u RejillaRegistroFinanciero\u ctl00\u ctl04\u labelCostoEnergia\”))Text,“\$(,)*[0-9]*[0-9]+[0-9]+(\.[0-9]{2})美元”);
}
捕获(断言异常e)
{verificationErrors.Append(e.Message);
}
尝试
{Assert.IsTrue(Regex.IsMatch(driver.findelelement)(By.XPath(“/*[@id=\”main content\u RejillaRegistroFinanciero\u ctl00\u ctl04\u labelcostounitarionergia\”))。Text,“\$(,*[0-9]*[0-9]*[0-9]+)+(\.[0-9]{2})美元”);
}
捕获(断言异常e)
{verificationErrors.Append(e.Message);
}
//验证数字格式1234567.89
尝试