Skip to content

Commit

Permalink
Merge pull request #643 from SavageLearning/feature/seleniun_work_4_t…
Browse files Browse the repository at this point in the history
…est_env

Feature/seleniun work 4 test env
  • Loading branch information
esteban-gs authored Mar 1, 2021
2 parents 94c4926 + 76688cd commit 6dc8b86
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 82 deletions.
124 changes: 124 additions & 0 deletions Machete.Selenium/Integration/HttpClientUtil/HttpClientUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Machete.Domain;
using Machete.Web.Maps.Api;
using Newtonsoft.Json;
using LookupViewModel = Machete.Web.ViewModel.Api.LookupVM;
using WorkAssignmentViewModel = Machete.Web.ViewModel.Api.WorkAssignmentVM;

namespace Machete.Test.Integration.HttpClientUtil
{
/// <summary>
/// The httpClient utility to grab Machete Lookups from the desired URL.
/// Replaces static lookups and allows UI testing on deployed test environments
/// </summary>
public static class HttpClientUtil
{
private static readonly HttpClient HttpClient;
private static readonly IMapper _mapper;

public static List<Lookup> TenantLookupsCache => _tenantLookupsCache;
private static List<Lookup> _tenantLookupsCache;

/// <summary>
/// Initializes httpClient instance with a cookie container
/// Initializes automapper with the necessary profiles
/// </summary>
static HttpClientUtil()
{
HttpClientHandler clientHandler =
new HttpClientHandler();
clientHandler.UseCookies = true;
clientHandler.CookieContainer = new CookieContainer();
HttpClient = new HttpClient(clientHandler);
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddProfile<LookupsMap>();
cfg.AddProfile<WorkAssignmentsMap>();
}
);
_mapper = new Mapper(configuration);
}

/// <summary>
///
/// </summary>
/// <param name="url">The base url for the Machete tenant e.g. https://tenant.example.com/</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task SetTenantLookUpCache(string url)
{
if (TenantLookupsCache is null)
{
var httpResponse = await HttpClient.GetAsync($"{url}api/lookups");
if (!httpResponse.IsSuccessStatusCode)
{
throw new Exception("Cannot retrieve records");
}
var content = await httpResponse.Content.ReadAsStringAsync();
var deserializedContent = JsonConvert.DeserializeObject<List<LookupViewModel>>(content);
var resultList = _mapper.Map<List<Lookup>>(deserializedContent);
_tenantLookupsCache = new List<Lookup>(resultList);
var temp = _tenantLookupsCache.First(x => x.category == "race" && x.text_EN == "Latino").ID;
Console.WriteLine("asdfasdf");
}
}

public static int GetLookup(string category, string text_EN)
{
var lu = _tenantLookupsCache.FirstOrDefault(x => x.category == category && x.text_EN == text_EN);
if (lu is null)
{
throw new Exception("No record found");
}
return lu.ID;
}

public static int GetFirstLookupInCategory(string category)
{
var lu = _tenantLookupsCache.FirstOrDefault(x => x.category == category);
if (lu is null)
{
throw new Exception("No record found");
}
return lu.ID;
}

public static string GetFirstLookupTextEn(int id)
{
var lu = _tenantLookupsCache.FirstOrDefault(x => x.ID == id);
if (lu is null)
{
throw new Exception("No record found");
}
return lu.text_EN;
}

public static string TextEN(this int id) => GetFirstLookupTextEn(id);

public static async Task<int> GetWorkAssignment(int id)
{
var waPseudoId = "";
var creds = JsonConvert.SerializeObject(new
{
username = SharedConfig.SeleniumUser,
passWord = SharedConfig.SeleniumUserPassword
});
var body = new StringContent(creds, Encoding.UTF8, "application/json");
var httpResponse = await HttpClient.PostAsync($"{SharedConfig.BaseSeleniumTestUrl}id/login", body);
httpResponse.EnsureSuccessStatusCode();
var waResponse = await
HttpClient.GetAsync($"{SharedConfig.BaseSeleniumTestUrl}api/workassignments/{id}");
var httpResponseString = await waResponse.Content.ReadAsStringAsync();
var deserializedResponse = JsonConvert.DeserializeObject<WorkAssignmentViewModel>(httpResponseString);
var domainWA = _mapper.Map<WorkAssignment>(deserializedResponse);
return domainWA.pseudoID ?? 0;
}
}
}
16 changes: 16 additions & 0 deletions Machete.Selenium/Integration/HttpClientUtil/ListResponseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using Machete.Domain;

namespace Machete.Test.Integration.HttpClientUtil
{
public class ListResponseModel<T> where T : class

{
public List<T> data { get; set; }
}

public class ItemResponseModel<T> where T : class
{
public T data { get; set; }
}
}
17 changes: 10 additions & 7 deletions Machete.Selenium/Integration/Views/ActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Machete.Web.Maps;
using Machete.Test.Integration.Fluent;
using Machete.Test.Integration.HttpClientUtil;

namespace Machete.Test.Selenium.View
{
Expand All @@ -26,22 +27,24 @@ public class ActivityTests
static IMapper map;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
public static async Task ClassInitialize(TestContext testContext)
{
//WebServer.StartIis();
var webMapperConfig = new MapperConfiguration(config => config.ConfigureMvc());
map = webMapperConfig.CreateMapper();
baseURL = SharedConfig.BaseSeleniumTestUrl;
// Get this tenant's lookups based on the baseURL
await HttpClientUtil.SetTenantLookUpCache(baseURL);
}

[TestInitialize]
public void SetupTest()
{
frb = FluentRecordBaseFactory.Get();
driver = new ChromeDriver("/usr/local/bin");
baseURL = "http://localhost:4213/";
driver = new ChromeDriver(SharedConfig.ChromeDriverPath);
ui = new sharedUI(driver, baseURL, map);
verificationErrors = new StringBuilder();
ui.login();
ui.login(SharedConfig.SeleniumUser, SharedConfig.SeleniumUserPassword);
}

[TestCleanup]
Expand Down Expand Up @@ -254,8 +257,8 @@ public void SeActivity_test_search()

// Test good search first
ui.WaitForElement(By.Id("activityTable_searchbox")).Clear();
ui.WaitForElement(By.Id("activityTable_searchbox")).SendKeys("jadmin");
result = ui.WaitForElementValue(By.XPath("//table[@id='activityTable']/tbody/tr[5]/td[3]"), "jadmin");
ui.WaitForElement(By.Id("activityTable_searchbox")).SendKeys(SharedConfig.SeleniumUser);
result = ui.WaitForElementValue(By.XPath("//table[@id='activityTable']/tbody/tr[5]/td[3]"), SharedConfig.SeleniumUser);
Assert.IsTrue(result, "Activities search not returning proper results");
}

Expand Down
24 changes: 14 additions & 10 deletions Machete.Selenium/Integration/Views/EmployerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using System.Configuration;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Machete.Web.Maps;
using Machete.Test.Integration.Fluent;
using Machete.Test.Integration.HttpClientUtil;

namespace Machete.Test.Selenium.View
{
Expand All @@ -18,27 +20,29 @@ public class EmployerTests
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
static private string baseURL;
private sharedUI ui;
private FluentRecordBase frb;
private static IMapper map;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
public static async Task ClassInitialize(TestContext testContext)
{
var webMapperConfig = new MapperConfiguration(config => { config.ConfigureMvc(); });
map = webMapperConfig.CreateMapper();
baseURL = SharedConfig.BaseSeleniumTestUrl;
// Get this tenant's lookups based on the baseURL
await HttpClientUtil.SetTenantLookUpCache(baseURL);
}

[TestInitialize]
public void SetupTest()
{
frb = FluentRecordBaseFactory.Get();
driver = new ChromeDriver("/usr/local/bin");
baseURL = "http://localhost:4213/";
driver = new ChromeDriver(SharedConfig.ChromeDriverPath);
ui = new sharedUI(driver, baseURL, map);
verificationErrors = new StringBuilder();
ui.login();
ui.login(SharedConfig.SeleniumUser, SharedConfig.SeleniumUserPassword);
}

[TestCleanup]
Expand Down Expand Up @@ -86,7 +90,7 @@ public void SeEmployer_Create_workorder()
//Arrange
var _emp = frb.CloneEmployer();
var _wo = frb.CloneWorkOrder();
_wo.statusID = frb.ToServ<ILookupService>().GetByKey(LCategory.orderstatus, LOrderStatus.Pending).ID; // start work order off as pending
_wo.statusID = HttpClientUtil.GetLookup(LCategory.orderstatus, LOrderStatus.Pending); // start work order off as pending
//Act
ui.employerCreate(_emp);
ui.workOrderCreate(_emp, _wo);
Expand Down Expand Up @@ -119,20 +123,20 @@ public void SeEmployer_Create_workorder_copyinfo()
_wo.zipcode = _emp.zipcode;
_wo.phone = _emp.phone;
_wo.description = "";
_wo.statusID = frb.ToServ<ILookupService>().GetByKey(LCategory.orderstatus, LOrderStatus.Pending).ID;
_wo.statusID = HttpClientUtil.GetLookup(LCategory.orderstatus, LOrderStatus.Pending);
_wo.ID = ui.getSelectedTabRecordID("WO");
//Assert
ui.workOrderValidate(_wo);
}
[TestMethod, TestCategory(TC.SE), TestCategory(TC.View), TestCategory(TC.Employers)]
public void SeEmployer_Create_and_Activate_WorkAssignment()
public async Task SeEmployer_Create_and_Activate_WorkAssignment()
{
//Arrange
var _employer1 = frb.CloneEmployer();
var _wo = frb.CloneWorkOrder();
var _wa1 = frb.CloneWorkAssignment();
_wo.contactName = ui.RandomString(10);
_wo.statusID = frb.ToServ<ILookupService>().GetByKey(LCategory.orderstatus, LOrderStatus.Pending).ID; // status = pending
_wo.statusID = HttpClientUtil.GetLookup(LCategory.orderstatus, LOrderStatus.Pending); // status = pending
//
// Create employer
ui.employerCreate(_employer1);
Expand All @@ -144,7 +148,7 @@ public void SeEmployer_Create_and_Activate_WorkAssignment()
//_wa1.workOrder = _wo;
_wa1.workOrderID = _wo.ID;
// pseudoID needs to be updated; created on save above
_wa1.pseudoID = frb.ToServ<IWorkAssignmentService>().Get(_wa1.ID).pseudoID;
_wa1.pseudoID = await HttpClientUtil.GetWorkAssignment(_wa1.ID);
// Activate assignment
ui.workAssignmentActivate(_employer1, _wo, _wa1);
//
Expand Down
14 changes: 9 additions & 5 deletions Machete.Selenium/Integration/Views/PersonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Machete.Data.Initialize;
using Machete.Web.Maps;
using Machete.Test.Integration.Fluent;
using Machete.Test.Integration.HttpClientUtil;

namespace Machete.Test.Selenium.View
{
Expand All @@ -17,32 +19,34 @@ public class PersonTests
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private static string baseURL;
private sharedUI ui;
//private static string testdir;
private static string testimagefile;
FluentRecordBase frb;
static IMapper map;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext) {
public static async Task ClassInitialize(TestContext testContext) {
string solutionDirectory = sharedUI.SolutionDirectory();
//testdir = solutionDirectory + "\\Machete.test\\";
testimagefile = solutionDirectory + "/jimmy_machete.jpg";
var mapperConfig = new MapperConfiguration(config => { config.ConfigureMvc(); });
map = mapperConfig.CreateMapper();
baseURL = SharedConfig.BaseSeleniumTestUrl;
// Get this tenant's lookups based on the baseURL from appsettings.json
await HttpClientUtil.SetTenantLookUpCache(baseURL);
}

[TestInitialize]
public void SetupTest()
{
frb = FluentRecordBaseFactory.Get();

driver = new ChromeDriver("/usr/local/bin");
baseURL = "http://localhost:4213/";
driver = new ChromeDriver(SharedConfig.ChromeDriverPath);
ui = new sharedUI(driver, baseURL, map);
verificationErrors = new StringBuilder();
ui.login();
ui.login(SharedConfig.SeleniumUser, SharedConfig.SeleniumUserPassword);
}

[TestCleanup]
Expand Down
6 changes: 3 additions & 3 deletions Machete.Selenium/Integration/Views/UnauthActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public static void ClassInitialize(TestContext testContext)
public void SetupTest()
{
frb = FluentRecordBaseFactory.Get();
driver = new ChromeDriver("/usr/local/bin");
baseURL = "http://localhost:4213/";
driver = new ChromeDriver(SharedConfig.ChromeDriverPath);
baseURL = SharedConfig.BaseSeleniumTestUrl;
ui = new sharedUI(driver, baseURL, map);
verificationErrors = new StringBuilder();
ui.login();
ui.login(SharedConfig.SeleniumUser, SharedConfig.SeleniumUserPassword);
ui.gotoMachete();
}

Expand Down
Loading

0 comments on commit 6dc8b86

Please sign in to comment.