简体   繁体   中英

how can I automate firefox login to website from c#

I'd like to send a command from ac# app to open a web page in firefox and fill in a form and click a button.

I dont want to use Selenium, what can i do to build this on my own?

The reason frameworks like selenium work is because websites are tempermental and browsers like firefox already implement the requests and responses needed, and frameworks like selenium build on top of that.

As for the profile problem, look here to get some info on creating custom profiles for selenium to use and then instantiate the driver like this:

driver = new FirefoxDriver(new FirefoxProfile(@"...\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\7923jt85.default"));

I personaly prefer Selenium's API. Basically I used the IDE extension in firefox to record and export a C# test case, and then used the output to figure out how Selenium parses the html, and then built a library wrapper to make it customizable to my needs. (The exported test case has a lot of NUnit test framework attributes, I just delete all of those, and call the methods given.)

The example below will open firefox, search google for "Cute Fluffy Cats", and then click on the Images tab. You can use the API to do more if necessary, its just a matter of seeing what is available in the documentation. (You can copy this source, create a console project in visual studio, add the selenium references and see it execute right before you very eyes.)

Another alternative is WatiN, but it is similar to Selenium, a test framework for people who build and want to test the user experience of their websites.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    public class Googletest
    {
        private IWebDriver driver;
        private WebDriverWait wait;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        public static void Main(string[] args)
        {
            var gt = new Googletest();
            gt.SetupTest();
            gt.TheGoogleTest();
            //gt.TeardownTest();
        }

        public void SetupTest()
        {
            driver = new FirefoxDriver();
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            baseURL = "https://www.google.com/";
            verificationErrors = new StringBuilder();
        }

        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
        }

        public void TheGoogleTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            driver.FindElement(By.Id("gbqfq")).Clear();
            driver.FindElement(By.Id("gbqfq")).SendKeys("Cute Fluffy Cats");
            driver.FindElement(By.Id("gbqfb")).Click();
            wait.Until(d => d.FindElement(By.LinkText("Images"))).Click();

        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM