Skip to content

Instantly share code, notes, and snippets.

@bryanmikaelian
Created August 18, 2011 15:35
Show Gist options
  • Save bryanmikaelian/1154337 to your computer and use it in GitHub Desktop.
Save bryanmikaelian/1154337 to your computer and use it in GitHub Desktop.
example for Matthew S.
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using Gallio.Framework;
using MbUnit.Framework;
using Selenium;
namespace Test.Foundation {
[TestFixture]
public class TestFixture {
// Holds all the tests that are actively running.
private Dictionary<string, TestBase> _testContainer = new Dictionary<string, TestBase>();
protected Dictionary<string, TestBase> TestContainer {
get { return _testContainer; }
}
[SetUp]
public void SetUp() {
// Create a new test object. By aggregation, this object is responsible for all the technical components to make a test run. It is possible to create a test object outside of the set up.
TestBase test = new TestBase();
// Add this test object to the container of test objects. Each test that runs will have a test object.
_testContainer.Add(Gallio.Framework.TestContext.CurrentContext.Test.Name, test);
}
[TearDown]
public void TearDown() {
// If the test's selenium object is not null, attempt to stop the selenium instance for that test. Remove the test from the container since the test has finished.
if (_testContainer[Gallio.Framework.TestContext.CurrentContext.Test.Name].Selenium != null) {
try {
_testContainer[Gallio.Framework.TestContext.CurrentContext.Test.Name].Selenium.Stop();
_testContainer.Remove(Gallio.Framework.TestContext.CurrentContext.Test.Name);
}
catch (NullReferenceException) { }
}
}
}
/// <summary>
/// The test class is just a representation of what a given tests needs to function. This can be the selenium object, a DB connection, a generic method, etc. We don't need to inherit anything from this class, so it is sealed.
/// </summary>
public sealed class TestBase {
// Properties
private ISelenium _selenium;
private string _serverHost;
private string _browser;
private string _url;
private string _environment;
private int _defaultPort;
private string _connectionString;
// Accessors
public ISelenium Selenium {
get { return _selenium; }
}
public string ServerHost {
get { return _serverHost; }
}
public string Browser {
get { return _browser; }
}
public string URL {
get { return _url; }
}
public string Environment {
get { return _environment; }
}
// Default constructor. Creates a selenium instance and starts the server.
public TestBase() {
// Set the serverhost and browser
this._serverHost = "localhost";
this._browser = "*firefox";
// The default port for selenium is 4444.
this._defaultPort = 4444;
// Set the URL and connection string (Based on the environment)
this._environment = System.Configuration.ConfigurationManager.AppSettings["Environment"];
switch (this._environment) {
case "QA":
this._url = "";
this._connectionString = "";
break;
case "EFIX":
this._url = "";
this._connectionString = "";
break;
case "PROD":
this._url = "";
this._connectionString = "";
break;
default:
break;
}
// Create a new selenium object. By composition, we will have a selenium object for each test object. The selenium object itself doesn't do much outside a test.
this._selenium = new DefaultSelenium(this._serverHost, this._defaultPort, this._browser, this._url);
// Start the server and maximize the window.
//this._selenium.Start();
//this._selenium.WindowMaximize();
}
/// <summary>
/// Executes a given query against the database.
/// </summary>
/// <param name="query">The query to be executed.</param>
public DataTable executeQuery(string query) {
DataTable results;
using (SqlConnection dbConnection = new SqlConnection(this._connectionString)) {
dbConnection.Open();
using (SqlDataAdapter dbAdapter = new SqlDataAdapter(query, this._connectionString)) {
results = new DataTable();
dbAdapter.Fill(results);
}
dbConnection.Close();
}
return results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment