Skip to content

Commit

Permalink
feat(roll): roll Playwright to 1.43.0-beta-1712646596000 (#2905)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Apr 9, 2024
1 parent 6e04b94 commit 9c725ca
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Common/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyVersion>1.42.0</AssemblyVersion>
<PackageVersion>$(AssemblyVersion)</PackageVersion>
<DriverVersion>1.43.0</DriverVersion>
<DriverVersion>1.43.0-beta-1712646596000</DriverVersion>
<ReleaseVersion>$(AssemblyVersion)</ReleaseVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<NoDefaultExcludes>true</NoDefaultExcludes>
Expand Down
90 changes: 90 additions & 0 deletions src/Playwright.Tests/ChromiumLauncherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace Microsoft.Playwright.Tests.Firefox;

public class ChromiumLauncherTests : PlaywrightTestEx
{
[PlaywrightTest("chromium/launcher.spec.ts", "should return background pages")]
[Skip(SkipAttribute.Targets.Webkit, SkipAttribute.Targets.Firefox)]
public async Task ShouldReturnBackgroundPages()
{
using var userDataDir = new TempDirectory();
var extensionPath = TestUtils.GetAsset("simple-extension");
var extensionOptions = new BrowserTypeLaunchPersistentContextOptions
{
Headless = false,
Args = new[] {
$"--disable-extensions-except={extensionPath}",
$"--load-extension={extensionPath}",
},
};
var context = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, extensionOptions);
var backgroundPages = context.BackgroundPages;
var backgroundPage = backgroundPages.Count > 0
? backgroundPages[0]
: await WaitForBackgroundPage(context);
Assert.NotNull(backgroundPage);
Assert.Contains(backgroundPage, context.BackgroundPages.ToList());
Assert.False(context.Pages.Contains(backgroundPage));
await context.CloseAsync();
Assert.IsEmpty(context.Pages);
Assert.IsEmpty(context.BackgroundPages);
}

[PlaywrightTest("chromium/launcher.spec.ts", "should return background pages when recording video")]
[Skip(SkipAttribute.Targets.Webkit, SkipAttribute.Targets.Firefox)]
public async Task ShouldReturnBackgroundPagesWhenRecordingVideo()
{
using var tempDirectory = new TempDirectory();
using var userDataDir = new TempDirectory();
var extensionPath = TestUtils.GetAsset("simple-extension");
var extensionOptions = new BrowserTypeLaunchPersistentContextOptions
{
Headless = false,
Args = new[] {
$"--disable-extensions-except={extensionPath}",
$"--load-extension={extensionPath}",
},
RecordVideoDir = tempDirectory.Path,
};
var context = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, extensionOptions);
var backgroundPages = context.BackgroundPages;

var backgroundPage = backgroundPages.Count > 0
? backgroundPages[0]
: await WaitForBackgroundPage(context);
Assert.NotNull(backgroundPage);
Assert.Contains(backgroundPage, context.BackgroundPages.ToList());
Assert.False(context.Pages.Contains(backgroundPage));
await context.CloseAsync();
}

private async Task<IPage> WaitForBackgroundPage(IBrowserContext context)
{
var tsc = new TaskCompletionSource<IPage>();
context.BackgroundPage += (_, e) => tsc.TrySetResult(e);
return await tsc.Task;
}
}
17 changes: 17 additions & 0 deletions src/Playwright/API/Generated/IBrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ namespace Microsoft.Playwright;
/// </summary>
public partial interface IBrowserContext
{
/// <summary>
/// <para>Emitted when new background page is created in the context.</para>
/// <code>
/// context.BackgroundPage += (_, backgroundPage) =&gt;<br/>
/// {<br/>
/// Console.WriteLine(backgroundPage.Url);<br/>
/// };<br/>
///
/// </code>
/// </summary>
/// <remarks><para>Only works with Chromium browser's persistent context.</para></remarks>
event EventHandler<IPage> BackgroundPage;

/// <summary>
/// <para>
/// Emitted when Browser context gets closed. This might happen because of one of the
Expand Down Expand Up @@ -239,6 +252,10 @@ public partial interface IBrowserContext
/// <param name="scriptPath">Instead of specifying <paramref name="script"/>, gives the file name to load from.</param>
Task AddInitScriptAsync(string? script = default, string? scriptPath = default);

/// <summary><para>All existing background pages in the context.</para></summary>
/// <remarks><para>Background pages are only supported on Chromium-based browsers.</para></remarks>
IReadOnlyList<IPage> BackgroundPages { get; }

/// <summary>
/// <para>
/// Returns the browser instance of the context. If it was launched as a persistent
Expand Down
12 changes: 12 additions & 0 deletions src/Playwright/Core/BrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal class BrowserContext : ChannelOwner, IBrowserContext
private readonly Dictionary<string, Delegate> _bindings = new();
private readonly BrowserContextInitializer _initializer;
private readonly Tracing _tracing;
internal readonly HashSet<IPage> _backgroundPages = new();
internal readonly IAPIRequestContext _request;
private readonly Dictionary<string, HarRecorder> _harRecorders = new();
internal readonly List<IWorker> _serviceWorkers = new();
Expand Down Expand Up @@ -93,6 +94,8 @@ public event EventHandler<IDialog> Dialog

public event EventHandler<IPage> Page;

public event EventHandler<IPage> BackgroundPage;

public event EventHandler<IWebError> WebError;

public event EventHandler<IRequest> Request
Expand Down Expand Up @@ -143,13 +146,22 @@ public ITracing Tracing

public IReadOnlyList<IWorker> ServiceWorkers => _serviceWorkers;

public IReadOnlyList<IPage> BackgroundPages => _backgroundPages.ToList();

internal override void OnMessage(string method, JsonElement? serverParams)
{
switch (method)
{
case "close":
OnClose();
break;
case "backgroundPage":
{
var page = serverParams?.GetProperty("page").ToObject<Page>(_connection.DefaultJsonSerializerOptions);
_backgroundPages.Add(page);
BackgroundPage?.Invoke(this, page);
break;
}
case "bindingCall":
Channel_BindingCall(
serverParams?.GetProperty("binding").ToObject<BindingCall>(_connection.DefaultJsonSerializerOptions));
Expand Down
3 changes: 2 additions & 1 deletion src/Playwright/Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,8 @@ private async Task UpdateInterceptionAsync()
internal void OnClose()
{
IsClosed = true;
Context?._pages.Remove(this);
Context._pages.Remove(this);
Context._backgroundPages.Remove(this);
DisposeHarRouters();
Close?.Invoke(this, this);
}
Expand Down

0 comments on commit 9c725ca

Please sign in to comment.