diff --git a/BTCPayServer.Tests/CheckoutUITests.cs b/BTCPayServer.Tests/CheckoutUITests.cs index fc7730e18..46531aea9 100644 --- a/BTCPayServer.Tests/CheckoutUITests.cs +++ b/BTCPayServer.Tests/CheckoutUITests.cs @@ -71,7 +71,7 @@ namespace BTCPayServer.Tests } catch { } - s.Driver.AssertElementNotFound(By.Id("emailAddressFormInput")); + s.Driver.AssertElementNotFound(By.Id("emailAddressFormInput")); s.Driver.Navigate().Refresh(); s.Driver.AssertElementNotFound(By.Id("emailAddressFormInput")); } @@ -92,7 +92,7 @@ namespace BTCPayServer.Tests s.GoToInvoiceCheckout(invoiceId); Assert.True(s.Driver.FindElement(By.Id("DefaultLang")).FindElements(By.TagName("option")).Count > 1); var payWithTextEnglish = s.Driver.FindElement(By.Id("pay-with-text")).Text; - + var prettyDropdown = s.Driver.FindElement(By.Id("prettydropdown-DefaultLang")); prettyDropdown.Click(); await Task.Delay(200); @@ -100,13 +100,13 @@ namespace BTCPayServer.Tests await Task.Delay(1000); Assert.NotEqual(payWithTextEnglish, s.Driver.FindElement(By.Id("pay-with-text")).Text); s.Driver.Navigate().GoToUrl(s.Driver.Url + "?lang=da-DK"); - + Assert.NotEqual(payWithTextEnglish, s.Driver.FindElement(By.Id("pay-with-text")).Text); - + s.Driver.Quit(); } } - + [Fact(Timeout = TestTimeout)] [Trait("Altcoins", "Altcoins")] [Trait("Lightning", "Lightning")] @@ -121,7 +121,7 @@ namespace BTCPayServer.Tests s.RegisterNewUser(); var store = s.CreateNewStore(); s.AddDerivationScheme("BTC"); - + //check that there is no dropdown since only one payment method is set var invoiceId = s.CreateInvoice(store.storeName, 10, "USD", "a@g.com"); s.GoToInvoiceCheckout(invoiceId); @@ -129,33 +129,31 @@ namespace BTCPayServer.Tests s.GoToHome(); s.GoToStore(store.storeId); s.AddDerivationScheme("LTC"); - s.AddLightningNode("BTC",LightningConnectionType.CLightning); + s.AddLightningNode("BTC", LightningConnectionType.CLightning); //there should be three now invoiceId = s.CreateInvoice(store.storeName, 10, "USD", "a@g.com"); s.GoToInvoiceCheckout(invoiceId); - var currencyDropdownButton = s.Driver.FindElement(By.ClassName("payment__currencies")); + var currencyDropdownButton = s.Driver.WaitForElement(By.ClassName("payment__currencies")); Assert.Contains("BTC", currencyDropdownButton.Text); currencyDropdownButton.Click(); - + var elements = s.Driver.FindElement(By.ClassName("vex-content")).FindElements(By.ClassName("vexmenuitem")); Assert.Equal(3, elements.Count); elements.Single(element => element.Text.Contains("LTC")).Click(); - Thread.Sleep(1000); - currencyDropdownButton = s.Driver.FindElement(By.ClassName("payment__currencies")); + currencyDropdownButton = s.Driver.WaitForElement(By.ClassName("payment__currencies")); Assert.Contains("LTC", currencyDropdownButton.Text); currencyDropdownButton.Click(); elements = s.Driver.FindElement(By.ClassName("vex-content")).FindElements(By.ClassName("vexmenuitem")); elements.Single(element => element.Text.Contains("Lightning")).Click(); - Thread.Sleep(1000); - currencyDropdownButton = s.Driver.FindElement(By.ClassName("payment__currencies")); + currencyDropdownButton = s.Driver.WaitForElement(By.ClassName("payment__currencies")); Assert.Contains("Lightning", currencyDropdownButton.Text); - + s.Driver.Quit(); } } - + [Fact(Timeout = TestTimeout)] [Trait("Lightning", "Lightning")] public async Task CanUseLightningSatsFeature() @@ -171,12 +169,12 @@ namespace BTCPayServer.Tests s.GoToStore(store.storeId, StoreNavPages.Checkout); s.SetCheckbox(s, "LightningAmountInSatoshi", true); var command = s.Driver.FindElement(By.Name("command")); - + command.ForceClick(); var invoiceId = s.CreateInvoice(store.storeName, 10, "USD", "a@g.com"); s.GoToInvoiceCheckout(invoiceId); Assert.Contains("Sats", s.Driver.FindElement(By.ClassName("payment__currencies_noborder")).Text); - + } } @@ -218,4 +216,30 @@ namespace BTCPayServer.Tests } } } + + public static class SeleniumExtensions + { + /// + /// Utility method to wait until timeout for element to be present (optionally displayed) + /// + /// Wait context + /// How we search for element + /// Flag to wait for element to be displayed or just present + /// How long to wait for element to be present/displayed + /// Element we were waiting for + public static IWebElement WaitForElement(this IWebDriver context, By by, bool displayed = true, uint timeout = 3) + { + var wait = new DefaultWait(context); + wait.Timeout = TimeSpan.FromSeconds(timeout); + wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); + return wait.Until(ctx => + { + var elem = ctx.FindElement(by); + if (displayed && !elem.Displayed) + return null; + + return elem; + }); + } + } }