Skip to content

Commit

Permalink
Added ability to skip resolving file paths for UploadFileAsync. Fixes h…
Browse files Browse the repository at this point in the history
…ardkoded#1363 (hardkoded#1364)

* Added ability to skip resolving file paths for UploadFileAsync.

* Add Tests for UploadFileAsync
  • Loading branch information
Code-DJ authored and kblok committed Jan 24, 2020
1 parent e5d2c7d commit 473913e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
50 changes: 50 additions & 0 deletions lib/PuppeteerSharp.Tests/InputTests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,56 @@ public async Task ShouldUploadTheFile()
}", input));
}

[Fact]
public async Task ShouldUploadTheFileIfResolveFilePathIsFalse()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
var filePath = TestConstants.FileToUpload;
var input = await Page.QuerySelectorAsync("input");
await input.UploadFileAsync(false, filePath);
Assert.Equal("file-to-upload.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
Assert.Equal("contents of the file", await Page.EvaluateFunctionAsync<string>(@"e => {
const reader = new FileReader();
const promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(e.files[0]);
return promise.then(() => reader.result);
}", input));
}

[Fact]
public async Task ShouldNotUploadTheFileIfPathIsWrong()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
var filePath = TestConstants.FileToUpload.Replace("file-to-upload.txt", "missing-file.txt");
var input = await Page.QuerySelectorAsync("input");
await input.UploadFileAsync(filePath);
Assert.Equal("missing-file.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<string>(@"e => {
const reader = new FileReader();
const promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(e.files[0]);
return promise.then(() => reader.result);
}", input));
Assert.Contains("Promise was collected", exception.Message);
}

[Fact]
public async Task ShouldNotUploadTheFileIfPathIsWrongAndResolveFilePathIsFalse()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
var filePath = TestConstants.FileToUpload.Replace("file-to-upload.txt", "missing-file.txt");
var input = await Page.QuerySelectorAsync("input");
await input.UploadFileAsync(false, filePath);
Assert.Equal("missing-file.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<string>(@"e => {
const reader = new FileReader();
const promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(e.files[0]);
return promise.then(() => reader.result);
}", input));
Assert.Contains("Promise was collected", exception.Message);
}

[Fact]
public async Task ShouldResizeTheTextarea()
{
Expand Down
13 changes: 11 additions & 2 deletions lib/PuppeteerSharp/ElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,18 @@ public async Task ClickAsync(ClickOptions options = null)
/// <param name="filePaths">Sets the value of the file input to these paths. Paths are resolved using <see cref="Path.GetFullPath(string)"/></param>
/// <remarks>This method expects <c>elementHandle</c> to point to an <c>input element</c> <see href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input"/> </remarks>
/// <returns>Task</returns>
public Task UploadFileAsync(params string[] filePaths)
public Task UploadFileAsync(params string[] filePaths) => UploadFileAsync(true, filePaths);

/// <summary>
/// Uploads files
/// </summary>
/// <param name="filePaths">Sets the value of the file input to these paths. Paths are resolved using <see cref="Path.GetFullPath(string)"/></param>
/// <param name="resolveFilePaths">Set to true to resolve paths using <see cref="Path.GetFullPath(string)"/></param>
/// <remarks>This method expects <c>elementHandle</c> to point to an <c>input element</c> <see href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input"/> </remarks>
/// <returns>Task</returns>
public Task UploadFileAsync(bool resolveFilePaths, params string[] filePaths)
{
var files = filePaths.Select(Path.GetFullPath).ToArray();
var files = resolveFilePaths ? filePaths.Select(Path.GetFullPath).ToArray() : filePaths;
var objectId = RemoteObject.ObjectId;
return Client.SendAsync("DOM.setFileInputFiles", new DomSetFileInputFilesRequest
{
Expand Down

0 comments on commit 473913e

Please sign in to comment.