Skip to content

Commit

Permalink
IsCanceled and IsCompleted properties to allow users to perform bette…
Browse files Browse the repository at this point in the history
…r logic decisions
  • Loading branch information
msm-tomlonghurst committed Aug 16, 2023
1 parent 8564e6a commit 7f5130a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/Playwright.Tests/ScreencastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,20 @@ public async Task ShouldCaptureStaticPageInPersistentContext()

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

var context = await BrowserType.LaunchPersistentContextAsync(userDirectory.Path, new()
{
RecordVideoDir = tempDirectory.Path,
Headless = false
RecordVideoDir = tempDirectory.Path
});

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

try
{
await page.Video!.PathAsync();
}
catch (TaskCanceledException)
{
// Ignored
}
Assert.That(page.Video!.IsCompleted, Is.True);
Assert.That(page.Video!.IsCanceled, Is.True);
}
}
6 changes: 6 additions & 0 deletions src/Playwright/API/Generated/IVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public partial interface IVideo
/// </summary>
/// <param name="path">Path where the video should be saved.</param>
Task SaveAsAsync(string path);

/// <summary><para>True if the video file is not waiting for completion.</para></summary>
bool IsCompleted();

/// <summary><para>True if the video file was canceled.</para></summary>
bool IsCanceled();
}

#nullable disable
4 changes: 4 additions & 0 deletions src/Playwright/Core/Video.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public async Task SaveAsAsync(string path)
await artifact.SaveAsAsync(path).ConfigureAwait(false);
}

public bool IsCompleted() => _artifactTcs.Task.IsCompleted;

public bool IsCanceled() => _artifactTcs.Task.IsCanceled;

internal void ArtifactReady(Artifact artifact) => _artifactTcs.TrySetResult(artifact);

private void TrySetCanceledOnPageClose(Page page) => page.ClosedOrCrashedTcs.Task.ContinueWith(_ => _artifactTcs.TrySetCanceled(), TaskScheduler.Default);
Expand Down

0 comments on commit 7f5130a

Please sign in to comment.