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:
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.
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:
Unfortunately no chrome under default location on my laptop. To overcome this issue, some extra lines for locations.
[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/
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:
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 |
// 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/
相关文章推荐
- [Selenium With C#基础教程] Lesson-02 Web元素定位
- Webdriver(selenium2.0)+NUnit+C# (二)
- How to Locate Web Elements with Selenium WebDriver?
- 自动化测试:Selenium webdriver 学习笔记-C#版(四)
- Automating web tests with FitNesse and Selenium
- 25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation
- [Selenium With C#基础教程] Lesson-06 单选按钮
- Highlight elements with Selenium WebDriver
- [Selenium With C#基础教程] Lesson-06 单选按钮
- [Selenium With C#基础教程] Lesson-07 复选框
- C#: Create a WebRequest with HTTP Basic Authentication
- C#: Create a WebRequest with HTTPClient
- Make Yahoo! Web Service REST Calls With C#
- Writing ASP.NET Web Forms with C#
- [Selenium With C#基础教程] Lesson-06 单选按钮
- webdriver selenium org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with
- An Introduction to Testing Web Applications with twill and Selenium
- TestNG – Test Automation with Selenium
- Microsoft Web Browser Automation using C#
- C# WebAPI with HttpWebRequest, HttpWebResponse and HttpContext Example