From 0f1d7177d20b08ce7738a8640e93551b31aecfbf Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Wed, 1 May 2024 14:00:28 +0300 Subject: [PATCH] Add UITestOptions - Make properties for IBrowser, IPage, IElement getter & setter --- src/OrchardCoreContrib.Testing.UI/Browser.cs | 21 ++++++++------- .../BrowserFactory.cs | 25 ++++++++++++------ src/OrchardCoreContrib.Testing.UI/Element.cs | 16 ++++++++---- src/OrchardCoreContrib.Testing.UI/IBrowser.cs | 13 +++------- src/OrchardCoreContrib.Testing.UI/IElement.cs | 6 ++--- src/OrchardCoreContrib.Testing.UI/IPage.cs | 8 +++--- src/OrchardCoreContrib.Testing.UI/Page.cs | 23 ++++++++++++---- src/OrchardCoreContrib.Testing.UI/UITest.cs | 11 ++++++-- .../UITestBase.cs | 2 ++ .../UITestOfT.cs | 8 +++++- .../UITestOptions.cs | 17 ++++++++++++ .../BrowserFactoryTests.cs | 26 ++++++++++++------- .../BrowserTests.cs | 5 ++-- .../ElementTests.cs | 22 +++++----------- .../UITestOfTTests.cs | 2 +- 15 files changed, 130 insertions(+), 75 deletions(-) create mode 100644 src/OrchardCoreContrib.Testing.UI/UITestOptions.cs diff --git a/src/OrchardCoreContrib.Testing.UI/Browser.cs b/src/OrchardCoreContrib.Testing.UI/Browser.cs index 913d242..3162664 100644 --- a/src/OrchardCoreContrib.Testing.UI/Browser.cs +++ b/src/OrchardCoreContrib.Testing.UI/Browser.cs @@ -11,27 +11,30 @@ namespace OrchardCoreContrib.Testing.UI; /// The . /// The . /// Whether to run browser in headless mode. -public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor, BrowserType type, bool headless) : IBrowser +public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor) : IBrowser { /// public PlaywrightBrowser InnerBrowser => playwrightBrowserAccessor.PlaywrightBrowser; /// - public bool Headless => headless; + public BrowserType Type { get; set; } /// - public BrowserType Type => type; - - /// - public string Version => InnerBrowser.Version; + public string Version { get; set; } = playwrightBrowserAccessor.PlaywrightBrowser.Version; /// public async Task OpenPageAsync(string url) { - var page = await InnerBrowser.NewPageAsync(); + var playwrightPage = await InnerBrowser.NewPageAsync(); + + await playwrightPage.GotoAsync(url); - await page.GotoAsync(url); + var page = new Page(new PlaywrightPageAccessor(playwrightPage)) + { + Title = await playwrightPage.TitleAsync(), + Content = await playwrightPage.ContentAsync() + }; - return new Page(new PlaywrightPageAccessor(page)); + return page; } } diff --git a/src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs b/src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs index da7797a..fbc951f 100644 --- a/src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs +++ b/src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs @@ -16,20 +16,29 @@ public static class BrowserFactory /// Creates a new instance of with a given browser type. /// /// The . - /// The browser type in which will be created. - /// Whether the browser runs in headless mode or not. + /// The . /// An instance of . /// - public static async Task CreateAsync(IPlaywright playwright, BrowserType browserType, bool headless) + public static async Task CreateAsync(IPlaywright playwright, UITestOptions testOptions) { - var browser = browserType switch + var browser = testOptions.BrowserType switch { - BrowserType.Edge => _edgeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Channel = "msedge", Headless = headless }), - BrowserType.Chrome => _chromeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless }), - BrowserType.Firefox => _fireFoxBrowser ?? await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless }), + BrowserType.Edge => _edgeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions + { + Channel = "msedge", + Headless = testOptions.Headless + }), + BrowserType.Chrome => _chromeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions + { + Headless = testOptions.Headless + }), + BrowserType.Firefox => _fireFoxBrowser ?? await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions + { + Headless = testOptions.Headless + }), _ => throw new NotSupportedException() }; - return new Browser(new PlaywrightBrowserAccessor(browser), browserType, headless); + return new Browser(new PlaywrightBrowserAccessor(browser)) { Type = testOptions.BrowserType }; } } diff --git a/src/OrchardCoreContrib.Testing.UI/Element.cs b/src/OrchardCoreContrib.Testing.UI/Element.cs index 77e32d8..86a29b8 100644 --- a/src/OrchardCoreContrib.Testing.UI/Element.cs +++ b/src/OrchardCoreContrib.Testing.UI/Element.cs @@ -10,20 +10,26 @@ namespace OrchardCoreContrib.Testing.UI; public class Element(ILocator locator) : IElement { /// - public string InnerText => locator.InnerTextAsync().GetAwaiter().GetResult(); + public string InnerText { get; set; } /// - public string InnerHtml => locator.InnerHTMLAsync().GetAwaiter().GetResult(); + public string InnerHtml { get; set; } /// - public bool Enabled => locator.IsEnabledAsync().GetAwaiter().GetResult(); + public bool Enabled { get; set; } /// - public bool Visible => locator.IsVisibleAsync().GetAwaiter().GetResult(); + public bool Visible { get; set; } /// public async Task ClickAsync() => await locator.ClickAsync(); /// - public async Task TypeAsync(string text) => await locator.FillAsync(text); + public async Task TypeAsync(string text) + { + await locator.FillAsync(text); + + InnerText = await locator.InnerTextAsync(); + InnerHtml = await locator.InnerHTMLAsync(); + } } diff --git a/src/OrchardCoreContrib.Testing.UI/IBrowser.cs b/src/OrchardCoreContrib.Testing.UI/IBrowser.cs index 2c71e5c..11946e4 100644 --- a/src/OrchardCoreContrib.Testing.UI/IBrowser.cs +++ b/src/OrchardCoreContrib.Testing.UI/IBrowser.cs @@ -11,19 +11,14 @@ public interface IBrowser public Microsoft.Playwright.IBrowser InnerBrowser { get; } /// - /// Gets whether the browser runs on headless mode or not. + /// Gets or sets the browser type. /// - public bool Headless { get; } + public BrowserType Type { get; set; } /// - /// Gets the browser type. + /// Gets or sets the browser version. /// - public BrowserType Type { get; } - - /// - /// Gets the browser version. - /// - public string Version { get; } + public string Version { get; set; } /// /// Opens a page with a given URL diff --git a/src/OrchardCoreContrib.Testing.UI/IElement.cs b/src/OrchardCoreContrib.Testing.UI/IElement.cs index bb4d088..66e2958 100644 --- a/src/OrchardCoreContrib.Testing.UI/IElement.cs +++ b/src/OrchardCoreContrib.Testing.UI/IElement.cs @@ -8,7 +8,7 @@ public interface IElement /// /// Gets the inner text of the element. /// - public string InnerText { get; } + public string InnerText { get; set; } /// /// Gets the inner HTML of the element. @@ -18,12 +18,12 @@ public interface IElement /// /// Gets whether the element is enabled. /// - public bool Enabled { get; } + public bool Enabled { get; set; } /// /// Gets whether the element is visible. /// - public bool Visible { get; } + public bool Visible { get; set; } /// /// Writes a given text into an element. diff --git a/src/OrchardCoreContrib.Testing.UI/IPage.cs b/src/OrchardCoreContrib.Testing.UI/IPage.cs index e65559b..2fbc7dc 100644 --- a/src/OrchardCoreContrib.Testing.UI/IPage.cs +++ b/src/OrchardCoreContrib.Testing.UI/IPage.cs @@ -11,14 +11,14 @@ public interface IPage public Microsoft.Playwright.IPage InnerPage { get; } /// - /// Gets the page title. + /// Gets or sets the page title. /// - public string Title { get; } + public string Title { get; set; } /// - /// Gets the page content in HTML format. + /// Gets or sets the page content in HTML format. /// - public string Content { get; } + public string Content { get; set; } /// /// Navigates to a given URL. diff --git a/src/OrchardCoreContrib.Testing.UI/Page.cs b/src/OrchardCoreContrib.Testing.UI/Page.cs index c88aef6..979f5ad 100644 --- a/src/OrchardCoreContrib.Testing.UI/Page.cs +++ b/src/OrchardCoreContrib.Testing.UI/Page.cs @@ -15,20 +15,33 @@ public class Page(IPlaywrightPageAccessor playwrightPageAccessor) : IPage public Microsoft.Playwright.IPage InnerPage => playwrightPageAccessor.PlaywrightPage; /// - public string Title => InnerPage.TitleAsync().GetAwaiter().GetResult(); + public string Title { get; set; } /// - public string Content => InnerPage.ContentAsync().GetAwaiter().GetResult(); + public string Content { get; set; } /// - public async Task GoToAsync(string url) => await InnerPage.GotoAsync(url); + public async Task GoToAsync(string url) + { + await InnerPage.GotoAsync(url); + + Title = await InnerPage.TitleAsync(); + Content = await InnerPage.ContentAsync(); + } /// public IElement FindElement(string selector) { var locator = InnerPage.Locator(selector); - - return new Element(locator); + var element = new Element(locator) + { + InnerText = locator.InnerTextAsync().GetAwaiter().GetResult(), + InnerHtml = locator.InnerHTMLAsync().GetAwaiter().GetResult(), + Enabled = locator.IsEnabledAsync().GetAwaiter().GetResult(), + Visible = locator.IsVisibleAsync().GetAwaiter().GetResult() + }; + + return element; } /// diff --git a/src/OrchardCoreContrib.Testing.UI/UITest.cs b/src/OrchardCoreContrib.Testing.UI/UITest.cs index 1fb1cc5..e6cfe53 100644 --- a/src/OrchardCoreContrib.Testing.UI/UITest.cs +++ b/src/OrchardCoreContrib.Testing.UI/UITest.cs @@ -1,5 +1,4 @@ using Microsoft.Playwright; -using OrchardCoreContrib.Testing.UI.Infrastructure; using Xunit; namespace OrchardCoreContrib.Testing.UI; @@ -18,12 +17,20 @@ public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = /// public IBrowser Browser { get; private set; } + public UITestOptions Options { get; private set; } + /// public async Task InitializeAsync() { + Options = new UITestOptions + { + BrowserType = browserType, + Headless = headless + }; + _playwright = await Playwright.CreateAsync(); - Browser = await BrowserFactory.CreateAsync(_playwright, browserType, headless); + Browser = await BrowserFactory.CreateAsync(_playwright, Options); } /// diff --git a/src/OrchardCoreContrib.Testing.UI/UITestBase.cs b/src/OrchardCoreContrib.Testing.UI/UITestBase.cs index 0a912bf..5bd552f 100644 --- a/src/OrchardCoreContrib.Testing.UI/UITestBase.cs +++ b/src/OrchardCoreContrib.Testing.UI/UITestBase.cs @@ -13,4 +13,6 @@ public abstract class UITestBase(WebApplicationFactoryFixture public string BaseUrl => fixture.ServerAddress; + + public UITestOptions Options { get; protected set; } } diff --git a/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs b/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs index 09aa109..8f3efab 100644 --- a/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs +++ b/src/OrchardCoreContrib.Testing.UI/UITestOfT.cs @@ -24,9 +24,15 @@ public class UITest(BrowserType browserType = BrowserType.Edge, bool h /// public async Task InitializeAsync() { + Options = new UITestOptions + { + BrowserType = browserType, + Headless = headless + }; + _playwright = await Playwright.CreateAsync(); - Browser = await BrowserFactory.CreateAsync(_playwright, browserType, headless); + Browser = await BrowserFactory.CreateAsync(_playwright, Options); } /// diff --git a/src/OrchardCoreContrib.Testing.UI/UITestOptions.cs b/src/OrchardCoreContrib.Testing.UI/UITestOptions.cs new file mode 100644 index 0000000..068244c --- /dev/null +++ b/src/OrchardCoreContrib.Testing.UI/UITestOptions.cs @@ -0,0 +1,17 @@ +namespace OrchardCoreContrib.Testing.UI; + +/// +/// Represents a set of options to be used during the test. +/// +public class UITestOptions +{ + /// + /// Gets or sets whether to run the browser on headless mode. Defaults true. + /// + public bool Headless { get; set; } = true; + + /// + /// Gets or sets the browser type to run the test on. Defaults . + /// + public BrowserType BrowserType { get; set; } = BrowserType.Edge; +} diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/BrowserFactoryTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/BrowserFactoryTests.cs index 3991792..9109aff 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/BrowserFactoryTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/BrowserFactoryTests.cs @@ -4,26 +4,28 @@ namespace OrchardCoreContrib.Testing.UI.Tests; public class BrowserFactoryTests { - [InlineData(BrowserType.Chrome, PlaywrightBrowserType.Chromium, false)] - [InlineData(BrowserType.Edge, PlaywrightBrowserType.Chromium, false)] - [InlineData(BrowserType.Firefox, PlaywrightBrowserType.Firefox, false)] - [InlineData(BrowserType.Chrome, PlaywrightBrowserType.Chromium, true)] - [InlineData(BrowserType.Edge, PlaywrightBrowserType.Chromium, true)] - [InlineData(BrowserType.Firefox, PlaywrightBrowserType.Firefox, true)] + [InlineData(BrowserType.Chrome, PlaywrightBrowserType.Chromium)] + [InlineData(BrowserType.Edge, PlaywrightBrowserType.Chromium)] + [InlineData(BrowserType.Firefox, PlaywrightBrowserType.Firefox)] [Theory] - public async Task CreateBrowser(BrowserType browserType, string playwrightBrowserType, bool headless) + public async Task CreateBrowser(BrowserType browserType, string playwrightBrowserType) { // Arrange var playwright = await Playwright.CreateAsync(); + var testOptions = new UITestOptions + { + BrowserType = browserType + }; + // Act - var browser = await BrowserFactory.CreateAsync(playwright, browserType, headless); + var browser = await BrowserFactory.CreateAsync(playwright, testOptions); + browser.Type = browserType; // Assert Assert.NotNull(browser); Assert.Equal(browserType, browser.Type); Assert.Equal(playwrightBrowserType, browser.InnerBrowser.BrowserType.Name); - Assert.Equal(headless, browser.Headless); } [Fact] @@ -31,11 +33,15 @@ public async Task CreateBrowser_ThrowsException_WhenBrowserTypeInvalid() { // Arrange var playwright = await Playwright.CreateAsync(); + var testOptions = new UITestOptions + { + BrowserType = BrowserType.NotSet + }; // Act & Assert await Assert.ThrowsAsync(async () => { - await BrowserFactory.CreateAsync(playwright, BrowserType.NotSet, headless: true); + await BrowserFactory.CreateAsync(playwright, testOptions); }); } } diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/BrowserTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/BrowserTests.cs index 6b2c430..7d01557 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/BrowserTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/BrowserTests.cs @@ -22,13 +22,12 @@ public void ShouldCreateBrowser() var playwrightBrowserAccessor = new PlaywrightBrowserAccessor(_browserMock.Object); // Act - var browser = new Browser(playwrightBrowserAccessor, BrowserType.Edge, headless: true); + var browser = new Browser(playwrightBrowserAccessor) { Type = BrowserType.Edge }; // Assert Assert.NotNull(browser); Assert.Equal(BrowserType.Edge, browser.Type); Assert.Equal(version, browser.Version); - Assert.True(browser.Headless); Assert.Same(playwrightBrowserAccessor.PlaywrightBrowser, browser.InnerBrowser); } @@ -37,7 +36,7 @@ public async Task ShouldOpenPage() { // Arrange var playwrightBrowserAccessor = new PlaywrightBrowserAccessor(_browserMock.Object); - var browser = new Browser(playwrightBrowserAccessor, BrowserType.Edge, headless: true); + var browser = new Browser(playwrightBrowserAccessor) { Type = BrowserType.Edge }; // Act var page = await browser.OpenPageAsync("https://www.orchardcore.net"); diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs index d667fa1..79a8d48 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs @@ -5,22 +5,14 @@ public class ElementTests [Fact] public void GetElementInformation() { - // Arrange - var locatorMock = new Mock(); - locatorMock.Setup(l => l.InnerHTMLAsync(null)) - .ReturnsAsync("

Orchard Core Contrib

"); - - locatorMock.Setup(l => l.InnerTextAsync(null)) - .ReturnsAsync("Orchard Core Contrib"); - - locatorMock.Setup(l => l.IsDisabledAsync(null)) - .ReturnsAsync(false); - - locatorMock.Setup(l => l.IsVisibleAsync(null)) - .ReturnsAsync(true); - // Act - var element = new Element(locatorMock.Object); + var element = new Element(Mock.Of()) + { + InnerHtml = "

Orchard Core Contrib

", + InnerText = "Orchard Core Contrib", + Enabled = false, + Visible = true + }; // Assert Assert.Equal("

Orchard Core Contrib

", element.InnerHtml); diff --git a/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs b/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs index fc11e0b..1a0d146 100644 --- a/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs +++ b/test/OrchardCoreContrib.Testing.UI.Tests/UITestOfTTests.cs @@ -23,7 +23,7 @@ public async Task RunTest() [Fact] public async Task NavigateToHomePage() { - // Arrange & Act + // Act var page = await Browser.OpenPageAsync(BaseUrl + "foo"); // Assert