ASP.Net Web Application Integration testing using Selenium 2.0
Selenium primarily used for automating web applications for testing purposes. You can create browser based regression tests using Selenium WebDriver.
Selenium WebDriver provides a concise programming interface. It drives the browser much more effectively. Selenium WebDriver makes direct calls to browser using each browser's native support for automation.
This articles deals with running tests against the application all running on the same machine. WebDriver will run the browser directly. *This article doesn't cover test distribution over multiple machine*. The article covers running tests for IE browser.
Setup:
To create and run Selenium tests using IE or chrome browser, we need to set up and reference few things before we hit the ground running.
Note: Following instructions are specific to IE browser.
DriverServer Setup:
1) Based on your computer architecture, please download 32bit or 64 bit IEDriverServer from here.
2) UnZip the contents of download and you can run the Driver in the two ways
a) Designate Driver.exe as part of your Test project test settings as deployable item so it copied to test project output every time you run the tests
b) Instead, set the IEDriverServer.Exe path as part of PATH environment variable.
3) The point is IEDriverServer should be running when executing tests using selenium
Assembly References in VS2012:
1) Please download the latest version of Selenium-dotnet-(version).zip which includes .Net bindings and WebDriver programming API from here
2) UnZip the contents of download and reference the "WebDriver.dll" based on your target .net Framework to your testing project as shown below.
3) You're all set now
Running the tests:
Following are the simple tests designed for a pseudo website with two pages. First test automates the login function and second test automates user details on the home page.
When you run the tests, you'll see IEDriverServer console running up and your sample site being run by Selenium in the web browser as shown below:
second test results
Code Sample:
Code Sample:
using System;
using System.ComponentModel;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace SeleniumAutomatedTests
{
[TestClass]
public class SeleniumUnitTests
{
private static InternetExplorerDriver _webDriver;
[TestMethod]
public void TestSiteLoginFunction()
{
//initialize internet explorer driver and you can set up options for driver
_webDriver = new InternetExplorerDriver(new InternetExplorerOptions()
{
InitialBrowserUrl = "http://localhost/TestSite/Login.aspx",
});
//find the desired elements by id, css class or name, xpath
_webDriver.FindElement((By.Id("userNameTxtBox"))).SendKeys("Selenium");
_webDriver.FindElement(By.Id("passwordTxtBox")).SendKeys("selenium");
var logInButton = _webDriver.FindElement(By.Id("signInBtn"));
//simulating the button click
logInButton.Click();
//wait for 5 seconds for the page to load
_webDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));
var welcomeLabel = _webDriver.FindElement(By.Id("WelcomeLabel"));
//interface to execute javascript code on the host page
var js = (IJavaScriptExecutor) _webDriver;
//you can also execute on fly javascript code or predefined javascript functions attached to host page.
js.ExecuteScript("alert('you successfully signed in.')");
//finally, some asserting
Assert.IsNotNull(welcomeLabel.Text);
}
[TestMethod]
public void TestHomePageSubmitFunction()
{
_webDriver = new InternetExplorerDriver();
_webDriver.Navigate().GoToUrl("http://localhost/TestSite/Home.aspx");
_webDriver.FindElement((By.Id("ageTxtBox"))).SendKeys("29");
_webDriver.FindElement(By.Id("cityTxtBox")).SendKeys("Los Angeles");
//submitting the form using selenium
var form = _webDriver.FindElement(By.Id("detailsForm"));
form.Submit();
_webDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));
var welcomeLabel = _webDriver.FindElement(By.Id("WelcomeLabel"));
Assert.IsTrue(welcomeLabel.Text.Contains("Los Angeles") && welcomeLabel.Text.Contains("29"));
}
}
}
Hope this helps..