Let me give a brief introduction to selenium at first:
Selenium is a set of tools that supports rapid development of test automation for web-based applications. Two major tools will be used here: Selenium-IDE (Integrated Development Environment): A Firefox add-on, which has a recording feature and will keep account of user actions and store them as a reusable script to play back. Selenium-RC (Remote Control): This allows using high-level programming language (HTML, Java, C#, Perl, PHP, Python, and Ruby) to develop test cases and allows automated testing to be integrated with a project’s automated build environment.
Now I will introduce how to do web application automation test in VS by selenium:
1. Some tools to install:
a) As selenium-IDE is an only Firefox add-on, Firefox is required to install at first.
b) Download and install Selenium-IDE, download Selenium-RC and unzip it to local disk from http://seleniumhq.org/download.
c) Java is required to install as it is used to start Selenium-RC server.
2. Open Firefox, start Selenium-IDE (Tools -> Selenium IDE) to record test case.
3. Export test case generated by Selenium-IDE to C# code(baidu.cs):
public class baidu
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheBaiduTest()
{
selenium.Open("/");
selenium.Type("kw", "google");
selenium.Click("su");
selenium.WaitForPageToLoad("30000");
selenium.Click("//table[@id='1']/tbody/tr/td/a/font");
}
}
4. Create a test project in VSTT.
5. Copy .dll files from unzipped Selenium-RC folder “~\selenium-remote-control-1.0.1\selenium-dotnet-client-driver-1.0.1” to your test project’s \Bin\Debug folder and add reference.
6. Add a new unit test file, copy the c# code generated by Selenium-IDE to the unit test file, and change the attribute of methods to that supported by VSTT:
7. Before you run your test, you should start selenium-server at first,
start java -jar selenium-server.jar -forcedbrowsermode *firefox(*googlechrome, *iexplore, *opera, *safari, ...)
8. Run automation test cases:
mstest /runconfig:%TestProjPath%\TestRunConfig.testrunconfig /testmetadata:%TestProjPath%\[testname].vsmdi /testlist:%TestToRun% /resultsfile:%TestProjPath%\TestResults\[resultname].trx, %TestToRun% is the test list you select to run in [testname].vsmdi.