Selenium WebDriver in C-Sharp
Preparing environment
Download and install Oracle Java:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Download and install Visual Studio Community 2013:
http://www.visualstudio.com/downloads/download-visual-studio-vs
Download NUnit:
http://nunit.org/?p=download
Run NUnit-2.6.4.msi or install it later from Package Manager.
Run Visual Studio, select Development Settings: Visual C# and Light color theme. Create a new Project:
Templates > Visual C# > Class Library Name: SeleniumTests
Download and install NuGet Package Manager. Go to:
Tools > Extensions and Updates
Restart Visual Studio.
Open Package Manager Console.
NuGet Package Manager > Package Manager Console
Install required Selenium packages:
Install-Package Selenium.WebDriver Install-Package Selenium.Support Install-Package NUnit -Version 2.6.4 Install-Package NUnitTestAdapter
Now in Solution Explorer we should have References to WebDriver and WebDriver.Support libraries.
Install NUnit Test Adapter
Tools > Extensions and Updates > Online > Visual Studio Gallery > NUnit Test Adapter
Install Firefox and Selenium add-on.
Prepare and run a test
Record a test with Selenium IDE and export it:
File > Export Test Case As > C# / NUnit / WebDriver
Name it like this:
Wallet.cs
Load exported file in Visual Studio.
Project > Add existing Item
Alternatively, you can drag and drop file on to project name in Solution Explorer.
Run Test.
Test > Windows > Test Explorer
Use shortcut ctrl + r and then press a to run all tests.
Yo can open Output window:
View > Output
Data sets in Selenium WebDriver
Prepare TestCase with two parameters like user and password:
[TestCase("demo", "demo")] [TestCase("test", "demo")] public void TheWalletTest(string user, string password) { driver.Navigate().GoToUrl(baseURL + "/wallet/"); driver.FindElement(By.Name("user")).Clear(); driver.FindElement(By.Name("user")).SendKeys(user); driver.FindElement(By.Name("password")).Clear(); driver.FindElement(By.Name("password")).SendKeys(password); driver.FindElement(By.Id("log-in")).Click(); Assert.IsTrue(IsElementPresent(By.Id("log-out"))); driver.FindElement(By.Id("log-out")).Click(); }
Annotations:
[TestFixture] - This marks a class that contains tests and, optionally, setup or teardown methods [SetUp] - Is used to provide a set of functions that are performed just before each test method is called [Test] - This marks a method inside a TestFixture class as a test [TestCase] - This serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method [TearDown] - Is used to provide a set of functions that are performed after each test method is run
Run tests on multiple browsers
Change driver from:
driver = new FirefoxDriver();
to:
driver = new ChromeDriver();
Download Chrome Driver:
http://chromedriver.storage.googleapis.com/2.12/chromedriver_win32.zip
Specify path to Chrome Driver executable file:
driver = new ChromeDriver(@"C:\Selenium");
Chrome Driver can be also installed using Package Manager Console:
Install-Package WebDriver.ChromeDriver.win32 Install-Package WebDriver.IEDriverServer.win32
Run tests.
Getting Internet Explorer to work
Change security settings for Internet Explorer. Go to:
Internet Options > Security
Enable Protected Mode for all zones.
Depending on situation, in Trusted sites zone click on Sites, add Web application server address e.g. http://192.168.0.108 and uncheck Require server verification box.
Parameters in Selenium WebDriver
Prepare TestFixture with browser names as parameters.
Create default constructor for TestFixture class.
Create if statement to assign different drivers.
namespace SeleniumTests { [TestFixture("firefox")] [TestFixture("chrome")] [TestFixture("internet explorer")] public class Wallet { private IWebDriver driver; private StringBuilder verificationErrors; private string baseURL; private bool acceptNextAlert = true; private string browser; public Wallet(string browser) { this.browser = browser; } [SetUp] public void SetupTest() { if (browser == "chrome") { driver = new ChromeDriver(); } else if (browser == "internet explorer") { driver = new InternetExplorerDriver(); } else { driver = new FirefoxDriver(); } baseURL = "http://192.168.0.108"; verificationErrors = new StringBuilder(); }
Other option is to create a switch statement:
switch (browser) { case "chrome": driver = new ChromeDriver(); break; case "internet explorer": driver = new InternetExplorerDriver(); break; default: driver = new FirefoxDriver(); break; }
Remote WebDriver
Download Selenium Server Standalone and run it from command line. Configure all necessary drivers:
java -jar selenium-server-standalone-2.44.0.jar -Dwebdriver.chrome.driver=chromedriver.exe -Dwebdriver.ie.driver=IEDriverServer.exe
Change the code:
DesiredCapabilities capabilities = new DesiredCapabilities(); switch (browser) { case "chrome": capabilities = DesiredCapabilities.Chrome(); break; case "internet explorer": capabilities = DesiredCapabilities.InternetExplorer(); break; default: capabilities = DesiredCapabilities.Firefox(); break; } capabilities.SetCapability(CapabilityType.BrowserName, browser); capabilities.SetCapability(CapabilityType.Platform, os); driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
Add second parameter to TestFixture :
[TestFixture("firefox", "WINDOWS")] [TestFixture("chrome", "WINDOWS")] [TestFixture("internet explorer", "WINDOWS")]
Add missing variables:
private string browser; private string os; public Wallet(string browser, string os) { this.browser = browser; this.os = os; }
Run all tests again.
Selenium Grid
Start Selenium Server as a Hub:
java -jar selenium-server-standalone-2.44.0.jar -role hub
The console is available at:
http://localhost:4444/grid/console
Prepare configuration file for Node.
Register the Node with network address e.g. 192.168.0.50 to Selenium Hub with specific network address e.g. 192.168.0.10:
java -jar selenium-server-standalone-2.44.0.jar -role node -nodeConfig Windows_7.json -Dwebdriver.chrome.driver=chromedriver.exe -Dwebdriver.ie.driver=IEDriverServer.exe
Change the code:
[TestFixture("firefox", "WINDOWS")] [TestFixture("chrome", "WINDOWS")] [TestFixture("internet explorer", "WINDOWS")] [TestFixture("firefox", "LINUX")] [TestFixture("chrome", "LINUX")] public class Wallet { private IWebDriver driver; private StringBuilder verificationErrors; private string baseURL; private bool acceptNextAlert = true; private string browser; private string os; public Wallet(string browser, string os) { this.browser = browser; this.os = os; } [SetUp]
Run all tests again.
Debugging
You can debug your tests:
System.Console.WriteLine("Page title is: " + driver.Title); System.Console.WriteLine("Page address is: " + driver.Url);