test(BrokenLinks) Now checking for broken links on a few pages
This commit is contained in:
@@ -11,18 +11,63 @@ public class TestReport
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public Test CreateTest()
|
||||
{
|
||||
var testName = new StackTrace().GetFrame(1)!.GetMethod()!.Name!;
|
||||
Tests.Add(new Test { Name = testName });
|
||||
Tests.Add(new Test
|
||||
{
|
||||
Name = TestContext.CurrentContext.Test.Name
|
||||
});
|
||||
return Tests.Last();
|
||||
}
|
||||
|
||||
public void ThrowErrors()
|
||||
{
|
||||
if (!Tests.Last().Result)
|
||||
{
|
||||
string messages = string.Join("\n", Tests.Last().Messages.Select(x => x.Description).ToList());
|
||||
|
||||
throw new Exception(
|
||||
$"{Tests.Last().Name} test failed with {Tests.Last().Messages.Count} messages.\n\n{messages}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task VerifyLinks(BasePage page)
|
||||
{
|
||||
foreach (var link in page.GetLinks())
|
||||
try
|
||||
{
|
||||
if(link.StartsWith("mailto")) continue;
|
||||
|
||||
using var client = new HttpClient();
|
||||
var response = await client.GetAsync(link);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
CheckPassed(false,
|
||||
new TestMessage
|
||||
{
|
||||
Color = "red", Title = "Bad Link",
|
||||
Description = $"{link} failed on page {page.Url} with status code {response.StatusCode}"
|
||||
});
|
||||
Console.WriteLine(response.StatusCode.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
CheckPassed(false,
|
||||
new TestMessage
|
||||
{
|
||||
Color = "red", Title = "Bad Link",
|
||||
Description = $"{link} failed on page {page.Url} with stacktrace {e.StackTrace}"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void CheckPassed(bool passed, TestMessage message)
|
||||
{
|
||||
if (passed) return;
|
||||
Tests.Last().Result = false;
|
||||
Tests.Last().Messages.Add(message);
|
||||
|
||||
throw new Exception(message.Description);
|
||||
}
|
||||
|
||||
public bool DidTestsPass()
|
||||
|
||||
@@ -7,11 +7,24 @@ namespace TestAutomation.Utils;
|
||||
|
||||
public class Website
|
||||
{
|
||||
public static readonly DeploymentType DeploymentType =
|
||||
Environment.GetEnvironmentVariable("TEST_HOOK")!.Contains("localhost")
|
||||
? DeploymentType.Local
|
||||
: DeploymentType.Dev;
|
||||
|
||||
public static readonly string Url =
|
||||
DeploymentType.Equals(DeploymentType.Dev)
|
||||
? "https://calm-mud-04916b210.1.azurestaticapps.net"
|
||||
: "https://localhost:7234";
|
||||
|
||||
public readonly ScreenType ScreenType = ScreenType.Desktop;
|
||||
|
||||
public Website(IWebDriver webDriver)
|
||||
public TestReport TestReport { get; set; }
|
||||
|
||||
public Website(IWebDriver webDriver, TestReport testReport)
|
||||
{
|
||||
WebDriver = webDriver;
|
||||
TestReport = testReport;
|
||||
|
||||
// Pages
|
||||
HarassCalculatorPage = new HarassCalculatorPage(this);
|
||||
@@ -100,6 +113,16 @@ public class Website
|
||||
}
|
||||
}
|
||||
|
||||
public ReadOnlyCollection<IWebElement> FindAllWithTag(string tag)
|
||||
{
|
||||
return WebDriver.FindElements(By.TagName(tag));
|
||||
}
|
||||
|
||||
public ReadOnlyCollection<IWebElement> FindAllWithTag(IWebElement parent, string tag)
|
||||
{
|
||||
return parent.FindElements(By.TagName(tag));
|
||||
}
|
||||
|
||||
|
||||
public IWebElement FindButtonWithLabel(string label)
|
||||
{
|
||||
@@ -177,4 +200,17 @@ public class Website
|
||||
{
|
||||
return Find(byId).Text;
|
||||
}
|
||||
|
||||
public Website Goto()
|
||||
{
|
||||
WebDriver.Navigate().GoToUrl($"{Url}");
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Goto(string path)
|
||||
{
|
||||
var url = $"{Url}/{path}";
|
||||
|
||||
WebDriver.Navigate().GoToUrl($"{url}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user