Event Definition helps us to run the test scripts or
scenarios in the definite manner and pattern. I have run the
scenarios as feature basis, when one feature completes then WebDriver stops and
re-initiate when next feature starts whatever the number of scenarios it
contain. This helps us to save the execution time and get the maximum
throughput for our test suite. Below is the structure of event definition file,
[Binding]
public class BaseTestScript
{
public static IWebDriver
WebDriver = null;
public static ISelenium
BackedSelenium= null;
public static SqlConnection
DatConnection = DatabaseTools.GetSqlConnection();
static readonly string
Browser = ConfigurationManager.AppSettings["browser"];
// Purpose:
To Start WebDriver
public static void
WebDriverStart()
{
try
{
WebDriver.Navigate().GoToUrl(ConfigurationManager.AppSettings["browserUrl"]);
Thread.Sleep(3000);
WebDriver.FindElement(By.Id("ImgPoweredBy"));
WebDriver.Manage().Window.Maximize();
Thread.SetData(Thread.GetNamedDataSlot("webdriverInstance"),
WebDriver);
Thread.SetData(Thread.GetNamedDataSlot("backedseleniumInstance"),
BackedSelenium);
}
catch
(Exception e)
{
if
(Browser != null) throw
new TypeLoadException(Browser+" cannot display the webpage, Please Try Again");
}
Console.WriteLine("WebDriver Started");
}
// Purpose:
To Stop WebDriver
public static void
WebDriverStop()
{
if
(WebDriver == null)
return;
try
{
WebDriver.Manage().Cookies.DeleteAllCookies();
WebDriver.Dispose();
if
(Browser.Equals("IE"))
{
Process[]
iexpPro = Process.GetProcessesByName("iexplore");
if
(iexpPro.Length > 0)
{
foreach (Process clsProcess in iexpPro)
{
clsProcess.Kill();
}
}
}
if
(Browser.Equals("Firefox"))
{
Process[]
fireFoxPro = Process.GetProcessesByName("firefox");
if
(fireFoxPro.Length > 0)
{
foreach (Process clsProcess in fireFoxPro)
{
clsProcess.Kill();
}
}
}
if
(Browser.Equals("Safari"))
{
Process[]
safariPro = Process.GetProcessesByName("safari");
if
(safariPro.Length > 0)
{
foreach (Process clsProcess in safariPro)
{
clsProcess.Kill();
}
}
}
Process[]
selcomwin = Process.GetProcessesByName("mshta");
if
(selcomwin.Length > 0)
{
foreach
(Process clsProcess in
selcomwin)
{
clsProcess.Kill();
}
}
Process[]
ieDriverserver = Process.GetProcessesByName("IEDriverServer");
if
(ieDriverserver.Length > 0)
{
foreach
(Process clsProcess in
ieDriverserver)
{
clsProcess.Kill();
}
}
}
catch
(Exception e)
{
//throw
new Exception("Unable To Stop WebDriver: Please Try Again.");
}
WebDriver = null;
Console.WriteLine("WebDriver Stopped");
}
[BeforeTestRun]
// Purpose:
To Initialize TestScript
public static void
TestInitialize()
{
try
{
//Purpose:
Opening DB Connection
DatConnection.Open();
}
catch
(Exception e)
{
throw
new Exception("Unable To Open Database Connection: Please Try
Again.");
}
//Purpose
: Call Method To Select Browser
BrowserSetup();
//Purpose
: Call Method To Start WebDriver
WebDriverStart();
}
[AfterTestRun]
// Purpose:
To Dispose TestScript
public static void
TearDown()
{
try
{
//Purpose:
Closing DB Connection
DatConnection.Close();
}
catch
(Exception e)
{
throw
new Exception("Unable To Close WebDriver: Please Try Again.");
}
WebDriverStop();
Thread.SetData(Thread.GetNamedDataSlot("webdriverInstance"),
WebDriver);
Thread.SetData(Thread.GetNamedDataSlot("backedseleniumInstance"),
BackedSelenium);
}
//Purpose:
Selecting Browser To Run Test
public static void
BrowserSetup()
{
if
(Browser.Equals("IE"))
{
string
ieDriverPath = ConfigurationManager.AppSettings["ieDriverPath"];
var
options = new InternetExplorerOptions
{ IntroduceInstabilityByIgnoringProtectedModeSettings = true };
WebDriver = new InternetExplorerDriver(ieDriverPath,
options);
BackedSelenium = new WebDriverBackedSelenium(WebDriver,
ConfigurationManager.AppSettings["browserUrl"]);
BackedSelenium.Start();
}
if
(Browser.Equals("Firefox"))
{
WebDriver = new FirefoxDriver();
BackedSelenium = new WebDriverBackedSelenium(WebDriver,
ConfigurationManager.AppSettings["browserUrl"]);
BackedSelenium.Start();
}
if
(Browser.Equals("Safari"))
{
WebDriver = new SafariDriver();
BackedSelenium = new WebDriverBackedSelenium(WebDriver,
ConfigurationManager.AppSettings["browserUrl"]);
BackedSelenium.Start();
}
}
}