Here is an example of Google search and the results are saved as a HTML file:
Code for GoogleSearch.cs file:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
using System.Diagnostics;
namespace SeleniumTests
{
[TestFixture]
public class GoogleSearch
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.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 GoogleSearchTest()
{
int stepsPassed = 0;
int stepsFailed = 0;
string TestCase = "Google Search";
Results.writeTCHeading(TestCase);
selenium.Open("/");
selenium.Type("q", "seleniumwiki");
selenium.Click("btnG");
selenium.WaitForPageToLoad("30000");
try
{
Assert.IsTrue(selenium.IsTextPresent("Selenium Wiki"));
Results.passMSG("The text Selenium Wiki is present on the search results page"); //pass message
stepsPassed++;
}
catch (AssertionException e)
{
verificationErrors.Append(e.Message);
Results.failMSG("The text Selenium Wiki is not present on the search results page"); //fail message
stepsFailed++;
}
//write report for the total number of steps passed and failed.
int totalsteps = stepsPassed + stepsFailed;
if (stepsFailed > 0)
{
Results.writereport(TestCase + "test case failed - " + stepsFailed + " of " + totalsteps + " step(s) FAILED.");
}
else
{
Results.writereport(TestCase + "test case passed - " + stepsPassed + " step(s) PASSED.");
}
Using selenium you can capture the screen shot of the pages when running the selenium script. This is useful when you want to capture the screen shot of the page where you have a failure/ error.
selenium.CaptureEntirePageScreenshot("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\test.png", "");
In this article I will explain you how you can install and run your Selenium C# scripts on Selenium Grid using MbUnit and Gallio
1. Download Gallio runner
2. Unzip the files to C:\GallioBundle-3.2.603.0
3. Add reference to Mbunit and Gallio in your selenium framework

4. Add MbUnit.Framework namespace to the test script.

This is a plugin for Selenium IDE that adds WebDriver backed Selenium formatters, which allows users to take advantage of WebDriver without having to modify their tests to use the new API.
I am using Selenium RC in Visual Studio 2010 – Test Project framework. When I try to run the scripts from the share drive I get the following error “Unit Test Adapter threw exception: URI formats are not supported”
The solve the problem please follow the steps:
Double click on Local.testsettings which is under the Solution Items of Project
Test Settings window is displayed. Click on the Deployment link.
You will see a checkbox Enable Deployment. Select the checkbox and click Apply.
The Test Settings can also be found under TraceAndTestImpact.testsettings and just follow the same steps.
Now run your scripts.
Here is the code for the connection to a mysql Database using Microsoft SQL Server JDBC driver.
public void DBtest() throws Exception
{
// Load Microsoft SQL Server JDBC driver.
Class.forName(“com.mysql.jdbc.Driver”);
// Prepare connection url.
String url = “jdbc:mysql://localhost/TEST_DB”;
// Get connection to DB.
Connection con = DriverManager.getConnection(url, “root”, “admin”);
// Create statement object which would be used in writing DDL and DML
// SQL statement.
Statement stmt = con.createStatement();
// Send SQL SELECT statements to the database via the
Statement.executeQuery
// method which returns the requested information as rows of data in a
// ResultSet object.
ResultSet result = stmt.executeQuery(“select email from user”);
if (result.next()){
// Fetch value of “email_address” from “result” object.
String emailaddress = result.getString(“email”);
// Use the fetched value to login to application.
selenium.open(“/”);
selenium.windowMaximize();
selenium.type(“login_email”, emailaddress);
}
}