2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2023-07-21 02:08:32 +02:00
|
|
|
using System.Diagnostics;
|
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;
|
2022-12-08 05:16:18 +01:00
|
|
|
using BTCPayServer.Services.Wallets;
|
2019-05-14 12:13:55 +02:00
|
|
|
using BTCPayServer.Tests.Logging;
|
2019-05-12 07:51:24 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-12-08 05:16:18 +01:00
|
|
|
using NBXplorer.DerivationStrategy;
|
|
|
|
using NBXplorer.Models;
|
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
|
|
|
|
{
|
2022-12-08 05:16:18 +01:00
|
|
|
public static Task<KeyPathInformation> ReserveAddressAsync(this BTCPayWallet wallet, DerivationStrategyBase derivationStrategyBase)
|
|
|
|
{
|
|
|
|
return wallet.ReserveAddressAsync(null, derivationStrategyBase, "test");
|
|
|
|
}
|
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)
|
|
|
|
{
|
2022-01-07 09:34:06 +01:00
|
|
|
if (driver.PageSource.Contains("alert-danger"))
|
|
|
|
{
|
|
|
|
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}");
|
|
|
|
}
|
2022-01-16 04:38:47 +01:00
|
|
|
Assert.DoesNotContain("errors", driver.Url);
|
|
|
|
Assert.DoesNotContain("Error", driver.Title, StringComparison.OrdinalIgnoreCase);
|
2019-05-13 10:59:15 +02:00
|
|
|
}
|
2021-12-31 08:59:02 +01: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...
|
2021-10-05 08:47:57 +02:00
|
|
|
public static IWebElement FindElementUntilNotStaled(this IWebDriver driver, By by, Action<IWebElement> act)
|
2021-09-28 04:22:35 +02:00
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
retry:
|
2021-09-28 04:22:35 +02:00
|
|
|
try
|
|
|
|
{
|
2021-10-05 08:47:57 +02:00
|
|
|
var el = driver.FindElement(by);
|
|
|
|
act(el);
|
|
|
|
return el;
|
2021-09-28 04:22:35 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2023-11-28 15:20:03 +01:00
|
|
|
Assert.Fail("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)
|
|
|
|
{
|
2021-12-31 08:59:02 +01:00
|
|
|
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-02-17 04:14:29 +01:00
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
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-12-31 08:59:02 +01:00
|
|
|
|
2021-12-24 09:27:00 +01:00
|
|
|
public static void SetAttribute(this IWebDriver driver, string element, string attribute, string value)
|
|
|
|
{
|
|
|
|
driver.ExecuteJavaScript($"document.getElementById('{element}').setAttribute('{attribute}', '{value}')");
|
|
|
|
}
|
|
|
|
public static void InvokeJSFunction(this IWebDriver driver, string element, string funcName)
|
|
|
|
{
|
|
|
|
driver.ExecuteJavaScript($"document.getElementById('{element}').{funcName}()");
|
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
|
2023-06-22 09:09:53 +02:00
|
|
|
public static void WaitWalletTransactionsLoaded(this IWebDriver driver)
|
|
|
|
{
|
|
|
|
var wait = new WebDriverWait(driver, SeleniumTester.ImplicitWait);
|
|
|
|
wait.UntilJsIsReady();
|
|
|
|
wait.Until(d => d.WaitForElement(By.CssSelector("#WalletTransactions[data-loaded='true']")));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2022-11-24 00:53:32 +01:00
|
|
|
|
|
|
|
public static void FillIn(this IWebElement el, string text)
|
|
|
|
{
|
|
|
|
el.Clear();
|
|
|
|
el.SendKeys(text);
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-03-01 19:39:25 +01:00
|
|
|
public static void ScrollTo(this IWebDriver driver, IWebElement element)
|
|
|
|
{
|
|
|
|
driver.ExecuteJavaScript("arguments[0].scrollIntoView();", element);
|
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
|
2021-11-05 05:06:35 +01:00
|
|
|
public static void ScrollTo(this IWebDriver driver, By selector)
|
|
|
|
{
|
2022-03-01 19:39:25 +01:00
|
|
|
ScrollTo(driver, driver.FindElement(selector));
|
2021-11-05 05:06:35 +01:00
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-06-02 10:08:55 +02:00
|
|
|
public static void WaitUntilAvailable(this IWebDriver driver, By selector, TimeSpan? waitTime = null)
|
2021-02-17 04:14:29 +01:00
|
|
|
{
|
2022-02-07 10:37:45 +01:00
|
|
|
// Try fast path
|
2022-06-02 10:08:55 +02:00
|
|
|
var wait = new WebDriverWait(driver, SeleniumTester.ImplicitWait);
|
2022-02-07 10:37:45 +01:00
|
|
|
try
|
|
|
|
{
|
2022-06-02 10:08:55 +02:00
|
|
|
var el = driver.FindElement(selector);
|
|
|
|
wait.Until(_ => el.Displayed && el.Enabled);
|
2022-02-07 10:37:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
// Sometimes, selenium complain, so we enter hack territory
|
2021-02-17 04:14:29 +01:00
|
|
|
wait.UntilJsIsReady();
|
|
|
|
|
2021-10-31 06:20:31 +01:00
|
|
|
int retriesLeft = 4;
|
2023-01-06 14:18:07 +01:00
|
|
|
retry:
|
2021-10-31 06:20:31 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var el = driver.FindElement(selector);
|
2022-06-02 10:08:55 +02:00
|
|
|
wait.Until(_ => el.Displayed && el.Enabled);
|
2021-11-05 05:06:35 +01:00
|
|
|
driver.ScrollTo(selector);
|
2022-06-02 10:08:55 +02:00
|
|
|
driver.FindElement(selector);
|
2021-10-31 06:20:31 +01:00
|
|
|
}
|
2022-06-02 10:08:55 +02:00
|
|
|
catch (NoSuchElementException) when (retriesLeft > 0)
|
2021-10-31 06:20:31 +01:00
|
|
|
{
|
|
|
|
retriesLeft--;
|
2023-01-06 14:18:07 +01:00
|
|
|
if (waitTime != null)
|
|
|
|
Thread.Sleep(waitTime.Value);
|
2021-10-31 06:20:31 +01:00
|
|
|
goto retry;
|
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
wait.UntilJsIsReady();
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-06-02 10:08:55 +02:00
|
|
|
public static void WaitForAndClick(this IWebDriver driver, By selector)
|
|
|
|
{
|
|
|
|
driver.WaitUntilAvailable(selector);
|
|
|
|
driver.FindElement(selector).Click();
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-06-28 16:58:03 +02:00
|
|
|
public static bool ElementDoesNotExist(this IWebDriver driver, By selector)
|
|
|
|
{
|
2023-12-06 01:17:58 +01:00
|
|
|
Assert.Throws<NoSuchElementException>(
|
|
|
|
[DebuggerStepThrough]
|
|
|
|
() =>
|
2022-06-28 16:58:03 +02:00
|
|
|
{
|
|
|
|
driver.FindElement(selector);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-17 04:14:29 +01:00
|
|
|
|
2023-04-04 03:45:40 +02:00
|
|
|
public static bool SetCheckbox(this IWebDriver driver, By selector, bool value)
|
2021-02-17 04:14:29 +01:00
|
|
|
{
|
|
|
|
var element = driver.FindElement(selector);
|
|
|
|
if (value != element.Selected)
|
2023-04-04 03:45:40 +02:00
|
|
|
{
|
2022-02-07 10:37:45 +01:00
|
|
|
driver.WaitForAndClick(selector);
|
2023-04-04 03:45:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-02-17 04:14:29 +01:00
|
|
|
}
|
2019-05-12 07:51:24 +02:00
|
|
|
}
|
|
|
|
}
|