1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-30 23:42:33 +01:00
Sonarr/NzbDrone.Web.UI.Test/AutomationTestBase.cs

257 lines
7.0 KiB
C#
Raw Normal View History

2011-12-19 06:08:36 +01: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 06:08:36 +01:00
public abstract class AutomationTestBase
{
private static readonly EnvironmentProvider environmentProvider = new EnvironmentProvider();
2011-12-19 06:08:36 +01:00
private readonly string _clonePackagePath;
private readonly string _masterPackagePath;
protected string AppUrl
2011-12-19 06:08:36 +01:00
{
get
{
return "http://localhost:8989";
}
}
protected AutomationTestBase()
2011-12-19 06:08:36 +01:00
{
var rawPackagePath = Path.Combine(environmentProvider.ApplicationPath, "_rawPackage");
_clonePackagePath = Path.Combine(rawPackagePath, "NzbDrone_Automation");
_masterPackagePath = Path.Combine(rawPackagePath, "NzbDrone");
2011-12-19 06:08:36 +01:00
}
protected RemoteWebDriver Driver { get; private set; }
2011-12-19 06:08:36 +01:00
[SetUp]
public void AutomationSetup()
{
2011-12-19 06:08:36 +01:00
}
[TearDown]
public void AutomationTearDown()
{
2011-12-19 06:08:36 +01: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 06:08:36 +01:00
}
2011-12-19 06:08:36 +01:00
[TestFixtureTearDown]
public void AutomationTestFixtureTearDown()
{
Driver.Close();
2011-12-19 06:08:36 +01: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 06:08:36 +01:00
((ITakesScreenshot)Driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Png);
}
private void StartNzbDrone()
{
StartProcess("nzbdrone.exe", false);
2011-12-19 06:08:36 +01:00
}
2011-12-19 06:08:36 +01:00
private void StopNzbDrone()
2011-12-19 06:08:36 +01:00
{
foreach (var process in Process.GetProcesses())
{
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
2012-02-22 05:52:15 +01:00
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase))
{
process.Kill();
2012-02-22 05:52:15 +01:00
process.WaitForExit();
}
}
2011-12-19 06:08:36 +01:00
try
2011-12-19 06:08:36 +01:00
{
StartProcess("ServiceUninstall.exe", true);
}
catch { }
foreach (var process in Process.GetProcesses())
{
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
2012-02-22 05:52:15 +01:00
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase)
||
string.Equals(process.ProcessName, "ServiceUninstall", StringComparison.InvariantCultureIgnoreCase))
{
process.Kill();
2012-02-22 05:52:15 +01:00
process.WaitForExit();
}
2011-12-19 06:08:36 +01:00
}
}
private void InstallNzbDroneService()
{
StartProcess("ServiceInstall.exe", true);
}
private void StartProcess(string fileName, bool waitForExit)
2011-12-19 06:08:36 +01:00
{
2011-12-19 20:07:39 +01:00
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(_clonePackagePath, fileName),
2011-12-19 20:07:39 +01:00
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
2011-12-19 20:07:39 +01: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 20:07:39 +01:00
nzbDroneProcess.Start();
nzbDroneProcess.BeginErrorReadLine();
nzbDroneProcess.BeginOutputReadLine();
if (waitForExit)
{
nzbDroneProcess.WaitForExit();
}
2011-12-19 06:08:36 +01:00
}
2011-12-19 20:07:39 +01:00
private void ClonePackage()
{
new DiskProvider().CopyDirectory(_masterPackagePath, _clonePackagePath);
}
2011-12-19 20:07:39 +01:00
private void DeleteClone()
2011-12-19 06:08:36 +01:00
{
if (Directory.Exists(_clonePackagePath))
2011-12-19 06:08:36 +01:00
{
Directory.Delete(_clonePackagePath, true);
2011-12-19 06:08:36 +01:00
}
}
private string CreatePackage()
2011-12-19 06:08:36 +01:00
{
Console.WriteLine("Creating NzbDrone Package");
StopNzbDrone();
var rootDirectory = new DirectoryInfo(environmentProvider.ApplicationPath);
2011-12-19 06:08:36 +01:00
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(environmentProvider.SystemTemp, "NzbDroneAutomation");
2011-12-19 06:08:36 +01:00
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 (EnvironmentProvider.IsDebug)
{
folderName = "Release";
}
var dirs = Directory.GetDirectories(environmentProvider.ApplicationPath, folderName, SearchOption.AllDirectories);
foreach (var dir in dirs)
{
Directory.Delete(dir, true);
}
}
2011-12-19 06:08:36 +01:00
}
}