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:
- 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>
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#)的更多相关文章
- 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础
第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1 初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...
- 《零成本实现Web自动化测试--基于Selenium》 第四章 Selenium 命令
Selenium 命令,通常被称为Selenese,由一系列运行测试案例所需要的命令构成.按顺序排列这些命令就构成了测试脚本. 一. 验证颜面元素 1.Assertion或者Verification ...
- 《零成本实现Web自动化测试--基于Selenium》第二章 Selenium简介和基础
第一部分 Selenium简介 1.Selenium 组建 1.1 Selenium-IDE Selenium-IDC是开发Selenium测试案例的集成开发环境.它像FireFox插件一样的工作,支 ...
- python自动化测试应用-第6篇(WEB测试)--Selenium元素篇
篇6 python自动化测试应用-Selenium基础篇 --lamecho 1.1概要 大家好!我是lamecho(辣么丑),上一篇我们搭建好p ...
- WEB自动化(Python+selenium)的API
在做Web自动化过程中,汇总了Python+selenium的API相关方法,给公司里的同事做了第二次培训,分享给大家 ...
- 开源Web自动化测试工具Selenium IDE
Selenium IDE(也有简写SIDE的)是一款开源的Web自动化测试工具,它实现了测试用例的录制与回放. Selenium IDE目前版本为 3.6 系列,支持跨浏览器运行,所以IDE的UI从原 ...
- python3 web测试模块selenium
selenium是一个用于web应用程序测试工具,selenium测试直接运行在浏览器中,就像真正的用户在操作一样,支持的浏览器包括IE(7,8,9,10,11),mozilla firefox,sa ...
- .NET项目web自动化测试实战——Selenium 2.0
PS:这次用公司的项目来练手,希望公司不会起诉我,因为我绝对是抱着学习的态度,没有任何恶意.仅供交流学习. 该项目是基于SharePoint平台所开发的门户网站,为了切身感受一下Selenium 2. ...
- web automation 常用技术比较
selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)驱动真实浏览器完成测试. 除此之外, ...
随机推荐
- maven上传jar包到nexus私服后的存放路径 以及 使用IDEA上传jar包的步骤
maven上传jar包到nexus私服的方法,网上大神详解很多,那么上传后的jar包存放到哪里了呢? 在下使用nexus3.2.1版本,在本地搭建了私服,使用maven上传jar包.最后结果如下: 点 ...
- VMXNET3 vs E1000E and E1000
VMXNET3 vs E1000E and E1000 用户为什么要从E1000调整为VMXNET3,理由如下: E1000是千兆网路卡,而VMXNET3是万兆网路卡: E1000的性能相对较低,而V ...
- mysql账户添加远程访问
我们要将root账户设置为远程可访问 mysql> show databases; +--------------------+ | Database | +------------------ ...
- 致研究者:2018 AI 研究趋势
2017 年是机器学习领域最有成效.最具创意的一年.现在已经有很多博文以及官方报道总结了学界和业界的重大突破.本文略有不同,Alex Honchar在Medium发文,从研究者的角度分享机器学习明年发 ...
- 【树状数组套主席树】带修改区间K大数
P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...
- C# 基于MySQL的数据层基类(MySQLHelper)
这里介绍下比较简单的方式,引用MySql.Data.dll然后添加一个MySqlHelper类来对MySql数据库进行访问和操作. 1.将MySql.Data.dll引用到你的项目中 下载地址:MyS ...
- php错误日志
php错误日志 /usr/local/php/var/log/php-fpm.log」—————————
- python的内置模块re模块方法详解以及使用
正则表达式 一.普通字符 . 通配符一个.只匹配一个字符 匹配任意除换行符"\n"外的字符(在DOTALL模式中也能匹配换行符 >>> import re ...
- bzoj千题计划195:bzoj2844: albus就是要第一个出场
http://www.lydsy.com/JudgeOnline/problem.php?id=2844 题意:给定 n个数,把它的所有子集(可以为空)的异或值从小到大排序得到序列 B,请问 Q 在 ...
- spring框架学习(四)AOP思想
什么是AOP 为什么需要AOP 从Spring的角度看,AOP最大的用途就在于提供了事务管理的能力.事务管理就是一个关注点,你的正事就是去访问数据库,而你不想管事务(太烦),所以,Spring在你访问 ...