zoukankan      html  css  js  c++  java
  • 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 SettingsApplication DataGoogleChromeApplicationchrome.exe
    Windows Vista C:Users\%USERNAME%AppDataLocalGoogleChromeApplicationchrome.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/

  • 相关阅读:
    C# 使用Dev控件为gridView添加复选框,实现多选和单选
    C# 关于LINQ基础随笔
    Camstar客制化开发做查询操作(Designer中存放SQL语句)
    ASP.NET 实现自动登录(自定义自动登录时间期限)
    ASP.NET 页面生命周期-动态控件编程(动态添加控件并使用动态控件值)
    Camstar客制化开发查询数据库数据并显示在JQDataGrid表中(SQL语句编写在UserQuery中)同时获取JQDataGrid中值回传到Designer中
    Camstar客制化开发查询数据库数据并显示在JQDataGrid表中(SQL语句编写在代码中)
    WPF开发中常用的几种布局元素
    Cocos2dx 内存管理机制(1)
    cocos2dx 坐标系统详解
  • 原文地址:https://www.cnblogs.com/huys03/p/3426570.html
Copyright © 2011-2022 走看看