You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone.Web.UI.Test/AutomationTestBase.cs

257 lines
7.0 KiB

13 years ago
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")]
13 years ago
public abstract class AutomationTestBase
{
private static readonly EnvironmentProvider environmentProvider = new EnvironmentProvider();
13 years ago
private readonly string _clonePackagePath;
private readonly string _masterPackagePath;
protected string AppUrl
13 years ago
{
get
{
return "http://localhost:8989";
}
}
protected AutomationTestBase()
13 years ago
{
var rawPackagePath = Path.Combine(environmentProvider.ApplicationPath, "_rawPackage");
_clonePackagePath = Path.Combine(rawPackagePath, "NzbDrone_Automation");
_masterPackagePath = Path.Combine(rawPackagePath, "NzbDrone");
13 years ago
}
protected RemoteWebDriver Driver { get; private set; }
13 years ago
[SetUp]
public void AutomationSetup()
{
13 years ago
}
[TearDown]
public void AutomationTearDown()
{
13 years ago
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();
13 years ago
}
13 years ago
[TestFixtureTearDown]
public void AutomationTestFixtureTearDown()
{
Driver.Close();
13 years ago
StopNzbDrone();
}
protected void CaptureScreen()
{
var method = new StackFrame(1).GetMethod().Name;
var fileName = String.Format("{0}__{1}.png", GetType().Name, method);
13 years ago
((ITakesScreenshot)Driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Png);
}
private void StartNzbDrone()
{
StartProcess("nzbdrone.exe", false);
13 years ago
}
13 years ago
private void StopNzbDrone()
13 years ago
{
foreach (var process in Process.GetProcesses())
{
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase))
{
process.Kill();
process.WaitForExit();
}
}
13 years ago
try
13 years ago
{
StartProcess("ServiceUninstall.exe", true);
}
catch { }
foreach (var process in Process.GetProcesses())
{
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase)
||
string.Equals(process.ProcessName, "ServiceUninstall", StringComparison.InvariantCultureIgnoreCase))
{
process.Kill();
process.WaitForExit();
}
13 years ago
}
}
private void InstallNzbDroneService()
{
StartProcess("ServiceInstall.exe", true);
}
private void StartProcess(string fileName, bool waitForExit)
13 years ago
{
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(_clonePackagePath, fileName),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
var nzbDroneProcess = new Process
{
StartInfo = startInfo
};
nzbDroneProcess.OutputDataReceived += (o, args) => Console.WriteLine(args.Data);
nzbDroneProcess.ErrorDataReceived += (o, args) => Console.WriteLine(args.Data);
nzbDroneProcess.Start();
nzbDroneProcess.BeginErrorReadLine();
nzbDroneProcess.BeginOutputReadLine();
if (waitForExit)
{
nzbDroneProcess.WaitForExit();
}
13 years ago
}
private void ClonePackage()
{
new DiskProvider().CopyDirectory(_masterPackagePath, _clonePackagePath);
}
private void DeleteClone()
13 years ago
{
if (Directory.Exists(_clonePackagePath))
13 years ago
{
Directory.Delete(_clonePackagePath, true);
13 years ago
}
}
private string CreatePackage()
13 years ago
{
Console.WriteLine("Creating NzbDrone Package");
StopNzbDrone();
var rootDirectory = new DirectoryInfo(environmentProvider.ApplicationPath);
13 years ago
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");
13 years ago
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);
}
}
13 years ago
}
}