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)驱动真实浏览器完成测试. 除此之外, ...
随机推荐
- js数组去重五种方法
今天来聊一聊JS数组去重的一些方法,包括一些网上看到的和自己总结的,总共5种方法(ES5). 第一种:遍历数组法 这种方法最简单最直观,也最容易理解,代码如下: var arr = [2, 8, 5, ...
- java 调用 C# webapi
最近项目需要 java调用 .net 的webapi. 对于get来说很简单,但是post方法遇到了些问题,最后也是曲线救国. 先看代码 Java 代码 public static void ma ...
- MT【83】三个等号
分析:此类三个等式的一般做法先记为$t$,则有如下做法:
- MT【94】由参数前系数凑配系数题1
评:根据$b,c$前系数凑配系数,也是比较常见的思路.
- 获取androdmanifest里面的meta-data
/* * Copyright 2017 JessYan * * Licensed under the Apache License, Version 2.0 (the "License&qu ...
- AtCoder Grand Contest 006
AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...
- BZOJ 2648 / 2716 K-D Tree 模板题
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- 【转】在windows中使用Intellij Idea时选择自定义的64位JVM
原文地址:https://www.iflym.com/index.php/code/201404190001.html 本文英文原文自:https://intellij-support.jetbrai ...
- PyQt4 安装
安装PyQt4很简单,从官网下载相应的安装包即可. 需要注意的是:应该根据你电脑上已经装好的python版本选择相应的PyQt4安装包. PyQt4的安装目录一定要选python的安装目录,比如我的P ...
- box-sizing border-box 的理解
http://blog.csdn.net/isaisai/article/details/20449827 -webkit-box-sizing: border-box; 则div 设置的宽高将包含 ...