sonarr-repo-only/NzbDrone.Web.UI.Test/AutomationTestBase.cs

249 lines
6.4 KiB
C#
Raw Normal View History

2011-12-19 05:08:36 +00:00
using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using NUnit.Framework;
using NzbDrone.Common;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
namespace NzbDrone.Web.UI.Automation
{
[Explicit]
[TestFixture(Category = "Automation")]
2011-12-19 05:08:36 +00:00
public abstract class AutomationTestBase
{
private static readonly EnviromentProvider enviromentProvider = new EnviromentProvider();
2011-12-19 05:08:36 +00:00
private readonly string _clonePackagePath;
private readonly string _masterPackagePath;
protected string AppUrl
2011-12-19 05:08:36 +00:00
{
get
{
return "http://localhost:8989";
}
}
protected AutomationTestBase()
2011-12-19 05:08:36 +00:00
{
var rawPackagePath = Path.Combine(enviromentProvider.ApplicationPath, "_rawPackage");
_clonePackagePath = Path.Combine(rawPackagePath, "NzbDrone_Automation");
_masterPackagePath = Path.Combine(rawPackagePath, "NzbDrone");
2011-12-19 05:08:36 +00:00
}
protected RemoteWebDriver Driver { get; private set; }
2011-12-19 05:08:36 +00:00
[SetUp]
public void AutomationSetup()
{
2011-12-19 05:08:36 +00:00
}
[TearDown]
public void AutomationTearDown()
{
2011-12-19 05:08:36 +00:00
if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Screenshots"))
{
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Screenshots");
}
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*__*.png").Select(c => new FileInfo(c)))
{
File.Copy(file.FullName, Directory.GetCurrentDirectory() + "\\Screenshots\\" + file.Name, true);
file.Delete();
}
}
[TestFixtureSetUp]
public void AutomationTestFixtureSetup()
{
StopNzbDrone();
DeleteClone();
ClonePackage();
//StartNzbDrone();
InstallNzbDroneService();
new HttpProvider().DownloadString(AppUrl);
Driver = new FirefoxDriver();
2011-12-19 05:08:36 +00:00
}
2011-12-19 05:08:36 +00:00
[TestFixtureTearDown]
public void AutomationTestFixtureTearDown()
{
Driver.Close();
2011-12-19 05:08:36 +00:00
StopNzbDrone();
}
protected void CaptureScreen()
{
var method = new StackFrame(1).GetMethod().Name;
var fileName = String.Format("{0}__{1}.png", GetType().Name, method);
2011-12-19 05:08:36 +00:00
((ITakesScreenshot)Driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Png);
}
private void StartNzbDrone()
{
StartProcess("nzbdrone.exe", false);
2011-12-19 05:08:36 +00:00
}
2011-12-19 05:08:36 +00:00
private void StopNzbDrone()
2011-12-19 05:08:36 +00:00
{
foreach (var process in Process.GetProcessesByName("nzbdrone"))
2011-12-19 05:08:36 +00:00
{
process.Kill();
process.WaitForExit();
2011-12-19 05:08:36 +00:00
}
foreach (var process in Process.GetProcessesByName("iisexpress"))
{
process.Kill();
process.WaitForExit();
}
2011-12-19 05:08:36 +00:00
try
2011-12-19 05:08:36 +00:00
{
StartProcess("ServiceUninstall.exe", true);
}
catch (Exception)
{
2011-12-19 05:08:36 +00:00
}
}
private void InstallNzbDroneService()
{
StartProcess("ServiceInstall.exe", true);
}
private void StartProcess(string fileName, bool waitForExit)
2011-12-19 05:08:36 +00:00
{
2011-12-19 19:07:39 +00:00
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(_clonePackagePath, fileName),
2011-12-19 19:07:39 +00:00
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
2011-12-19 19:07:39 +00:00
};
var nzbDroneProcess = new Process
{
StartInfo = startInfo
};
nzbDroneProcess.OutputDataReceived += (o, args) => Console.WriteLine(args.Data);
nzbDroneProcess.ErrorDataReceived += (o, args) => Console.WriteLine(args.Data);
2011-12-19 19:07:39 +00:00
nzbDroneProcess.Start();
nzbDroneProcess.BeginErrorReadLine();
nzbDroneProcess.BeginOutputReadLine();
if (waitForExit)
{
nzbDroneProcess.WaitForExit();
}
2011-12-19 05:08:36 +00:00
}
2011-12-19 19:07:39 +00:00
private void ClonePackage()
{
new DiskProvider().CopyDirectory(_masterPackagePath, _clonePackagePath);
}
2011-12-19 19:07:39 +00:00
private void DeleteClone()
2011-12-19 05:08:36 +00:00
{
if (Directory.Exists(_clonePackagePath))
2011-12-19 05:08:36 +00:00
{
Directory.Delete(_clonePackagePath, true);
2011-12-19 05:08:36 +00:00
}
}
private string CreatePackage()
2011-12-19 05:08:36 +00:00
{
Console.WriteLine("Creating NzbDrone Package");
StopNzbDrone();
var rootDirectory = new DirectoryInfo(enviromentProvider.ApplicationPath);
if (rootDirectory.GetDirectories("_rawPackage").Any())
{
rootDirectory.GetDirectories("_rawPackage").ToList().ForEach(c => c.Delete(true));
}
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(rootDirectory.FullName, "package.bat"),
WorkingDirectory = rootDirectory.FullName
};
Process.Start(startInfo).WaitForExit();
var testFolder = Path.Combine(enviromentProvider.SystemTemp, "NzbDroneAutomation");
if (Directory.Exists(testFolder))
{
Directory.Delete(testFolder, true);
}
Directory.Move(Path.Combine(rootDirectory.FullName, "_rawPackage", "nzbdrone"), testFolder);
return testFolder;
}
private void ResetUserData()
{
var appDataPath = Path.Combine(_clonePackagePath, "NzbDrone.Web", "app_data");
if (Directory.Exists(appDataPath))
Directory.Delete(appDataPath, true);
}
private static void CleanBinFolder()
{
var folderName = "Debug";
if (EnviromentProvider.IsDebug)
{
folderName = "Release";
}
var dirs = Directory.GetDirectories(enviromentProvider.ApplicationPath, folderName, SearchOption.AllDirectories);
foreach (var dir in dirs)
{
Directory.Delete(dir, true);
}
}
2011-12-19 05:08:36 +00:00
}
}