2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2019-10-12 13:35:30 +02:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
2019-05-12 07:51:24 +02:00
|
|
|
using System.Threading.Tasks;
|
2019-05-14 12:13:55 +02:00
|
|
|
using BTCPayServer.Tests.Logging;
|
2019-05-12 07:51:24 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-06-28 10:15:42 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2019-05-13 10:59:15 +02:00
|
|
|
using OpenQA.Selenium;
|
2021-05-19 04:39:27 +02:00
|
|
|
using OpenQA.Selenium.Support.Extensions;
|
2021-02-17 04:14:29 +01:00
|
|
|
using OpenQA.Selenium.Support.UI;
|
2019-05-12 07:51:24 +02:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public static class Extensions
|
|
|
|
{
|
2021-01-25 14:10:19 +01:00
|
|
|
private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
|
|
|
|
public static string ToJson(this object o) => JsonConvert.SerializeObject(o, Formatting.None, JsonSettings);
|
2021-01-14 10:57:17 +01:00
|
|
|
|
2021-01-22 17:49:26 +01:00
|
|
|
public static void LogIn(this SeleniumTester s, string email)
|
2021-01-14 10:57:17 +01:00
|
|
|
{
|
2021-01-22 17:49:26 +01:00
|
|
|
s.Driver.FindElement(By.Id("Email")).SendKeys(email);
|
|
|
|
s.Driver.FindElement(By.Id("Password")).SendKeys("123456");
|
|
|
|
s.Driver.FindElement(By.Id("LoginButton")).Click();
|
|
|
|
s.Driver.AssertNoError();
|
2021-01-14 10:57:17 +01:00
|
|
|
}
|
2021-01-22 17:49:26 +01:00
|
|
|
|
2019-05-13 10:59:15 +02:00
|
|
|
public static void AssertNoError(this IWebDriver driver)
|
|
|
|
{
|
2021-05-19 04:39:27 +02:00
|
|
|
Assert.NotEmpty(driver.FindElements(By.ClassName("navbar-brand")));
|
|
|
|
if (!driver.PageSource.Contains("alert-danger")) return;
|
|
|
|
foreach (var dangerAlert in driver.FindElements(By.ClassName("alert-danger")))
|
|
|
|
Assert.False(dangerAlert.Displayed, $"No alert should be displayed, but found this on {driver.Url}: {dangerAlert.Text}");
|
2019-05-13 10:59:15 +02:00
|
|
|
}
|
2021-05-19 04:39:27 +02:00
|
|
|
|
2019-05-12 07:51:24 +02:00
|
|
|
public static T AssertViewModel<T>(this IActionResult result)
|
|
|
|
{
|
|
|
|
Assert.NotNull(result);
|
|
|
|
var vr = Assert.IsType<ViewResult>(result);
|
|
|
|
return Assert.IsType<T>(vr.Model);
|
|
|
|
}
|
|
|
|
public static async Task<T> AssertViewModelAsync<T>(this Task<IActionResult> task)
|
|
|
|
{
|
|
|
|
var result = await task;
|
|
|
|
Assert.NotNull(result);
|
|
|
|
var vr = Assert.IsType<ViewResult>(result);
|
|
|
|
return Assert.IsType<T>(vr.Model);
|
|
|
|
}
|
2019-09-10 10:03:24 +02:00
|
|
|
|
2021-09-28 04:22:35 +02:00
|
|
|
// Sometimes, selenium is flaky...
|
|
|
|
public static IWebElement FindElementUntilNotStaled(this IWebDriver driver, By by)
|
|
|
|
{
|
|
|
|
retry:
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return driver.FindElement(by);
|
|
|
|
}
|
|
|
|
catch (StaleElementReferenceException)
|
|
|
|
{
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 13:35:30 +02:00
|
|
|
public static void AssertElementNotFound(this IWebDriver driver, By by)
|
2019-09-10 10:03:24 +02:00
|
|
|
{
|
2019-10-12 13:35:30 +02:00
|
|
|
DateTimeOffset now = DateTimeOffset.Now;
|
|
|
|
var wait = SeleniumTester.ImplicitWait;
|
|
|
|
|
|
|
|
while (DateTimeOffset.UtcNow - now < wait)
|
2019-09-10 10:03:24 +02:00
|
|
|
{
|
2019-10-12 13:35:30 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var webElement = driver.FindElement(by);
|
|
|
|
if (!webElement.Displayed)
|
|
|
|
return;
|
|
|
|
}
|
2020-03-11 16:51:33 +01:00
|
|
|
catch (NoSuchWindowException)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-10-12 13:35:30 +02:00
|
|
|
catch (NoSuchElementException)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Thread.Sleep(50);
|
2019-09-10 10:03:24 +02:00
|
|
|
}
|
2019-10-12 13:35:30 +02:00
|
|
|
Assert.False(true, "Elements was found");
|
2019-09-10 10:03:24 +02:00
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
|
|
|
|
public static void UntilJsIsReady(this WebDriverWait wait)
|
|
|
|
{
|
|
|
|
wait.Until(d=>((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
|
|
|
|
wait.Until(d=>((IJavaScriptExecutor)d).ExecuteScript("return typeof(jQuery) === 'undefined' || jQuery.active === 0").Equals(true));
|
|
|
|
}
|
2021-05-19 04:39:27 +02:00
|
|
|
|
|
|
|
// Open collapse via JS, because if we click the link it triggers the toggle animation.
|
|
|
|
// This leads to Selenium trying to click the button while it is moving resulting in an error.
|
|
|
|
public static void ToggleCollapse(this IWebDriver driver, string collapseId)
|
|
|
|
{
|
|
|
|
driver.ExecuteJavaScript($"document.getElementById('{collapseId}').classList.add('show')");
|
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
|
|
|
|
public static IWebElement WaitForElement(this IWebDriver driver, By selector)
|
|
|
|
{
|
|
|
|
var wait = new WebDriverWait(driver, SeleniumTester.ImplicitWait);
|
|
|
|
wait.UntilJsIsReady();
|
|
|
|
|
|
|
|
var el = driver.FindElement(selector);
|
|
|
|
wait.Until(d => el.Displayed);
|
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void WaitForAndClick(this IWebDriver driver, By selector)
|
|
|
|
{
|
|
|
|
var wait = new WebDriverWait(driver, SeleniumTester.ImplicitWait);
|
|
|
|
wait.UntilJsIsReady();
|
|
|
|
|
|
|
|
var el = driver.FindElement(selector);
|
|
|
|
wait.Until(d => el.Displayed && el.Enabled);
|
|
|
|
el.Click();
|
|
|
|
|
|
|
|
wait.UntilJsIsReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void SetCheckbox(this IWebDriver driver, By selector, bool value)
|
|
|
|
{
|
|
|
|
var element = driver.FindElement(selector);
|
|
|
|
if ((value && !element.Selected) || (!value && element.Selected))
|
|
|
|
{
|
|
|
|
driver.WaitForAndClick(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != element.Selected)
|
|
|
|
{
|
|
|
|
Logs.Tester.LogInformation("SetCheckbox recursion, trying to click again");
|
|
|
|
driver.SetCheckbox(selector, value);
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 07:51:24 +02:00
|
|
|
}
|
|
|
|
}
|