Skip to content

Commit 8ff71d8

Browse files
committed
Merge branch 'main' into delay-execution
# Conflicts: # src/OrchardCoreContrib.Testing.UI/Browser.cs # src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs # src/OrchardCoreContrib.Testing.UI/Element.cs # src/OrchardCoreContrib.Testing.UI/IBrowser.cs # src/OrchardCoreContrib.Testing.UI/Page.cs # src/OrchardCoreContrib.Testing.UI/UITest.cs # src/OrchardCoreContrib.Testing.UI/UITestOfT.cs # test/OrchardCoreContrib.Testing.UI.Tests/BrowserFactoryTests.cs # test/OrchardCoreContrib.Testing.UI.Tests/BrowserTests.cs # test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs
2 parents bbdeaf3 + 0f1d717 commit 8ff71d8

15 files changed

Lines changed: 137 additions & 102 deletions

File tree

src/OrchardCoreContrib.Testing.UI/Browser.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,33 @@ namespace OrchardCoreContrib.Testing.UI;
1111
/// <param name="playwrightBrowserAccessor">The <see cref="IPlaywrightBrowserAccessor"/>.</param>
1212
/// <param name="type">The <see cref="BrowserType"/>.</param>
1313
/// <param name="headless">Whether to run browser in headless mode.</param>
14-
public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor, BrowserType type, bool headless, int delay) : IBrowser
14+
public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor, int delay) : IBrowser
1515
{
1616
/// <inheritdoc/>
1717
public PlaywrightBrowser InnerBrowser => playwrightBrowserAccessor.PlaywrightBrowser;
1818

1919
/// <inheritdoc/>
20-
public bool Headless => headless;
21-
22-
/// <inheritdoc/>
23-
public BrowserType Type => type;
20+
public BrowserType Type { get; set; }
2421

2522
/// <inheritdoc/>
2623
public int Delay => delay;
2724

2825
/// <inheritdoc/>
29-
public string Version => InnerBrowser.Version;
26+
public string Version { get; set; } = playwrightBrowserAccessor.PlaywrightBrowser.Version;
3027

3128
/// <inheritdoc/>
3229
public async Task<IPage> OpenPageAsync(string url)
3330
{
34-
var page = await InnerBrowser.NewPageAsync();
31+
var playwrightPage = await InnerBrowser.NewPageAsync();
32+
33+
await playwrightPage.GotoAsync(url);
3534

36-
await page.GotoAsync(url);
35+
var page = new Page(new PlaywrightPageAccessor(playwrightPage))
36+
{
37+
Title = await playwrightPage.TitleAsync(),
38+
Content = await playwrightPage.ContentAsync()
39+
};
3740

38-
return new Page(this, new PlaywrightPageAccessor(page));
41+
return page;
3942
}
4043
}

src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@ public static class BrowserFactory
1616
/// Creates a new instance of <see cref="IBrowser"/> with a given browser type.
1717
/// </summary>
1818
/// <param name="playwright">The <see cref="IPlaywright"/>.</param>
19-
/// <param name="browserType">The browser type in which <see cref="IBrowser"/> will be created.</param>
20-
/// <param name="headless">Whether the browser runs in headless mode or not.</param>
19+
/// <param name="testOptions">The <see cref="UITestOptions"/>.</param>
2120
/// <returns>An instance of <see cref="IBrowser"/>.</returns>
2221
/// <exception cref="NotSupportedException"></exception>
23-
public static async Task<IBrowser> CreateAsync(IPlaywright playwright, BrowserType browserType, bool headless, int delay)
22+
public static async Task<IBrowser> CreateAsync(IPlaywright playwright, UITestOptions testOptions)
2423
{
25-
var browser = browserType switch
24+
var browser = testOptions.BrowserType switch
2625
{
27-
BrowserType.Edge => _edgeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Channel = "msedge", Headless = headless }),
28-
BrowserType.Chrome => _chromeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless }),
29-
BrowserType.Firefox => _fireFoxBrowser ?? await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless }),
26+
BrowserType.Edge => _edgeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
27+
{
28+
Channel = "msedge",
29+
Headless = testOptions.Headless
30+
}),
31+
BrowserType.Chrome => _chromeBrowser ?? await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
32+
{
33+
Headless = testOptions.Headless
34+
}),
35+
BrowserType.Firefox => _fireFoxBrowser ?? await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions
36+
{
37+
Headless = testOptions.Headless
38+
}),
3039
_ => throw new NotSupportedException()
3140
};
3241

33-
return new Browser(new PlaywrightBrowserAccessor(browser), browserType, headless, delay);
42+
return new Browser(new PlaywrightBrowserAccessor(browser)) { Type = testOptions.BrowserType };
3443
}
3544
}

src/OrchardCoreContrib.Testing.UI/Element.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,26 @@ public class Element(IPage page, ILocator locator) : IElement
1919
IPage IElement.Page => page;
2020

2121
/// <inheritdoc/>
22-
public string InnerText => locator.InnerTextAsync().GetAwaiter().GetResult();
22+
public string InnerText { get; set; }
2323

2424
/// <inheritdoc/>
25-
public string InnerHtml => locator.InnerHTMLAsync().GetAwaiter().GetResult();
25+
public string InnerHtml { get; set; }
2626

2727
/// <inheritdoc/>
28-
public bool Enabled => locator.IsEnabledAsync().GetAwaiter().GetResult();
28+
public bool Enabled { get; set; }
2929

3030
/// <inheritdoc/>
31-
public bool Visible => locator.IsVisibleAsync().GetAwaiter().GetResult();
31+
public bool Visible { get; set; }
3232

3333
/// <inheritdoc/>
3434
public async Task ClickAsync() => await locator.ClickAsync(_locatorClickOptions);
3535

3636
/// <inheritdoc/>
37-
public async Task TypeAsync(string text) => await locator.PressSequentiallyAsync(text, _locatorPressSequentiallyOptions);
37+
public async Task TypeAsync(string text)
38+
{
39+
await locator.FillAsync(text);
40+
41+
InnerText = await locator.InnerTextAsync();
42+
InnerHtml = await locator.InnerHTMLAsync();
43+
}
3844
}

src/OrchardCoreContrib.Testing.UI/IBrowser.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,14 @@ public interface IBrowser
1111
public Microsoft.Playwright.IBrowser InnerBrowser { get; }
1212

1313
/// <summary>
14-
/// Gets whether the browser runs on headless mode or not.
14+
/// Gets or sets the browser type.
1515
/// </summary>
16-
public bool Headless { get; }
16+
public BrowserType Type { get; set; }
1717

1818
/// <summary>
19-
/// Gets the browser type.
19+
/// Gets or sets the browser version.
2020
/// </summary>
21-
public BrowserType Type { get; }
22-
23-
public int Delay { get; }
24-
25-
/// <summary>
26-
/// Gets the browser version.
27-
/// </summary>
28-
public string Version { get; }
21+
public string Version { get; set; }
2922

3023
/// <summary>
3124
/// Opens a page with a given URL

src/OrchardCoreContrib.Testing.UI/IElement.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IElement
1010
/// <summary>
1111
/// Gets the inner text of the element.
1212
/// </summary>
13-
public string InnerText { get; }
13+
public string InnerText { get; set; }
1414

1515
/// <summary>
1616
/// Gets the inner HTML of the element.
@@ -20,12 +20,12 @@ public interface IElement
2020
/// <summary>
2121
/// Gets whether the element is enabled.
2222
/// </summary>
23-
public bool Enabled { get; }
23+
public bool Enabled { get; set; }
2424

2525
/// <summary>
2626
/// Gets whether the element is visible.
2727
/// </summary>
28-
public bool Visible { get; }
28+
public bool Visible { get; set; }
2929

3030
/// <summary>
3131
/// Writes a given text into an element.

src/OrchardCoreContrib.Testing.UI/IPage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public interface IPage
1313
internal IBrowser Browser { get; }
1414

1515
/// <summary>
16-
/// Gets the page title.
16+
/// Gets or sets the page title.
1717
/// </summary>
18-
public string Title { get; }
18+
public string Title { get; set; }
1919

2020
/// <summary>
21-
/// Gets the page content in HTML format.
21+
/// Gets or sets the page content in HTML format.
2222
/// </summary>
23-
public string Content { get; }
23+
public string Content { get; set; }
2424

2525
/// <summary>
2626
/// Navigates to a given URL.

src/OrchardCoreContrib.Testing.UI/Page.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,43 @@ namespace OrchardCoreContrib.Testing.UI;
99
/// Creates an instance of <see cref="Page"/>.
1010
/// </remarks>
1111
/// <param name="playwrightPageAccessor">The <see cref="IPlaywrightPageAccessor"/>.</param>
12-
public class Page(IBrowser browser, IPlaywrightPageAccessor playwrightPageAccessor) : IPage
12+
public class Page(IPlaywrightPageAccessor playwrightPageAccessor) : IPage
1313
{
14-
private readonly PageClickOptions _pageClickOptions = browser.Delay == 0
15-
? null
16-
: new() { Delay = browser.Delay };
17-
18-
/// <inheritdoc/>
19-
IBrowser IPage.Browser => browser;
20-
2114
/// <inheritdoc/>
2215
public Microsoft.Playwright.IPage InnerPage => playwrightPageAccessor.PlaywrightPage;
2316

2417
/// <inheritdoc/>
25-
public string Title => InnerPage.TitleAsync().GetAwaiter().GetResult();
18+
public string Title { get; set; }
2619

2720
/// <inheritdoc/>
28-
public string Content => InnerPage.ContentAsync().GetAwaiter().GetResult();
21+
public string Content { get; set; }
2922

3023
/// <inheritdoc/>
31-
public async Task GoToAsync(string url) => await InnerPage.GotoAsync(url);
24+
public async Task GoToAsync(string url)
25+
{
26+
await InnerPage.GotoAsync(url);
27+
28+
Title = await InnerPage.TitleAsync();
29+
Content = await InnerPage.ContentAsync();
30+
}
3231

3332
/// <inheritdoc/>
3433
public IElement FindElement(string selector)
3534
{
3635
var locator = InnerPage.Locator(selector);
37-
38-
return new Element(this, locator);
36+
var element = new Element(locator)
37+
{
38+
InnerText = locator.InnerTextAsync().GetAwaiter().GetResult(),
39+
InnerHtml = locator.InnerHTMLAsync().GetAwaiter().GetResult(),
40+
Enabled = locator.IsEnabledAsync().GetAwaiter().GetResult(),
41+
Visible = locator.IsVisibleAsync().GetAwaiter().GetResult()
42+
};
43+
44+
return element;
3945
}
4046

4147
/// <inheritdoc/>
42-
public async Task ClickAsync(string selector) => await InnerPage.ClickAsync(selector, _pageClickOptions);
48+
public async Task ClickAsync(string selector) => await FindElement(selector).ClickAsync();
4349

4450
/// <inheritdoc/>
4551
public async Task ScreenShotAsync(string path, bool fullPage = false)

src/OrchardCoreContrib.Testing.UI/UITest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.Playwright;
2-
using OrchardCoreContrib.Testing.UI.Infrastructure;
32
using Xunit;
43

54
namespace OrchardCoreContrib.Testing.UI;
@@ -9,7 +8,7 @@ namespace OrchardCoreContrib.Testing.UI;
98
/// </summary>
109
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
1110
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
12-
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0) : IAsyncLifetime
11+
public class UITest(BrowserType browserType = BrowserType.Edge, bool headless = true) : IAsyncLifetime
1312
{
1413
private IPlaywright _playwright;
1514

@@ -18,12 +17,20 @@ public class UITest(BrowserType browserType = BrowserType.Edge, bool headless =
1817
/// </summary>
1918
public IBrowser Browser { get; private set; }
2019

20+
public UITestOptions Options { get; private set; }
21+
2122
/// <inheritdoc/>
2223
public async Task InitializeAsync()
2324
{
25+
Options = new UITestOptions
26+
{
27+
BrowserType = browserType,
28+
Headless = headless
29+
};
30+
2431
_playwright = await Playwright.CreateAsync();
2532

26-
Browser = await BrowserFactory.CreateAsync(_playwright, browserType, headless, delay);
33+
Browser = await BrowserFactory.CreateAsync(_playwright, Options);
2734
}
2835

2936
/// <inheritdoc/>

src/OrchardCoreContrib.Testing.UI/UITestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public abstract class UITestBase<TStartup>(WebApplicationFactoryFixture<TStartup
1313
/// Gets the base URL used for the tested website.
1414
/// </summary>
1515
public string BaseUrl => fixture.ServerAddress;
16+
17+
public UITestOptions Options { get; protected set; }
1618
}

src/OrchardCoreContrib.Testing.UI/UITestOfT.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OrchardCoreContrib.Testing.UI;
1010
/// <param name="browserType">The browser type that will be used during the test. Defaults to <see cref="BrowserType.Edge"/>.</param>
1111
/// <param name="headless">Whether the browser runs in headless mode or not. Defaults to <c>true</c>.</param>
1212
/// <typeparam name="TStartup">The startup class type that will be used as entry point.</typeparam>
13-
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true, int delay = 0) :
13+
public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool headless = true) :
1414
UITestBase<TStartup>(new WebApplicationFactoryFixture<TStartup>()),
1515
IAsyncLifetime where TStartup : class
1616
{
@@ -24,9 +24,15 @@ public class UITest<TStartup>(BrowserType browserType = BrowserType.Edge, bool h
2424
/// <inheritdoc/>
2525
public async Task InitializeAsync()
2626
{
27+
Options = new UITestOptions
28+
{
29+
BrowserType = browserType,
30+
Headless = headless
31+
};
32+
2733
_playwright = await Playwright.CreateAsync();
2834

29-
Browser = await BrowserFactory.CreateAsync(_playwright, browserType, headless, delay);
35+
Browser = await BrowserFactory.CreateAsync(_playwright, Options);
3036
}
3137

3238
/// <inheritdoc/>

0 commit comments

Comments
 (0)