6

I'm looking to write a program that opens up an instance of Firefox, namely the default instance of Firefox that contains my various login information, and then simply switch through a few sites. I'm sort of able to do this, using the following code:

System.Diagnostics.Process.Start("firefox.exe", "thisIsMyURL");

However, as I'm sure you're mostly aware, this simply opens a new Firefox process with the given URL as a default site to open to. In order to do what I want, I'd essentially have to open a new Firefox process, do what I need to on the page, kill the process, and repeat this for each page I need. This is less than ideal. So, I'm hoping that someone will know of a way to programmatically control Firefox, through an API or library or something. I've searched on Google, and so far have only found outdated solutions that didn't really solve my problem in the first place.

As always, thanks for your help! Everything you can offer is appreciated.

Sean Cogan
  • 2,516
  • 2
  • 27
  • 42

3 Answers3

4

You can use Selenium WebDriver for C #.

This is a cross-platform API that allows you to control various browsers using APIs for Java, C#, among others.

Attachment of a code C # with Selenium WebDriver tests.

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, "");
            }
        }
    }
}

download at http://vidadigital.com.mx/publicacion/source/Program.cs

  • This looks like a great start, thank you Victor! I'll check it out and let you know when I've tried it. – Sean Cogan Jan 24 '13 at 05:16
  • Ok, this is definitely a good start. Is there a way to have Selenium use the same extensions as my default Firefox settings? – Sean Cogan Jan 24 '13 at 17:09
3

I was reading MSDN magazine a while back, and I saw an article about a project called "Watir" that piqued my interested, because I was doing a lot of automated testing at the time. I looked into it and found there is actually a project called WatiN which is .NET based. Check it out I think it is exactly what you're looking to do.

http://watin.org/

http://watir.com/

mikey
  • 5,090
  • 3
  • 24
  • 27
0

First start the process using :

Process.Start("firefox.exe", "www.example.com");

Then to kill it you need to do next:

Process[] processes = Process.GetProcessesByName("firefox.exe");
foreach (Process p in processes)
{
    if (p.ProcessName.Equals("firefox.exe", StringComparison.OrdinalIgnoreCase))
    {
        p.Kill();
    }
}
Atif Imtiaz
  • 215
  • 2
  • 4
  • 24
  • He is not asking that, he asking how to automate an instance, not create and kill each one. – Mark Hall Jan 24 '13 at 04:41
  • well i haven't seen anything like that, i shared what i knew. may be if someone else could post a code here, that surely will help all of us :/ – Atif Imtiaz Jan 24 '13 at 04:59
  • you can go through this link i found one similar question here http://stackoverflow.com/questions/2395914/programmatic-control-of-firefox?rq=1 – Atif Imtiaz Jan 24 '13 at 05:00
  • 3
    Yeah, unfortunately, Atif, this solution is exactly what I'm trying to avoid. Thanks for your help anyway, though. – Sean Cogan Jan 24 '13 at 05:15