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.

Web Automation with Selenium (C#)

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

相关文章: