Web Automation Selenium

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:

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:

We will use FirefoxDriver, ChromeDriver and InternetExplorerDriver in C# here.

Step 1: Download selenium-dotnet-2.37.0.zip


Step 2: Setup the environment

Create an directory for selenium files . Then extract selenium-dotnet-2.37.0.zip to /lib.

NAnt script for building:

View Code

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.

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:

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.

1 // For chrome
2 var options = new ChromeOptions();
3 options.BinaryLocation = “C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe”; // Explicitly define the path to chrome.exe
4 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/

分类: C#,Firefox,HTML5,Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *