test(Discord) Added test logs to discord and start of discord chat bot

This commit is contained in:
2022-04-28 12:56:49 -04:00
parent 156339786c
commit b65dae0884
25 changed files with 6277 additions and 181 deletions
+3 -5
View File
@@ -1,13 +1,11 @@
using OpenQA.Selenium;
using TestAutomation.Utils;
namespace TestAutomation.Pages;
namespace TestAutomation.Pages;
public class BasePage {
public Website website;
public Website website;
public BasePage(Website website) {
this.website = website;
}
}
+17 -15
View File
@@ -1,14 +1,17 @@
namespace TestAutomation.Pages;
using TestAutomation.Utils;
namespace TestAutomation.Pages;
public class HarassCalculatorPage : BasePage {
public HarassCalculatorPage(Website website) : base(website) { }
private IWebElement NumberOfWorkersLostToHarass => website.Find("numberOfWorkersLostToHarass");
private IWebElement NumberOfTownHallsExisting => website.Find("numberOfTownHallsExisting");
private IList<IWebElement> OnTownHallTravelTimes => website.FindChildren("numberOfTownHallTravelTimes", "input");
private IWebElement NumberOfTownHallsExisting => website.Find("numberOfTownHallsExisting");
private IList<IWebElement> OnTownHallTravelTimes => website.FindChildren("numberOfTownHallTravelTimes", "input");
private int TotalAlloyHarassment => website.FindInt("totalAlloyHarassment");
private int WorkerReplacementCost => website.FindInt("workerReplacementCost");
private int WorkerReplacementCost => website.FindInt("workerReplacementCost");
private int DelayedMiningCost => website.FindInt("delayedMiningCost");
private int AverageTravelTime => website.FindInt("getAverageTravelTime");
private int AverageTravelTime => website.FindInt("getAverageTravelTime");
private int ExampleTotalAlloyLoss => website.FindInt("exampleTotalAlloyLoss");
private int ExampleWorkerCost => website.FindInt("exampleWorkerCost");
private int ExampleMiningTimeCost => website.FindInt("exampleMiningTimeCost");
@@ -16,29 +19,27 @@ public class HarassCalculatorPage : BasePage {
private int ExampleTotalAlloyLossAccurate => website.FindInt("exampleTotalAlloyLossAccurate");
private int ExampleTotalAlloyLossAccurateDifference => website.FindInt("exampleTotalAlloyLossAccurateDifference");
public HarassCalculatorPage(Website website) : base(website) { }
public HarassCalculatorPage SetWorkersLostToHarass(int number) {
website.EnterInput(NumberOfWorkersLostToHarass, number);
return this;
}
public HarassCalculatorPage SetNumberOfTownHallsExisting(int number) {
website.EnterInput(NumberOfTownHallsExisting, number);
return this;
}
public HarassCalculatorPage SetTownHallTravelTime(int forTownHall, int number) {
website.EnterInput(OnTownHallTravelTimes[forTownHall], number);
return this;
}
public HarassCalculatorPage GetTotalAlloyHarassment(out int result) {
result = TotalAlloyHarassment;
return this;
}
public HarassCalculatorPage GetExampleTotalAlloyLoss(out int result) {
result = ExampleTotalAlloyLoss;
return this;
@@ -53,18 +54,19 @@ public class HarassCalculatorPage : BasePage {
result = ExampleMiningTimeCost;
return this;
}
public HarassCalculatorPage GetExampleTotalAlloyLossAccurate(out int result) {
result = ExampleTotalAlloyLossAccurate;
return this;
}
public HarassCalculatorPage GetExampleTotalAlloyLossDifference(out int result) {
result = ExampleTotalAlloyLossDifference;
return this;
}
public HarassCalculatorPage GetExampleTotalAlloyLossAccurateDifference(out int result) {
result = ExampleTotalAlloyLossAccurateDifference;
return this;
}
}
+2
View File
@@ -9,6 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net.Webhook" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0-preview.2.22152.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.0" />
-119
View File
@@ -1,119 +0,0 @@
namespace TestAutomation;
public class Tests
{
private IWebDriver _webDriver = default!;
private readonly string localhost = "https://localhost:7234";
private readonly string develop = "https://calm-mud-04916b210.1.azurestaticapps.net/";
private Website Website { get; }
public Tests() {
var options = new FirefoxOptions();
//var options = new ChromeOptions();
options.AcceptInsecureCertificates = true;
options.AddArgument("--headless");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--start-maximized");
options.AddArgument("--test-type");
options.AddArgument("--allow-running-insecure-content");
//_webDriver = new FirefoxDriver(options);
//_webDriver = new ChromeDriver(Environment.CurrentDirectory, options);
_webDriver = new FirefoxDriver(Environment.CurrentDirectory, options);
Website = new Website(_webDriver);
}
[OneTimeSetUp]
public void Setup()
{
_webDriver.Navigate().GoToUrl(develop);
_webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
}
[OneTimeTearDown]
public void TearDown()
{
_webDriver.Quit();
}
[Test]
public void HarassCalculator()
{
_webDriver.Navigate().GoToUrl(develop + "/harass-calculator");
int expectedTotalAlloyHarassment = 240;
Website.HarassCalculatorPage
.SetWorkersLostToHarass(3)
.SetNumberOfTownHallsExisting(2)
.SetTownHallTravelTime(0, 30)
.GetTotalAlloyHarassment(out var foundTotalAlloyHarassment);
Assert.True(expectedTotalAlloyHarassment.Equals(foundTotalAlloyHarassment),
$"expectTotalAlloyHarassment of {expectedTotalAlloyHarassment} " +
"does not equal " +
$"foundTotalAlloyHarassment of {foundTotalAlloyHarassment} ");
}
[Test]
public void HarassCalculatorInformation()
{
_webDriver.Navigate().GoToUrl(develop + "/harass-calculator");
int expectedExampleTotalAlloyLoss = 720;
int expectedExampleWorkerCost = 300;
int expectedExampleMiningTimeCost = 420;
int expectedExampleTotalAlloyLossDifference = 300;
int expectedExampleTotalAlloyLossAccurate = 450;
int expectedExampleTotalAlloyLossAccurateDifference = 270;
Website.HarassCalculatorPage
.GetExampleTotalAlloyLoss(out var foundTotalAlloyLoss)
.GetExampleWorkerCost(out var foundExampleWorkerCost)
.GetExampleMiningTimeCost(out var foundExampleMiningTimeCost)
.GetExampleTotalAlloyLossAccurate(out var foundExampleTotalAlloyLossAccurate)
.GetExampleTotalAlloyLossDifference(out var foundGetExampleTotalAlloyLossDifference)
.GetExampleTotalAlloyLossAccurateDifference(out var foundExampleTotalAlloyLossAccurateDifference);
Assert.True(expectedExampleTotalAlloyLoss.Equals(foundTotalAlloyLoss),
$"expectedExampleTotalAlloyLoss of {expectedExampleTotalAlloyLoss} " +
"does not equal " +
$"foundTotalAlloyLoss of {foundTotalAlloyLoss} ");
Assert.True(expectedExampleWorkerCost.Equals(foundExampleWorkerCost),
$"expectedExampleWorkerCost of {expectedExampleWorkerCost} " +
"does not equal " +
$"foundExampleWorkerCost of {foundExampleWorkerCost} ");
Assert.True(expectedExampleMiningTimeCost.Equals(foundExampleMiningTimeCost),
$"expectedExampleMiningTimeCost of {expectedExampleMiningTimeCost} " +
"does not equal " +
$"foundExampleMiningTimeCost of {foundExampleMiningTimeCost} ");
Assert.True(expectedExampleTotalAlloyLossAccurate.Equals(foundExampleTotalAlloyLossAccurate),
$"expectedExampleTotalAlloyLossAccurate of {expectedExampleTotalAlloyLossAccurate} " +
"does not equal " +
$"foundExampleTotalAlloyLossAccurate of {foundExampleTotalAlloyLossAccurate} ");
Assert.True(expectedExampleTotalAlloyLossDifference.Equals(foundGetExampleTotalAlloyLossDifference),
$"expectedExampleTotalAlloyLossDifference of {expectedExampleTotalAlloyLossDifference} " +
"does not equal " +
$"foundGetExampleTotalAlloyLossDifference of {foundGetExampleTotalAlloyLossDifference} ");
Assert.True(expectedExampleTotalAlloyLossAccurateDifference.Equals(foundExampleTotalAlloyLossAccurateDifference),
$"expectedExampleTotalAlloyLossAccurateDifference of {expectedExampleTotalAlloyLossAccurateDifference} " +
"does not equal " +
$"foundExampleTotalAlloyLossAccurateDifference of {foundExampleTotalAlloyLossAccurateDifference} ");
}
}
+141
View File
@@ -0,0 +1,141 @@
using System.Text;
using Newtonsoft.Json;
using TestAutomation.Utils;
namespace TestAutomation;
public class Tests {
private readonly IWebDriver _webDriver = default!;
private readonly string develop = "https://calm-mud-04916b210.1.azurestaticapps.net/";
private readonly HttpClient httpClient = new();
private readonly string localhost = "https://localhost:7234";
private readonly TestReport testReport = new();
public Tests() {
var options = new FirefoxOptions();
options.AcceptInsecureCertificates = true;
options.AddArgument("--headless");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--start-maximized");
options.AddArgument("--test-type");
options.AddArgument("--allow-running-insecure-content");
_webDriver = new FirefoxDriver(Environment.CurrentDirectory, options);
Website = new Website(_webDriver);
}
private Website Website { get; }
[OneTimeSetUp]
public void Setup() {
_webDriver.Navigate().GoToUrl(develop);
_webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
}
[OneTimeTearDown]
public void TearDown() {
_webDriver.Quit();
var message = new {
content = "Test Report " + DateTime.Now.ToString("dd/MM/yyyy"),
embeds = testReport.GetMessages()
};
var content = new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json");
httpClient.PostAsync(Environment.GetEnvironmentVariable("TEST_HOOK"), content).Wait();
}
[Test]
public void HarassCalculator() {
testReport.CreateTest();
_webDriver.Navigate().GoToUrl(develop + "/harass-calculator");
var expectedTotalAlloyHarassment = 240;
try {
Website.HarassCalculatorPage
.SetWorkersLostToHarass(3)
.SetNumberOfTownHallsExisting(2)
.SetTownHallTravelTime(0, 30)
.GetTotalAlloyHarassment(out var foundTotalAlloyHarassment);
testReport.CheckPassed(expectedTotalAlloyHarassment.Equals(foundTotalAlloyHarassment),
TestMessage.CreateFailedMessage($"expectTotalAlloyHarassment of {expectedTotalAlloyHarassment} " +
"does not equal " +
$"foundTotalAlloyHarassment of {foundTotalAlloyHarassment} "));
}
catch (Exception e) {
testReport.CheckPassed(false,
TestMessage.CreateFailedMessage(e.StackTrace!));
}
}
[Test]
public void HarassCalculatorInformation() {
testReport.CreateTest();
_webDriver.Navigate().GoToUrl(develop + "/harass-calculator");
var expectedExampleTotalAlloyLoss = 720;
var expectedExampleWorkerCost = 300;
var expectedExampleMiningTimeCost = 420;
var expectedExampleTotalAlloyLossDifference = 300;
var expectedExampleTotalAlloyLossAccurate = 450;
var expectedExampleTotalAlloyLossAccurateDifference = 270;
Website.HarassCalculatorPage
.GetExampleTotalAlloyLoss(out var foundTotalAlloyLoss)
.GetExampleWorkerCost(out var foundExampleWorkerCost)
.GetExampleMiningTimeCost(out var foundExampleMiningTimeCost)
.GetExampleTotalAlloyLossAccurate(out var foundExampleTotalAlloyLossAccurate)
.GetExampleTotalAlloyLossDifference(out var foundGetExampleTotalAlloyLossDifference)
.GetExampleTotalAlloyLossAccurateDifference(out var foundExampleTotalAlloyLossAccurateDifference);
testReport.CheckPassed(expectedExampleTotalAlloyLoss.Equals(foundTotalAlloyLoss),
TestMessage.CreateFailedMessage($"expectedExampleTotalAlloyLoss of {expectedExampleTotalAlloyLoss} " +
"does not equal " +
$"foundTotalAlloyLoss of {foundTotalAlloyLoss} "));
testReport.CheckPassed(expectedExampleWorkerCost.Equals(foundExampleWorkerCost),
TestMessage.CreateFailedMessage($"expectedExampleWorkerCost of {expectedExampleWorkerCost} " +
"does not equal " +
$"foundExampleWorkerCost of {foundExampleWorkerCost} "));
testReport.CheckPassed(expectedExampleMiningTimeCost.Equals(foundExampleMiningTimeCost),
TestMessage.CreateFailedMessage($"expectedExampleMiningTimeCost of {expectedExampleMiningTimeCost} " +
"does not equal " +
$"foundExampleMiningTimeCost of {foundExampleMiningTimeCost} "));
testReport.CheckPassed(expectedExampleTotalAlloyLossAccurate.Equals(foundExampleTotalAlloyLossAccurate),
TestMessage.CreateFailedMessage(
$"expectedExampleTotalAlloyLossAccurate of {expectedExampleTotalAlloyLossAccurate} " +
"does not equal " +
$"foundExampleTotalAlloyLossAccurate of {foundExampleTotalAlloyLossAccurate} "));
testReport.CheckPassed(expectedExampleTotalAlloyLossDifference.Equals(foundGetExampleTotalAlloyLossDifference),
TestMessage.CreateFailedMessage(
$"expectedExampleTotalAlloyLossDifference of {expectedExampleTotalAlloyLossDifference} " +
"does not equal " +
$"foundGetExampleTotalAlloyLossDifference of {foundGetExampleTotalAlloyLossDifference} "));
testReport.CheckPassed(
expectedExampleTotalAlloyLossAccurateDifference.Equals(foundExampleTotalAlloyLossAccurateDifference),
TestMessage.CreateFailedMessage(
$"expectedExampleTotalAlloyLossAccurateDifference of {expectedExampleTotalAlloyLossAccurateDifference} " +
"does not equal " +
$"foundExampleTotalAlloyLossAccurateDifference of {foundExampleTotalAlloyLossAccurateDifference} "));
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace TestAutomation.Utils;
public class Test {
public string Name { get; set; } = "Name...";
public bool Result { get; set; } = true;
public IList<TestMessage> Messages { get; set; } = new List<TestMessage>();
}
+11
View File
@@ -0,0 +1,11 @@
namespace TestAutomation.Utils;
public class TestMessage {
public string Title { get; set; } = "Name...";
public string Description { get; set; } = "";
public string Color { get; set; } = "FFFFFF";
public static TestMessage CreateFailedMessage(string description) {
return new TestMessage { Title = "Check Failed", Description = description, Color = "FF0000" };
}
}
+57
View File
@@ -0,0 +1,57 @@
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace TestAutomation.Utils;
public class TestReport {
private List<Test> Tests { get; } = new();
[MethodImpl(MethodImplOptions.NoInlining)]
public Test CreateTest() {
var testName = new StackTrace().GetFrame(1)!.GetMethod()!.Name!;
Tests.Add(new Test { Name = testName });
return Tests.Last();
}
public void CheckPassed(bool passed, TestMessage message) {
if (passed) return;
Tests.Last().Result = false;
Tests.Last().Messages.Add(message);
}
public bool DidTestsPass() {
foreach (var test in Tests) {
if (test.Result) continue;
return false;
}
return true;
}
public List<object> GetMessages() {
if (DidTestsPass())
return new List<object> {
new {
title = "Passed",
color = int.Parse("00FF00", NumberStyles.HexNumber),
description = $"All {Tests.Count} tests passed."
}
};
var messageList = new List<object>();
foreach (var test in Tests)
foreach (var message in test.Messages)
messageList.Add(
new {
title = message.Title,
color = int.Parse(message.Color, NumberStyles.HexNumber),
description = message.Description
});
return messageList;
}
}
@@ -1,9 +1,7 @@
global using NUnit.Framework;
global using OpenQA.Selenium;
global using OpenQA.Selenium.Firefox;
global using OpenQA.Selenium.Chrome;
global using TestAutomation.Pages;
global using OpenQA.Selenium.Support.UI;
global using OpenQA.Selenium.Support;
global using OpenQA.Selenium.Support;
@@ -1,16 +1,16 @@
namespace TestAutomation;
namespace TestAutomation.Utils;
public class Website {
public IWebDriver WebDriver { get; }
public HarassCalculatorPage HarassCalculatorPage { get; }
public Website(IWebDriver webDriver) {
WebDriver = webDriver;
HarassCalculatorPage = new HarassCalculatorPage(this);
}
public IWebDriver WebDriver { get; }
public HarassCalculatorPage HarassCalculatorPage { get; }
public IWebElement Find(string byId) {
return WebDriver.FindElement(By.Id(byId));
}
@@ -18,17 +18,17 @@ public class Website {
public IList<IWebElement> FindChildren(string ofId, string tagname) {
return WebDriver.FindElements(By.CssSelector($"#{ofId} {tagname}"));
}
public string FindText(string byId) {
return WebDriver.FindElement(By.Id(byId)).Text;
}
public int FindInt(string byId) {
return int.Parse(WebDriver.FindElement(By.Id(byId)).Text);
}
public IWebElement EnterInput<T>(IWebElement element, T input) {
element.Clear();
element.SendKeys(input!.ToString());
@@ -36,7 +36,7 @@ public class Website {
return element;
}
public IWebElement EnterInput<T>(string byId, T input) {
var element = Find(byId);
element.Clear();