BasicTestAppTestBase.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.Linq;
  4. using BasicTestApp;
  5. using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
  6. using Microsoft.AspNetCore.E2ETesting;
  7. using OpenQA.Selenium;
  8. using OpenQA.Selenium.Support.UI;
  9. using Xunit.Abstractions;
  10. namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure
  11. {
  12. public class BasicTestAppTestBase : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
  13. {
  14. public string ServerPathBase
  15. => "/subdir" + (_serverFixture.ExecutionMode == ExecutionMode.Server ? "#server" : "");
  16. public BasicTestAppTestBase(
  17. BrowserFixture browserFixture,
  18. ToggleExecutionModeServerFixture<Program> serverFixture,
  19. ITestOutputHelper output)
  20. : base(browserFixture, serverFixture, output)
  21. {
  22. serverFixture.PathBase = ServerPathBase;
  23. }
  24. protected IWebElement MountTestComponent<TComponent>() where TComponent : IComponent
  25. {
  26. var componentTypeName = typeof(TComponent).FullName;
  27. var testSelector = WaitUntilTestSelectorReady();
  28. testSelector.SelectByValue("none");
  29. testSelector.SelectByValue(componentTypeName);
  30. return Browser.FindElement(By.TagName("app"));
  31. }
  32. protected SelectElement WaitUntilTestSelectorReady()
  33. {
  34. var elemToFind = By.CssSelector("#test-selector > select");
  35. WaitUntilExists(elemToFind, timeoutSeconds: 30, throwOnError: true);
  36. return new SelectElement(Browser.FindElement(elemToFind));
  37. }
  38. protected void SignInAs(string usernameOrNull, string rolesOrNull, bool useSeparateTab = false)
  39. {
  40. const string authenticationPageUrl = "/Authentication";
  41. var baseRelativeUri = usernameOrNull == null
  42. ? $"{authenticationPageUrl}?signout=true"
  43. : $"{authenticationPageUrl}?username={usernameOrNull}&roles={rolesOrNull}";
  44. if (useSeparateTab)
  45. {
  46. // Some tests need to change the authentication state without discarding the
  47. // original page, but this adds several seconds of delay
  48. var javascript = (IJavaScriptExecutor)Browser;
  49. var originalWindow = Browser.CurrentWindowHandle;
  50. javascript.ExecuteScript("window.open()");
  51. Browser.SwitchTo().Window(Browser.WindowHandles.Last());
  52. Navigate(baseRelativeUri);
  53. WaitUntilExists(By.CssSelector("h1#authentication"));
  54. javascript.ExecuteScript("window.close()");
  55. Browser.SwitchTo().Window(originalWindow);
  56. }
  57. else
  58. {
  59. Navigate(baseRelativeUri);
  60. WaitUntilExists(By.CssSelector("h1#authentication"));
  61. }
  62. }
  63. }
  64. }