Skip to content

Commit

Permalink
Add missing FileChooser Accept test (hardkoded#1838)
Browse files Browse the repository at this point in the history
* Add missing FileChooser Accept test

closes hardkoded#1751

* implement validation
  • Loading branch information
kblok authored Oct 5, 2021
1 parent daf9a55 commit 590fd45
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/PuppeteerSharp.Tests/InputTests/FileChooserAcceptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ await Assert.ThrowsAsync<PuppeteerException>(() => waitForTask.Result.AcceptAsyn
"./assets/pptr.png"));
}

[PuppeteerTest("input.spec.ts", "FileChooser.accept", "should fail for non-existent files")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldFailForNonExistentFiles()
{
await Page.SetContentAsync("<input type=file>");
var waitForTask = Page.WaitForFileChooserAsync();

await Task.WhenAll(
waitForTask,
Page.ClickAsync("input"));

await Assert.ThrowsAsync<PuppeteerException>(() => waitForTask.Result.AcceptAsync("file-does-not-exist.txt"));
}

[PuppeteerTest("input.spec.ts", "FileChooser.accept", "should fail when accepting file chooser twice")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldFailWhenAcceptingFileChooserTwice()
Expand Down
34 changes: 25 additions & 9 deletions lib/PuppeteerSharp/ElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public async Task UploadFileAsync(bool resolveFilePaths, params string[] filePat
}).ConfigureAwait(false);
var backendNodeId = node.Node.BackendNodeId;

if (!filePaths.Any())
if (!filePaths.Any() || filePaths == null)
{
await EvaluateFunctionAsync(@"(element) => {
element.files = new DataTransfer().files;
Expand All @@ -252,6 +252,7 @@ await EvaluateFunctionAsync(@"(element) => {
else
{
var files = resolveFilePaths ? filePaths.Select(Path.GetFullPath).ToArray() : filePaths;
CheckForFileAccess(files);
await Client.SendAsync("DOM.setFileInputFiles", new DomSetFileInputFilesRequest
{
ObjectId = objectId,
Expand All @@ -261,6 +262,21 @@ await EvaluateFunctionAsync(@"(element) => {
}
}

private void CheckForFileAccess(string[] files)
{
foreach (var file in files)
{
try
{
File.Open(file, FileMode.Open).Dispose();
}
catch (Exception ex)
{
throw new PuppeteerException($"{files} does not exist or is not readable", ex);
}
}
}

/// <summary>
/// Scrolls element into view if needed, and then uses <see cref="Touchscreen.TapAsync(decimal, decimal)"/> to tap in the center of the element.
/// </summary>
Expand Down Expand Up @@ -536,8 +552,8 @@ public async Task DragEnterAsync(DragData data)
}

await ScrollIntoViewIfNeededAsync().ConfigureAwait(false);
var start = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DragEnterAsync(start.x, start.y, data).ConfigureAwait(false);
var (x, y) = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DragEnterAsync(x, y, data).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -553,8 +569,8 @@ public async Task DragOverAsync(DragData data)
}

await ScrollIntoViewIfNeededAsync().ConfigureAwait(false);
var start = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DragOverAsync(start.x, start.y, data).ConfigureAwait(false);
var (x, y) = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DragOverAsync(x, y, data).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -570,8 +586,8 @@ public async Task DropAsync(DragData data)
}

await ScrollIntoViewIfNeededAsync().ConfigureAwait(false);
var start = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DropAsync(start.x, start.y, data).ConfigureAwait(false);
var (x, y) = await ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DropAsync(x, y, data).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -593,9 +609,9 @@ public async Task DragAndDropAsync(ElementHandle target, int delay = 0)
}

await ScrollIntoViewIfNeededAsync().ConfigureAwait(false);
var start = await ClickablePointAsync().ConfigureAwait(false);
var (x, y) = await ClickablePointAsync().ConfigureAwait(false);
var targetPoint = await target.ClickablePointAsync().ConfigureAwait(false);
await Page.Mouse.DragAndDropAsync(start.x, start.y, targetPoint.x, targetPoint.y, delay).ConfigureAwait(false);
await Page.Mouse.DragAndDropAsync(x, y, targetPoint.x, targetPoint.y, delay).ConfigureAwait(false);
}

private async Task<(decimal x, decimal y)> ClickablePointAsync()
Expand Down

0 comments on commit 590fd45

Please sign in to comment.