173 lines
5.9 KiB
C#
173 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Data.Entity.Infrastructure;
|
|
using System.Data.Entity.Validation;
|
|
using System.Data.SqlClient;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using IntegrationTests.Helpers;
|
|
using IntegrationTests.Models;
|
|
using IntegrationTests.Properties;
|
|
using NLog;
|
|
using NUnit.Framework;
|
|
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Support.UI;
|
|
|
|
namespace IntegrationTests
|
|
{
|
|
public class BaseIntegrationTest
|
|
{
|
|
protected Logger Logger = LogManager.GetCurrentClassLogger();
|
|
protected WebDriverContext DriverContext { get; set; }
|
|
protected IWebDriver WebDriver { get { return DriverContext == null ? null : DriverContext.WebDriver; } }
|
|
|
|
[TestFixtureSetUp]
|
|
public virtual void TestFixtureSetUp()
|
|
{
|
|
try
|
|
{
|
|
ExecScript(@"..\..\SQLScripts\CreateSnapshot.sql");
|
|
DriverContext = WebDriverContext.GetInstance();
|
|
Login();
|
|
Console.WriteLine("TestFixtureSetUp");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[TestFixtureTearDown]
|
|
public virtual void TestFixtureTearDown()
|
|
{
|
|
try
|
|
{
|
|
Logger.Fatal("started to tear down fixture");
|
|
DriverContext.WebDriver.Quit();
|
|
ExecScript(@"..\..\SQLScripts\DeleteSnapshot.sql");
|
|
Console.WriteLine("TestFixtureTearDown");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[SetUp]
|
|
public virtual void SetUp()
|
|
{
|
|
try
|
|
{
|
|
//CommonWebDriver = WebDriverContext.GetInstance();
|
|
//Login();
|
|
Console.WriteLine("SetUp");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[TearDown]
|
|
public virtual void TearDown()
|
|
{
|
|
try
|
|
{
|
|
Logger.Fatal("started to tear down test");
|
|
//CommonWebDriver.WebDriver.Quit();
|
|
ExecScript(@"..\..\SQLScripts\RestoreSnapshot.sql");
|
|
Console.WriteLine("TearDown");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogError(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
#region Helper Methods
|
|
public void Login()
|
|
{
|
|
if (WebDriver.FindElements(By.Id("aUserName")).Count != 0) return;
|
|
|
|
WebDriver.Url = Settings.Default.WebSiteUrl + "Account/Login";
|
|
WebDriver.Navigate();
|
|
|
|
WebDriver.FindElement(By.Id("username_id")).SendKeys(Settings.Default.AdminUser);
|
|
WebDriver.FindElement(By.Id("password_id")).SendKeys(Settings.Default.AdminPwd);
|
|
WebDriver.FindElement(By.Id("btnSubmit")).Click();
|
|
Browser.WaitForAjax(WebDriver);
|
|
var userLink = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10)).Until(
|
|
w => w.FindElement(By.Id("aUserName")));
|
|
}
|
|
public void Logoff()
|
|
{
|
|
if (WebDriver.FindElements(By.Id("aLogOff")).Count == 0) return;
|
|
WebDriver.FindElement(By.Id("aLogOff")).Click();
|
|
var wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
|
|
var userLink = wait.Until(w => w.FindElement(By.Id("username_id")));
|
|
}
|
|
|
|
public void ExecScript(string scriptFilePath)
|
|
{
|
|
var sql = File.OpenText(scriptFilePath).ReadToEnd();
|
|
if (string.IsNullOrWhiteSpace(sql)) return;
|
|
var tables = Regex.Split(sql, "^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
using (var dbContext = new EnvisageTestContext())
|
|
{
|
|
foreach (var table in tables.Where(table => !string.IsNullOrWhiteSpace(table)))
|
|
{
|
|
((IObjectContextAdapter)dbContext).ObjectContext.ExecuteStoreCommand(TransactionalBehavior.DoNotEnsureTransaction, string.Format(table,
|
|
Settings.Default.DatabaseName, Settings.Default.SnapshotPath));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LogError(Exception exception)
|
|
{
|
|
if (exception == null)
|
|
return;
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine(string.Format("{0}: {1}", exception.GetType(), exception.Message));
|
|
sb.AppendLine(exception.StackTrace);
|
|
|
|
var innerCount = 0;
|
|
var innerEx = exception;
|
|
while (innerEx.InnerException != null && innerCount++ < Constants.MAX_INNER_EXCEPTION_LOG_LEVEL)
|
|
{
|
|
if (innerEx.Message != innerEx.InnerException.Message)
|
|
sb.AppendLine("Inner Exception Message: " + exception.InnerException.Message);
|
|
innerEx = innerEx.InnerException;
|
|
}
|
|
if (exception is DbEntityValidationException)
|
|
{
|
|
sb.AppendLine();
|
|
foreach (var validationErrors in ((DbEntityValidationException)exception).EntityValidationErrors)
|
|
{
|
|
|
|
foreach (var validationError in validationErrors.ValidationErrors)
|
|
{
|
|
sb.AppendFormat("Property: {0} Error: {1}", validationError.PropertyName,
|
|
validationError.ErrorMessage);
|
|
|
|
}
|
|
}
|
|
sb.AppendLine(exception.StackTrace);
|
|
}
|
|
var logger = LogManager.GetCurrentClassLogger();
|
|
if (logger != null)
|
|
logger.Fatal(sb.ToString());
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|