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>

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());
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();
}
}
}

* 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.

 // For chrome
var options = new ChromeOptions();
options.BinaryLocation = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; // Explicitly define the path to chrome.exe
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/

Web Automation with Selenium (C#)的更多相关文章

  1. 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础

    第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1    初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...

  2. 《零成本实现Web自动化测试--基于Selenium》 第四章 Selenium 命令

    Selenium 命令,通常被称为Selenese,由一系列运行测试案例所需要的命令构成.按顺序排列这些命令就构成了测试脚本. 一. 验证颜面元素 1.Assertion或者Verification ...

  3. 《零成本实现Web自动化测试--基于Selenium》第二章 Selenium简介和基础

    第一部分 Selenium简介 1.Selenium 组建 1.1 Selenium-IDE Selenium-IDC是开发Selenium测试案例的集成开发环境.它像FireFox插件一样的工作,支 ...

  4. python自动化测试应用-第6篇(WEB测试)--Selenium元素篇

    篇6                            python自动化测试应用-Selenium基础篇 --lamecho 1.1概要 大家好!我是lamecho(辣么丑),上一篇我们搭建好p ...

  5. WEB自动化(Python+selenium)的API

    在做Web自动化过程中,汇总了Python+selenium的API相关方法,给公司里的同事做了第二次培训,分享给大家                                         ...

  6. 开源Web自动化测试工具Selenium IDE

    Selenium IDE(也有简写SIDE的)是一款开源的Web自动化测试工具,它实现了测试用例的录制与回放. Selenium IDE目前版本为 3.6 系列,支持跨浏览器运行,所以IDE的UI从原 ...

  7. python3 web测试模块selenium

    selenium是一个用于web应用程序测试工具,selenium测试直接运行在浏览器中,就像真正的用户在操作一样,支持的浏览器包括IE(7,8,9,10,11),mozilla firefox,sa ...

  8. .NET项目web自动化测试实战——Selenium 2.0

    PS:这次用公司的项目来练手,希望公司不会起诉我,因为我绝对是抱着学习的态度,没有任何恶意.仅供交流学习. 该项目是基于SharePoint平台所开发的门户网站,为了切身感受一下Selenium 2. ...

  9. web automation 常用技术比较

    selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)驱动真实浏览器完成测试. 除此之外, ...

随机推荐

  1. EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json

    1.在使用EF时,由于数据库主外键关联,将对象进行Json序列化时会遇到循环引用的问题 //EF 中由于数据库主外键关联,对象的序列化经常出现循环引用问题 //使用.Net 自带的序列化工具,序列化出 ...

  2. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

  3. 【BZOJ1025】[SCOI2009]游戏(动态规划)

    [BZOJ1025][SCOI2009]游戏(动态规划) 题面 BZOJ 洛谷 题解 显然就是一个个的置换,那么所谓的行数就是所有循环的大小的\(lcm+1\). 问题等价于把\(n\)拆分成若干个数 ...

  4. USACO Section 1.4 Mother's Milk 解题报告

    题目 题目描述 有三个牛奶桶,三个桶的容积分别是A,B,C,最小为1,最大为20.刚开始只有第三个桶里面装满了牛奶,其余两个桶都是空的.我们现在可以将第三个桶中的牛奶往其他两个桶里面倒一些牛奶,然后还 ...

  5. Oracle数据库--PL/SQL存储过程和函数的建立和调用

    1.存储过程建立的格式: create or replace procedure My_Procedure is begin --执行部分(函数内容); end; / 例子:(以hr表为例) crea ...

  6. yolo-v2只识别person

    一.修改源代码 (1)修改cfg/voc.data classess=20    改成 classes = 1 (2)修改data/voc.names 只留下person这一类 (3)修改exampl ...

  7. UiAutomator2.0入门

    总是听说UiAutomator这个框架,但从来没有使用过.找了篇入门,实践一下.实践之后感觉,uiautomator写测试代码,还是有点费劲.接口名比较多,比较长.网易的atx里使用的uiautoma ...

  8. SpringJMS解析--使用示例

    Spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  9. HDU 5299 圆扫描线 + 树上删边

    几何+博弈的简单组合技 给出n个圆,有包含关系,以这个关系做游戏,每次操作可以选择把一个圆及它内部的圆全部删除,不能操作者输. 圆的包含关系显然可以看做是树型结构,所以也就是树上删边的游戏. 而找圆的 ...

  10. My latest news

    2018.04.12  0:01 本站点停止更新,启用0x7c00.vip站点. 2018.03.23 复试报道(心态不太平稳).每一行的深入都是需要知识的积累和时间的沉淀,就像学法律.计算机等等.愿 ...