diff --git a/tests/Berufsschule_HAM.E2ETests/Berufsschule_HAM.E2ETests.csproj b/tests/Berufsschule_HAM.E2ETests/Berufsschule_HAM.E2ETests.csproj index 9c10485..c3d5cbc 100644 --- a/tests/Berufsschule_HAM.E2ETests/Berufsschule_HAM.E2ETests.csproj +++ b/tests/Berufsschule_HAM.E2ETests/Berufsschule_HAM.E2ETests.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/Berufsschule_HAM.E2ETests/Helper/AppHelper.cs b/tests/Berufsschule_HAM.E2ETests/Helper/AppHelper.cs index 13fd687..cbecef3 100644 --- a/tests/Berufsschule_HAM.E2ETests/Helper/AppHelper.cs +++ b/tests/Berufsschule_HAM.E2ETests/Helper/AppHelper.cs @@ -2,6 +2,8 @@ using System.Diagnostics; using System.Net; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Support.UI; +using SeleniumExtras.WaitHelpers; namespace Berufsschule_HAM.E2ETests.Helper; @@ -9,6 +11,7 @@ public static class AppHelper { public const string ServerUrl = "http://localhost:5275"; public static Uri ServerUri = new(ServerUrl); + public static int DefaultTimeout = 5; public static async Task StartApp(string appUrl) { var startInfo = new ProcessStartInfo @@ -123,4 +126,19 @@ public static class AppHelper throw; } } + + public static void AwaitVisible(ChromeDriver driver, By by) + { + Timeout(driver).Until(ExpectedConditions.ElementIsVisible(by)); + } + + public static WebDriverWait Timeout(ChromeDriver driver, int timeout) + { + return new(driver, TimeSpan.FromSeconds(timeout)); + } + + public static WebDriverWait Timeout(ChromeDriver driver) + { + return new(driver, TimeSpan.FromSeconds(DefaultTimeout)); + } } \ No newline at end of file diff --git a/tests/Berufsschule_HAM.E2ETests/Helper/GroupsHelper.cs b/tests/Berufsschule_HAM.E2ETests/Helper/GroupsHelper.cs index fdb2079..8c9782c 100644 --- a/tests/Berufsschule_HAM.E2ETests/Helper/GroupsHelper.cs +++ b/tests/Berufsschule_HAM.E2ETests/Helper/GroupsHelper.cs @@ -3,6 +3,7 @@ using System.Net; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; +using SeleniumExtras.WaitHelpers; namespace Berufsschule_HAM.E2ETests.Helper; @@ -13,7 +14,7 @@ public static class GroupsHelper NavigateToGroupsPage(driver); IWebElement createGroupButton = driver.FindElement(By.CssSelector("button[data-bs-target=\"#createGroupModal\"]")); createGroupButton.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(driver, By.Id("cn")); driver.FindElement(By.Id("cn")).SendKeys(cn); driver.FindElement(By.Id("displayname")).SendKeys(displayName); if (canInventorize) driver.FindElement(By.Id("canInventorize")).Click(); @@ -32,7 +33,7 @@ public static class GroupsHelper NavigateToGroupsPage(driver); IWebElement updateGroupButton = driver.FindElement(By.CssSelector($"button[data-group-id=\"{cn}\"].btn-update")); AppHelper.ScrollIntoViewAndClick(driver, updateGroupButton); - Thread.Sleep(500); + AppHelper.AwaitVisible(driver, By.CssSelector("input#groupId")); var cnInput = driver.FindElement(By.CssSelector("input#groupId")); cnInput.Click(); cnInput.Clear(); @@ -69,7 +70,7 @@ public static class GroupsHelper NavigateToGroupsPage(driver); IWebElement deleteButton = driver.FindElement(By.CssSelector($"button[data-group-id=\"{cn}\"].btn-delete")); AppHelper.ScrollIntoViewAndClick(driver, deleteButton); - Thread.Sleep(1000); + AppHelper.AwaitVisible(driver, By.Id("deleteModal")); driver.FindElement(By.Id("deleteModal")); IWebElement deleteConfirmButton = driver.FindElement(By.CssSelector("#deleteModal .btn-danger")); deleteConfirmButton.Click(); diff --git a/tests/Berufsschule_HAM.E2ETests/Helper/LocationsHelper.cs b/tests/Berufsschule_HAM.E2ETests/Helper/LocationsHelper.cs index 6d9f707..666b4de 100644 --- a/tests/Berufsschule_HAM.E2ETests/Helper/LocationsHelper.cs +++ b/tests/Berufsschule_HAM.E2ETests/Helper/LocationsHelper.cs @@ -13,7 +13,7 @@ public static class LocationsHelper NavigateToLocationsPage(driver); IWebElement createLocationButton = driver.FindElement(By.CssSelector("button[data-bs-target=\"#createModal\"]")); createLocationButton.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(driver, By.Id("createLocationName")); driver.FindElement(By.Id("createLocationName")).SendKeys(name); driver.FindElement(By.Id("createRoomNumber")).SendKeys(room); driver.FindElement(By.Id("createSeat")).SendKeys(seat); @@ -28,7 +28,7 @@ public static class LocationsHelper string selector = $"button.btn-warning[data-location-name=\"{name}\"][data-room-number=\"{room}\"][data-seat=\"{seat}\"]"; IWebElement updateLocationButton = driver.FindElement(By.CssSelector(selector)); AppHelper.ScrollIntoViewAndClick(driver, updateLocationButton); - Thread.Sleep(500); + AppHelper.AwaitVisible(driver, By.Id("editLocationName")); List ids = [["editLocationName", newName], ["editRoomNumber", newRoom], ["editSeat", newSeat]]; ids.ForEach(id => { @@ -48,7 +48,7 @@ public static class LocationsHelper NavigateToLocationsPage(driver); IWebElement deleteButton = driver.FindElement(By.CssSelector($"button.btn-danger[data-location-id=\"{cn}\"]")); AppHelper.ScrollIntoViewAndClick(driver, deleteButton); - Thread.Sleep(1000); + AppHelper.AwaitVisible(driver, By.Id("deleteModal")); driver.FindElement(By.Id("deleteModal")); IWebElement deleteConfirmButton = driver.FindElement(By.Id("deleteForm")); deleteConfirmButton.Click(); diff --git a/tests/Berufsschule_HAM.E2ETests/HomePageTests.cs b/tests/Berufsschule_HAM.E2ETests/HomePageTests.cs index 6620a6c..25c6a5a 100644 --- a/tests/Berufsschule_HAM.E2ETests/HomePageTests.cs +++ b/tests/Berufsschule_HAM.E2ETests/HomePageTests.cs @@ -1,6 +1,7 @@ using Berufsschule_HAM.E2ETests.Helper; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; +using SeleniumExtras.WaitHelpers; using System.Diagnostics; namespace Berufsschule_HAM.E2ETests; @@ -46,7 +47,7 @@ public class HomePageTests : IDisposable IWebElement logout = _driver.FindElement(By.CssSelector("a[href=\"/Home/Logout\"]")); Assert.True(logout.Displayed); logout.Click(); - Thread.Sleep(250); + AppHelper.Timeout(_driver).Until(ExpectedConditions.UrlContains("/Home/Login")); Assert.Contains("/Home/Login", _driver.Url); } diff --git a/tests/Berufsschule_HAM.E2ETests/InventoryPageTests.cs b/tests/Berufsschule_HAM.E2ETests/InventoryPageTests.cs index 48e1d5c..d82bc48 100644 --- a/tests/Berufsschule_HAM.E2ETests/InventoryPageTests.cs +++ b/tests/Berufsschule_HAM.E2ETests/InventoryPageTests.cs @@ -1,6 +1,7 @@ using Berufsschule_HAM.E2ETests.Helper; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; +using SeleniumExtras.WaitHelpers; using System.Diagnostics; namespace Berufsschule_HAM.E2ETests; @@ -41,11 +42,10 @@ public class InventoryPageTests : IDisposable assetIdInputField.SendKeys("1"); IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton")); scanBarcode.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal")); IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal")); Assert.True(viewModal.Displayed); Assert.True(_driver.FindElement(By.CssSelector("svg[class=\"form-control\"]")).Displayed); - Thread.Sleep(500); Assert.NotEmpty(_driver.FindElement(By.Id("detailName")).GetAttribute("value") ?? ""); Assert.NotEmpty(_driver.FindElement(By.Id("detailLocation")).GetAttribute("value") ?? ""); @@ -71,12 +71,11 @@ public class InventoryPageTests : IDisposable assetIdInputField.SendKeys("1"); IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton")); scanBarcode.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal")); IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal")); Assert.True(viewModal.Displayed); IWebElement okButton = _driver.FindElement(By.CssSelector("#viewAssetModal button.btn.btn-primary[data-bs-dismiss=\"modal\"]")); okButton.Click(); - Thread.Sleep(500); Assert.True(AppHelper.TryRetryFindSuccessToast(_driver)); } @@ -89,15 +88,14 @@ public class InventoryPageTests : IDisposable assetIdInputField.SendKeys("1"); IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton")); scanBarcode.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal")); IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal")); Assert.True(viewModal.Displayed); IWebElement updateButton = _driver.FindElement(By.CssSelector("#viewAssetModal button.btn.btn-warning[data-bs-dismiss=\"modal\"]")); updateButton.Click(); - Thread.Sleep(1000); + AppHelper.AwaitVisible(_driver, By.CssSelector("#updateAssetModal button.btn.btn-warning[type=\"submit\"]")); IWebElement okButton = _driver.FindElement(By.CssSelector("#updateAssetModal button.btn.btn-warning[type=\"submit\"]")); okButton.Click(); - Thread.Sleep(500); Assert.True(AppHelper.TryRetryFindSuccessToast(_driver)); } @@ -108,7 +106,7 @@ public class InventoryPageTests : IDisposable _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory")); IWebElement link = _driver.FindElement(By.CssSelector("a[href=\"/Home/Assets?CreateModal=true\"]")); link.Click(); - Thread.Sleep(500); + AppHelper.Timeout(_driver).Until(ExpectedConditions.UrlContains("/Home/Assets")); Assert.Contains("/Home/Assets", _driver.Url); } @@ -119,7 +117,7 @@ public class InventoryPageTests : IDisposable _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory")); IWebElement openPrintModalButton = _driver.FindElement(By.Id("openPrintModal")); openPrintModalButton.Click(); - Thread.Sleep(500); + AppHelper.AwaitVisible(_driver, By.Id("printModal")); IWebElement printModal = _driver.FindElement(By.Id("printModal")); Assert.True(printModal.Displayed); }