2011-12-19 08:08:36 +03: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
|
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
[Explicit]
|
2012-02-16 05:22:14 +03:00
|
|
|
[TestFixture(Category = "Automation")]
|
2011-12-19 08:08:36 +03:00
|
|
|
public abstract class AutomationTestBase
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
private static readonly EnvironmentProvider environmentProvider = new EnvironmentProvider();
|
2011-12-19 08:08:36 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private readonly string _clonePackagePath;
|
|
|
|
private readonly string _masterPackagePath;
|
|
|
|
|
|
|
|
protected string AppUrl
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return "http://localhost:8989";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
protected AutomationTestBase()
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
var rawPackagePath = Path.Combine(environmentProvider.ApplicationPath, "_rawPackage");
|
2012-02-16 04:53:59 +03:00
|
|
|
_clonePackagePath = Path.Combine(rawPackagePath, "NzbDrone_Automation");
|
|
|
|
_masterPackagePath = Path.Combine(rawPackagePath, "NzbDrone");
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
protected RemoteWebDriver Driver { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-19 08:08:36 +03:00
|
|
|
[SetUp]
|
|
|
|
public void AutomationSetup()
|
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void AutomationTearDown()
|
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
|
2011-12-19 08:08:36 +03: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();
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
DeleteClone();
|
|
|
|
ClonePackage();
|
|
|
|
|
|
|
|
//StartNzbDrone();
|
|
|
|
InstallNzbDroneService();
|
|
|
|
|
|
|
|
new HttpProvider().DownloadString(AppUrl);
|
|
|
|
|
2011-12-19 22:31:17 +03:00
|
|
|
Driver = new FirefoxDriver();
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
|
2011-12-19 08:08:36 +03:00
|
|
|
[TestFixtureTearDown]
|
|
|
|
public void AutomationTestFixtureTearDown()
|
|
|
|
{
|
2011-12-19 22:31:17 +03:00
|
|
|
Driver.Close();
|
2011-12-19 08:08:36 +03:00
|
|
|
StopNzbDrone();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void CaptureScreen()
|
|
|
|
{
|
|
|
|
var method = new StackFrame(1).GetMethod().Name;
|
|
|
|
|
2011-12-19 22:31:17 +03:00
|
|
|
var fileName = String.Format("{0}__{1}.png", GetType().Name, method);
|
2011-12-19 08:08:36 +03:00
|
|
|
|
|
|
|
((ITakesScreenshot)Driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Png);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private void StartNzbDrone()
|
|
|
|
{
|
|
|
|
StartProcess("nzbdrone.exe", false);
|
2011-12-19 08:08:36 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
}
|
2011-12-19 08:08:36 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private void StopNzbDrone()
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
|
|
|
|
2012-02-22 05:31:39 +03:00
|
|
|
foreach (var process in Process.GetProcesses())
|
2012-02-16 04:53:59 +03:00
|
|
|
{
|
2012-02-22 05:31:39 +03:00
|
|
|
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
|
2012-02-22 07:52:15 +03:00
|
|
|
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2012-02-22 05:31:39 +03:00
|
|
|
process.Kill();
|
2012-02-22 07:52:15 +03:00
|
|
|
process.WaitForExit();
|
|
|
|
}
|
2012-02-16 04:53:59 +03:00
|
|
|
}
|
2011-12-19 08:08:36 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
try
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
StartProcess("ServiceUninstall.exe", true);
|
|
|
|
}
|
2012-02-22 05:31:39 +03:00
|
|
|
catch { }
|
2012-02-16 04:53:59 +03:00
|
|
|
|
2012-02-22 05:31:39 +03:00
|
|
|
foreach (var process in Process.GetProcesses())
|
|
|
|
{
|
|
|
|
if (string.Equals(process.ProcessName, "NzbDrone", StringComparison.InvariantCultureIgnoreCase)
|
2012-02-22 07:52:15 +03:00
|
|
|
|| string.Equals(process.ProcessName, "IISExpress", StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
||
|
|
|
|
string.Equals(process.ProcessName, "ServiceUninstall", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2012-02-22 05:31:39 +03:00
|
|
|
process.Kill();
|
2012-02-22 07:52:15 +03:00
|
|
|
process.WaitForExit();
|
|
|
|
}
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private void InstallNzbDroneService()
|
|
|
|
{
|
|
|
|
StartProcess("ServiceInstall.exe", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartProcess(string fileName, bool waitForExit)
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
2011-12-19 22:07:39 +03:00
|
|
|
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
FileName = Path.Combine(_clonePackagePath, fileName),
|
2011-12-19 22:07:39 +03:00
|
|
|
RedirectStandardOutput = true,
|
2012-02-16 04:53:59 +03:00
|
|
|
RedirectStandardError = true,
|
|
|
|
UseShellExecute = false,
|
|
|
|
CreateNoWindow = true,
|
2011-12-19 22:07:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
var nzbDroneProcess = new Process
|
2012-02-16 04:53:59 +03:00
|
|
|
{
|
|
|
|
StartInfo = startInfo
|
|
|
|
};
|
|
|
|
|
|
|
|
nzbDroneProcess.OutputDataReceived += (o, args) => Console.WriteLine(args.Data);
|
|
|
|
nzbDroneProcess.ErrorDataReceived += (o, args) => Console.WriteLine(args.Data);
|
2011-12-19 22:07:39 +03:00
|
|
|
|
|
|
|
nzbDroneProcess.Start();
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
nzbDroneProcess.BeginErrorReadLine();
|
|
|
|
nzbDroneProcess.BeginOutputReadLine();
|
|
|
|
|
|
|
|
if (waitForExit)
|
|
|
|
{
|
|
|
|
nzbDroneProcess.WaitForExit();
|
|
|
|
}
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
|
2011-12-19 22:07:39 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private void ClonePackage()
|
|
|
|
{
|
|
|
|
new DiskProvider().CopyDirectory(_masterPackagePath, _clonePackagePath);
|
|
|
|
}
|
2011-12-19 22:07:39 +03:00
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private void DeleteClone()
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
if (Directory.Exists(_clonePackagePath))
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
2012-02-16 04:53:59 +03:00
|
|
|
Directory.Delete(_clonePackagePath, true);
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:53:59 +03:00
|
|
|
private string CreatePackage()
|
2011-12-19 08:08:36 +03:00
|
|
|
{
|
|
|
|
Console.WriteLine("Creating NzbDrone Package");
|
|
|
|
|
|
|
|
StopNzbDrone();
|
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
var rootDirectory = new DirectoryInfo(environmentProvider.ApplicationPath);
|
2011-12-19 08:08:36 +03: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();
|
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
var testFolder = Path.Combine(environmentProvider.SystemTemp, "NzbDroneAutomation");
|
2011-12-19 08:08:36 +03:00
|
|
|
|
|
|
|
if (Directory.Exists(testFolder))
|
|
|
|
{
|
|
|
|
Directory.Delete(testFolder, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory.Move(Path.Combine(rootDirectory.FullName, "_rawPackage", "nzbdrone"), testFolder);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return testFolder;
|
|
|
|
}
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
if (EnvironmentProvider.IsDebug)
|
2012-02-16 04:53:59 +03:00
|
|
|
{
|
|
|
|
folderName = "Release";
|
|
|
|
}
|
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
var dirs = Directory.GetDirectories(environmentProvider.ApplicationPath, folderName, SearchOption.AllDirectories);
|
2012-02-16 04:53:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
foreach (var dir in dirs)
|
|
|
|
{
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
}
|
|
|
|
}
|
2011-12-19 08:08:36 +03:00
|
|
|
}
|
|
|
|
}
|