Skip to content

Commit

Permalink
Fix Video Hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-tomlonghurst committed Aug 15, 2023
1 parent efc4dff commit 20ec304
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
29 changes: 29 additions & 0 deletions src/Playwright.Tests/ScreencastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,33 @@ public async Task ShouldCaptureStaticPageInPersistentContext()

Assert.IsNotEmpty(new DirectoryInfo(tempDirectory.Path).GetFiles("*.webm"));
}

[PlaywrightTest("screencast.spec.ts", "Video does not hang if page not interacted with")]
[Timeout(30_000)]
public async Task VideoDoesNotHangIfNotHeadlessAndPageNotInteractedWith()
{
using var tempDirectory = new TempDirectory();

var chrome = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false
});

var context = await chrome.NewContextAsync(new BrowserNewContextOptions
{
RecordVideoDir = tempDirectory.Path,
});

var page = await context.NewPageAsync();
await page.CloseAsync();

try
{
await page.Video!.PathAsync();
}
catch (TaskCanceledException)
{
// Ignored
}
}
}
20 changes: 8 additions & 12 deletions src/Playwright/Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ internal Page(IChannelOwner parent, string guid, PageInitializer initializer) :
PageError?.Invoke(this, $"{e.Error.Name}: {e.Error.Message}");
};
_channel.Video += (_, artifact) => ForceVideo().ArtifactReady(artifact);

_channel.Video += (_, artifact) => _video.ArtifactReady(artifact);

if (Context.Options.RecordVideoDir != null)
{
_video = new Video(this, _channel.Connection);
}

_channel.FileChooser += (_, e) => _fileChooserImpl?.Invoke(this, new FileChooser(this, e.Element.Object, e.IsMultiple));
_channel.Worker += (_, worker) =>
Expand Down Expand Up @@ -237,15 +243,7 @@ public ITouchscreen Touchscreen

public IVideo Video
{
get
{
if (Context.Options.RecordVideoDir == null)
{
return null;
}

return ForceVideo();
}
get => _video;
set => _video = value as Video;
}

Expand Down Expand Up @@ -1276,8 +1274,6 @@ private Task InnerExposeBindingAsync(string name, Delegate callback, bool handle
return _channel.ExposeBindingAsync(name, handle);
}

private Video ForceVideo() => _video ??= new(this, _channel.Connection);

private FrameSetInputFilesOptions Map(PageSetInputFilesOptions options)
{
if (options == null)
Expand Down

0 comments on commit 20ec304

Please sign in to comment.