您的位置:首页 > 编程语言 > C#

Web Automation with Selenium (C#)

2013-11-16 11:39 429 查看
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:

Selenium IDE

Selenium Remote Control

Selenium WebDriver

Selenium Grid

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.

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


View Code



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

OSExpected 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 VistaC:\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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: