Skip to content

Instantly share code, notes, and snippets.

@VasylBakalets
Created October 5, 2015 13:16
Show Gist options
  • Save VasylBakalets/013ccfc3e0a4558f99e6 to your computer and use it in GitHub Desktop.
Save VasylBakalets/013ccfc3e0a4558f99e6 to your computer and use it in GitHub Desktop.
public enum Browser
{
Chrome,
Explorer,
Firefox
}
public class BaseTestSuite
{
public BaseTestSuite(Browser selectedBrowser)
{
Mouse.DefaultMoveTime = 100;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
CurrentBrowser = selectedBrowser;
}
public BaseTestSuite()
: this(Browser.Explorer)
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
}
public TestContext TestContext
{
get { return _testContextInstance; }
set { _testContextInstance = value; }
}
[ClassInitialize]
public static void ClassSetup(TestContext a)
{
ReportingHelper.SetupReport(a);
KillBrowser();
}
[ClassCleanup]
public static void ClassClean()
{
Report.End();
KillBrowsers();
}
public static void StartBrowser(string path)
{
lock (LockObj)
{
Host.Local.OpenBrowser(path, "ie", false, true);
Console.WriteLine("Browser is started.");
}
}
public static void StartBrowser(string path, Browser browser, bool killExisting = true, bool newSession = false,
bool incognitoMode = false)
{
var args = string.Empty;
if (newSession)
{
switch (browser)
{
case Browser.Explorer:
args = "-nomerge";
break;
}
}
lock (LockObj)
{
Host.Local.OpenBrowser(path, GetBrowserName(browser), args, killExisting, false, false, incognitoMode, false);
Console.WriteLine("Browser is started.");
}
}
public static String GetBrowserName(Browser browser)
{
switch (browser)
{
case Browser.Explorer:
{
return "IE";
}
case Browser.Firefox:
{
return "Firefox";
}
case Browser.Chrome:
{
return "Chrome";
}
}
return null;
}
public static void KillBrowser()
{
lock (LockObj)
{
Console.WriteLine("Killing browser.");
Process[] ieProcesses = Process.GetProcessesByName("iexplore");
foreach (var ieProcess in ieProcesses)
{
try
{
ieProcess.Kill();
ieProcess.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
public static void KillBrowsers()
{
lock (LockObj)
{
Console.WriteLine("Killing browser.");
KillBrowser("iexplore");
KillBrowser("firefox");
KillBrowser("chrome");
}
}
private static void KillBrowser(String browserName)
{
lock (LockObj)
{
Console.WriteLine("Killing browser.");
Process[] ieProcesses = Process.GetProcessesByName(browserName);
foreach (var ieProcess in ieProcesses)
{
try
{
ieProcess.Kill();
ieProcess.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
protected Browser CurrentBrowser;
private TestContext _testContextInstance;
private static readonly object LockObj = new object();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment