Web Automation with Selenium (C#)

Web Automation is a quite regular task nowadays, scripting for repeated operations and testing. Selenium is a good toolkit for this kind of tasks.

There are four subprojects in Selenium:

Selenuim IDE is a firefox addon. It can record and replay your actions in firefox, then export scripts in your desired language (Selenese, Java, C# or other bindings). Selenium WebDriver is used for driving a browser natively in your language binding, including:

We will use FirefoxDriver, ChromeDriver and InternetExplorerDriver in C# here.

Step 1: Download selenium-dotnet-2.37.0.zip

http://code.google.com/p/selenium/downloads/detail?name=selenium-dotnet-2.37.0.zip&can=2&q=

Step 2: Setup the environment

Create an directory for selenium files <selenium>. Then extract selenium-dotnet-2.37.0.zip to <selenium>/lib.

NAnt script for building:

<?xml version="1.0"?>
<project name="selenium" default="run">
    <property name="debug" value="true" />
    <property name="outdir" value="bin" />
    <property name="libdir" value="lib/net40" />
    <property name="datadir" value="data" />
    <target name="clean" description="remove all generated files">
    <!-- Files: '*.xml; *.pdb' -->
        <delete>
            <fileset>
                <include name="*.xml" />
                <include name="*.exe" />
                <include name="*.pdb" />
            </fileset>
        </delete>
        <delete dir="${outdir}" />
    </target>
    <target name="build" description="compiles the source code">
        <mkdir dir="${outdir}" />
        <foreach item="File" property="filename">
            <in>
                <items>
                    <include name="*.cs" />
                    <exclude name="*Test.cs" />
                </items>
            </in>
            <do>
                <echo message="${filename}" />
                <csc debug="${debug}" output="${path::combine(path::combine(path::get-directory-name(filename), outdir), path::get-file-name(path::change-extension(filename, 'exe')))}" target="exe">
                    <sources>
                        <include name="${filename}" />
                    </sources>
                    <references basedir="${libdir}">
                        <include name="WebDriver.dll" />
                        <include name="WebDriver.Support.dll" />
                    </references>
                </csc>
            </do>
        </foreach>
        <foreach item="File" property="filename">
            <in>
                <items>
                    <include name="*Test.cs" />
                </items>
            </in>
            <do>
                <echo message="${filename}" />
                <csc debug="${debug}" output="${path::combine(path::combine(path::get-directory-name(filename), outdir), path::get-file-name(path::change-extension(filename, 'dll')))}" target="library">
                    <sources>
                        <include name="${filename}" />
                    </sources>
                    <references basedir=".">
                        <include name="${nant::scan-probing-paths('nunit.framework.dll')}" />
                        <include name="${libdir}/WebDriver.dll" />
                        <include name="${libdir}/WebDriver.Support.dll" />
                    </references>
                </csc>
            </do>
        </foreach>
        <copy todir="${outdir}">
            <fileset basedir="${libdir}">
              <include name="WebDriver.dll" />
              <include name="WebDriver.Support.dll" />
            </fileset>
        </copy>
    </target>
    <target name="run" depends="build">
        <foreach item="File" property="filename">
            <in>
                <items>
                    <include name="${outdir}/*.exe" />
                </items>
            </in>
            <do>
                <echo message="${filename}" />
                <exec program="${path::combine(outdir,filename)}"  workingdir="${outdir}"/>
            </do>
        </foreach>
    </target>
    <target name="test" depends="build">
        <nunit2>
            <formatter type="Plain" />
            <test>
                <assemblies basedir="${outdir}">
                    <include name="*Test.dll" />
                </assemblies>
            </test>
        </nunit2>
    </target>
</project>
View Code

 

Step 3: Kick start

Just a Hello World in Selenium WebDriver C#. It opens the browser and search 'selenium' in Google, then return the page title.

using System;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;

namespace huys
{
    class Program
    {
        static void Main(string[] args)
        {
            // For Firefox
            //var driver = new FirefoxDriver();
            
            // For IE
            //var driver = new InternetExplorerDriver();

            // For chrome
            var options = new ChromeOptions();
            options.BinaryLocation = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
            var driver = new ChromeDriver("..\\lib", options);

            //Notice navigation is slightly different than the Java version
            //This is because 'get' is a keyword in C#
            driver.Navigate().GoToUrl("http://www.google.com/");

            // Find the text input element by its name
            IWebElement query = driver.FindElement(By.Name("q"));

            // Enter something to search for
            query.SendKeys("selenium");

            // Now submit the form. WebDriver will find the form for us from the element
            query.Submit();

            // Google's search is rendered dynamically with JavaScript.
            // Wait for the page to load, timeout after 10 seconds
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until((d) => { return d.Title.ToLower().StartsWith("selenium"); });

            // Should see: "Cheese - Google Search"
            System.Console.WriteLine("Page title is: " + driver.Title);

            //Close the browser
            driver.Quit();
        }
    }
}
View Code

 

 

* Problems with ChromeDriver

FireFoxDriver is perfect in selenium, but ChromeDriver isn't. You have to download chromedriver.exe for running ChromeDriver. If chrome wasn't installed under default location, the code definitely will fail.

The server expects you to have Chrome installed in the default location for each system:

OS Expected Location of Chrome
Linux /usr/bin/google-chrome1
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe

Unfortunately no chrome under default location on my laptop. To overcome this issue, some extra lines for locations.

1 // For chrome
2 var options = new ChromeOptions();
3 options.BinaryLocation = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; // Explicitly define the path to chrome.exe
4  var driver = new ChromeDriver("..\\lib", options); // Add the directory for chromedriver.exe

 

 

[1] http://code.google.com/p/selenium/wiki/ChromeDriver

[2] http://selenium.googlecode.com/git/docs/api/dotnet/index.html

[3] http://docs.seleniumhq.org/

posted @ 2013-11-16 11:39  独木  阅读(4961)  评论(0编辑  收藏  举报