我是靠谱客的博主 可靠枕头,最近开发中收集的这篇文章主要介绍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:

  • Selenium IDE

  • Selenium Remote Control

  • Selenium WebDriver

  • Selenium Grid

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:

  • AndroidDriver
  • ChromeDriver
  • EventFiringWebDriver
  • FirefoxDriver
  • HtmlUnitDriver
  • InternetExplorerDriver
  • IPhoneDriver
  • PhantomJSDriver
  • RemoteWebDriver
  • SafariDriver

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:

OSExpected 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 VistaC: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/

转载于:https://www.cnblogs.com/huys03/p/3426570.html

最后

以上就是可靠枕头为你收集整理的Web Automation with Selenium (C#)的全部内容,希望文章能够帮你解决Web Automation with Selenium (C#)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部