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. BZOJ2753 SCOI2012滑雪与时间胶囊(最小生成树)

    首先显然可以把所有能到的点拎出来建个新图,这样第一问也就做好了. 剩下的部分似乎是一个裸的最小树形图.但显然这个东西是没什么学的必要的并且不太能跑过去. 考虑建出来的图有什么性质.可以发现如果没有高度 ...

  2. CF933A A Twisty Movement

    题意翻译 给定一个序列 A,你可以翻转其中的一个区间内的数,求翻转后的序列的最长不下降子序列的长度.(∣A∣≤2000,1≤ai≤2|A|\le 2000,1\le a_i \le 2∣A∣≤2000 ...

  3. 字符编码问题mysql

    2019-02-27 07:32:17.108 ERROR 21745 --- [nio-8086-exec-2] c.h.h.rest.configurer.WebMvcConfigurer : 接 ...

  4. 【bzoj3196】 Tyvj1730—二逼平衡树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3196 (题目链接) 题意 1.查询k在区间内的排名:2.查询区间内排名为k的值:3.修改某一位值上的 ...

  5. Linux上常用的基本命令

    复制:copy [keysystem@localhost happydzy]$ cp file1 file2 [keysystem@localhost happydzy]$ ll total -rw- ...

  6. phpstorm 配置 webpack @ 别名跳转

    webstorm中专门有webpack的相关配置,默认的路径直接是项目根目录下的 webpack.config.js,但是我们用各种cli生成的项目中,webpack的配置一般都是在build下,导致 ...

  7. 【CSS】定位层

    html:定位层1.概念: >>.定位层是由html元素(标签)形成的一个特殊的box盒子. >>.其重点在于“定位”,而html元素(标签)的定位方式由CSS来控制. 通常情 ...

  8. docker mysql authentication_string client does not support authentication 连接问题

    docker安装mysql后,本地navicat连接报错client does not support authentication 解决办法: 1. docker ps -a 查找到容器id 2.  ...

  9. Codeforces 338 D. GCD Table

    http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...

  10. 【原创】backbone1.1.0源码解析之Model

    趁热打铁,将Backbone.Model的源代码注释也发出来. Model是用来干嘛的?写过mvc的同学应该都知道,说白了就是model实例用来存储数据表中的一行数据(row) Backbone利用m ...