Skip to content

Commit

Permalink
Fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed May 1, 2024
1 parent 8ff71d8 commit e63f005
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/OrchardCoreContrib.Testing.UI/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OrchardCoreContrib.Testing.UI;
/// <param name="playwrightBrowserAccessor">The <see cref="IPlaywrightBrowserAccessor"/>.</param>
/// <param name="type">The <see cref="BrowserType"/>.</param>
/// <param name="headless">Whether to run browser in headless mode.</param>
public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor, int delay) : IBrowser
public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor) : IBrowser
{
/// <inheritdoc/>
public PlaywrightBrowser InnerBrowser => playwrightBrowserAccessor.PlaywrightBrowser;
Expand All @@ -20,10 +20,10 @@ public class Browser(IPlaywrightBrowserAccessor playwrightBrowserAccessor, int d
public BrowserType Type { get; set; }

/// <inheritdoc/>
public int Delay => delay;
public string Version { get; set; } = playwrightBrowserAccessor.PlaywrightBrowser.Version;

/// <inheritdoc/>
public string Version { get; set; } = playwrightBrowserAccessor.PlaywrightBrowser.Version;
public UITestOptions TestOptions { get; set; }

/// <inheritdoc/>
public async Task<IPage> OpenPageAsync(string url)
Expand All @@ -32,7 +32,7 @@ public async Task<IPage> OpenPageAsync(string url)

await playwrightPage.GotoAsync(url);

var page = new Page(new PlaywrightPageAccessor(playwrightPage))
var page = new Page(new PlaywrightPageAccessor(playwrightPage), this)
{
Title = await playwrightPage.TitleAsync(),
Content = await playwrightPage.ContentAsync()
Expand Down
6 changes: 5 additions & 1 deletion src/OrchardCoreContrib.Testing.UI/BrowserFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static async Task<IBrowser> CreateAsync(IPlaywright playwright, UITestOpt
_ => throw new NotSupportedException()
};

return new Browser(new PlaywrightBrowserAccessor(browser)) { Type = testOptions.BrowserType };
return new Browser(new PlaywrightBrowserAccessor(browser))
{
Type = testOptions.BrowserType,
TestOptions = testOptions
};
}
}
13 changes: 7 additions & 6 deletions src/OrchardCoreContrib.Testing.UI/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace OrchardCoreContrib.Testing.UI;
/// </summary>
/// <remarks>The <see cref="Element"/>.</remarks>
/// <param name="locator">The <see cref="ILocator"/>.</param>
public class Element(IPage page, ILocator locator) : IElement
/// <param name="IPage">The <see cref="page"/>.</param>
public class Element(ILocator locator, IPage page) : IElement
{
private readonly LocatorClickOptions _locatorClickOptions = page.Browser.Delay == 0
private readonly LocatorClickOptions _locatorClickOptions = page.Browser.TestOptions.Delay == 0
? null
: new() { Delay = page.Browser.Delay };
private readonly LocatorPressSequentiallyOptions _locatorPressSequentiallyOptions = page.Browser.Delay == 0
: new() { Delay = page.Browser.TestOptions.Delay };
private readonly LocatorPressSequentiallyOptions _locatorPressSequentiallyOptions = page.Browser.TestOptions.Delay == 0
? null
: new() { Delay = page.Browser.Delay };
: new() { Delay = page.Browser.TestOptions.Delay };

IPage IElement.Page => page;

Expand All @@ -36,7 +37,7 @@ public class Element(IPage page, ILocator locator) : IElement
/// <inheritdoc/>
public async Task TypeAsync(string text)
{
await locator.FillAsync(text);
await locator.PressSequentiallyAsync(text, _locatorPressSequentiallyOptions);

InnerText = await locator.InnerTextAsync();
InnerHtml = await locator.InnerHTMLAsync();
Expand Down
5 changes: 5 additions & 0 deletions src/OrchardCoreContrib.Testing.UI/IBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public interface IBrowser
/// </summary>
public Microsoft.Playwright.IBrowser InnerBrowser { get; }

/// <summary>
/// Gets the test options that will be applied;
/// </summary>
public UITestOptions TestOptions { get; set; }

/// <summary>
/// Gets or sets the browser type.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions src/OrchardCoreContrib.Testing.UI/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ namespace OrchardCoreContrib.Testing.UI;
/// Creates an instance of <see cref="Page"/>.
/// </remarks>
/// <param name="playwrightPageAccessor">The <see cref="IPlaywrightPageAccessor"/>.</param>
public class Page(IPlaywrightPageAccessor playwrightPageAccessor) : IPage
/// <param name="browser">The <see cref="IBrowser"/>.</param>
public class Page(IPlaywrightPageAccessor playwrightPageAccessor, IBrowser browser) : IPage
{
/// <inheritdoc/>
public Microsoft.Playwright.IPage InnerPage => playwrightPageAccessor.PlaywrightPage;

IBrowser IPage.Browser => browser;

/// <inheritdoc/>
public string Title { get; set; }

Expand All @@ -33,7 +36,7 @@ public async Task GoToAsync(string url)
public IElement FindElement(string selector)
{
var locator = InnerPage.Locator(selector);
var element = new Element(locator)
var element = new Element(locator, this)
{
InnerText = locator.InnerTextAsync().GetAwaiter().GetResult(),
InnerHtml = locator.InnerHTMLAsync().GetAwaiter().GetResult(),
Expand Down
5 changes: 5 additions & 0 deletions src/OrchardCoreContrib.Testing.UI/UITestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public class UITestOptions
/// Gets or sets the browser type to run the test on. Defaults <see cref="BrowserType.Edge"/>.
/// </summary>
public BrowserType BrowserType { get; set; } = BrowserType.Edge;

/// <summary>
/// Gets or sets amount of time to wait before executing each event on the page. Defaults <c>0</c>.
/// </summary>
public int Delay { get; set; } = 0;
}
19 changes: 15 additions & 4 deletions test/OrchardCoreContrib.Testing.UI.Tests/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

public class ElementTests
{
private readonly Page _page;

public ElementTests()
{
var browser = new Browser(new PlaywrightBrowserAccessor(Mock.Of<Microsoft.Playwright.IBrowser>()))
{
TestOptions = new UITestOptions()
};
_page = new Page(new PlaywrightPageAccessor(Mock.Of<Microsoft.Playwright.IPage>()), browser);
}

[Fact]
public void GetElementInformation()
{
// Act
var element = new Element(Mock.Of<ILocator>())
var element = new Element(Mock.Of<ILocator>(), _page)
{
InnerHtml = "<h1>Orchard Core Contrib</h1>",
InnerText = "Orchard Core Contrib",
Expand All @@ -26,7 +37,7 @@ public async Task ClickElement()
{
// Arrange
var locatorMock = new Mock<ILocator>();
var element = new Element(locatorMock.Object);
var element = new Element(locatorMock.Object, _page);

// Act
await element.ClickAsync();
Expand All @@ -40,13 +51,13 @@ public async Task TypeTextIntoElement()
{
// Arrange
var locatorMock = new Mock<ILocator>();
locatorMock.Setup(l => l.FillAsync(It.IsAny<string>(), null))
locatorMock.Setup(l => l.PressSequentiallyAsync(It.IsAny<string>(), null))
.Callback(() =>
{
locatorMock.Setup(l => l.InnerTextAsync(null))
.ReturnsAsync("Orchard Core Contrib");
});
var element = new Element(locatorMock.Object);
var element = new Element(locatorMock.Object, _page);

// Act
await element.TypeAsync("Orchard Core Contrib");
Expand Down
16 changes: 12 additions & 4 deletions test/OrchardCoreContrib.Testing.UI.Tests/PageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ namespace OrchardCoreContrib.Testing.UI.Tests;

public class PageTests
{
private readonly IBrowser _browser = Mock.Of<IBrowser>();
private readonly Browser _browser;

public PageTests()
{
_browser = new Browser(new PlaywrightBrowserAccessor(Mock.Of<Microsoft.Playwright.IBrowser>()))
{
TestOptions = new UITestOptions()
};
}

[Fact]
public void ShouldCreatePage()
Expand All @@ -11,7 +19,7 @@ public void ShouldCreatePage()
var playwrightPageAccessor = new PlaywrightPageAccessor(Mock.Of<Microsoft.Playwright.IPage>());

// Act
var page = new Page(_browser, playwrightPageAccessor);
var page = new Page(playwrightPageAccessor, _browser);

// Assert
Assert.NotNull(page);
Expand All @@ -33,7 +41,7 @@ public async Task GetPageInformation()
var playwrightPageAccessor = new PlaywrightPageAccessor(pageMock.Object);

// Act
var page = new Page(_browser, playwrightPageAccessor);
var page = new Page(playwrightPageAccessor, _browser);
await page.GoToAsync("www.occ.com");

// Assert
Expand All @@ -52,7 +60,7 @@ public void ShouldFindElement()

var playwrightPageAccessor = new PlaywrightPageAccessor(pageMock.Object);

var page = new Page(_browser, playwrightPageAccessor);
var page = new Page(playwrightPageAccessor, _browser);

// Act
var result = page.FindElement("selector");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ public static async Task<IPage> GotoAsync(string pageName)
{
var playwright = await Playwright.CreateAsync();
var playwrightBrowser = await playwright.Chromium.LaunchAsync();
var browser = new Browser(new PlaywrightBrowserAccessor(playwrightBrowser), BrowserType.Edge, headless: true, delay: 0);
var browser = new Browser(new PlaywrightBrowserAccessor(playwrightBrowser))
{
TestOptions = new UITestOptions()
};

var page = await playwrightBrowser.NewPageAsync();

await page.GotoAsync(PageHelper.GetFullPath(pageName));

return new Page(browser, new PlaywrightPageAccessor(page));
return new Page(new PlaywrightPageAccessor(page), browser);
}
}

0 comments on commit e63f005

Please sign in to comment.