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)驱动真实浏览器完成测试. 除此之外, ...
随机推荐
- 深入理解Vue的生命周期
谈到Vue的生命周期,相信许多人并不陌生.但大部分人和我一样,只是听过而已,具体用在哪,怎么用,却不知道.我在学习vue一个多礼拜后,感觉现在还停留在初级阶段,对于mounted这个挂载还不是很清楚. ...
- 初识elasticsearch_2(查询和整合springboot)
初始化 首先将官网所下载的json文件,放入到es中,采用如下命令: curl -H "Content-Type: application/json" -XPOST 'localh ...
- 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...
- POJ 1182 食物链 (拆点并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78601 Accepted: 23422 Description ...
- MT【170】裂项相消
已知$a,b>0$证明:$\dfrac{1}{a+2b}+\dfrac{1}{a+4b}+\dfrac{1}{a+6b}<\dfrac{3}{\sqrt{(a+b)(a+7b)}}$ 证明 ...
- 【题解】 bzoj1088: [SCOI2005]扫雷Mine (神奇的做法)
bzoj1088,懒得复制,戳我戳我 Solution: 其实这个有个结论,答案只会有\(0\),\(1\),\(2\)三种(我真的是个弱鸡,这个都想不到) 然后我们假设第一个就可以推出所有的状态(显 ...
- easyui form 提交问题,纠结了很久,有点诡异
http://www.iteye.com/problems/131602 form 提交,后台运行有时慢,页面就不等后台数据的响应,直接alert("服务器维护中,请稍后再试!") ...
- TensorFlow分布式实践
大数据时代,基于单机的建模很难满足企业不断增长的数据量级的需求,开发者需要使用分布式的开发方式,在集群上进行建模.而单机和分布式的开发代码有一定的区别,本文就将为开发者们介绍,基于TensorFlow ...
- Python学习笔记 - 实现探测Web服务质量
#!/usr/bin/python3# _*_ coding:utf-8 _*_import sys, osimport timeimport pycurl url = "https://d ...
- python的内置模块之os模块方法详解以及使用
1.getcwd() 获取当前工作路径 import os print(os.getcwd()) C:\python35\python3.exe D:/pyproject/day21模块/os模块.p ...