Skip to content

Commit 473913e

Browse files
Code-DJkblok
authored andcommitted
Added ability to skip resolving file paths for UploadFileAsync. Fixes #1363 (#1364)
* Added ability to skip resolving file paths for UploadFileAsync. * Add Tests for UploadFileAsync
1 parent e5d2c7d commit 473913e

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

lib/PuppeteerSharp.Tests/InputTests/InputTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,56 @@ public async Task ShouldUploadTheFile()
5050
}", input));
5151
}
5252

53+
[Fact]
54+
public async Task ShouldUploadTheFileIfResolveFilePathIsFalse()
55+
{
56+
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
57+
var filePath = TestConstants.FileToUpload;
58+
var input = await Page.QuerySelectorAsync("input");
59+
await input.UploadFileAsync(false, filePath);
60+
Assert.Equal("file-to-upload.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
61+
Assert.Equal("contents of the file", await Page.EvaluateFunctionAsync<string>(@"e => {
62+
const reader = new FileReader();
63+
const promise = new Promise(fulfill => reader.onload = fulfill);
64+
reader.readAsText(e.files[0]);
65+
return promise.then(() => reader.result);
66+
}", input));
67+
}
68+
69+
[Fact]
70+
public async Task ShouldNotUploadTheFileIfPathIsWrong()
71+
{
72+
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
73+
var filePath = TestConstants.FileToUpload.Replace("file-to-upload.txt", "missing-file.txt");
74+
var input = await Page.QuerySelectorAsync("input");
75+
await input.UploadFileAsync(filePath);
76+
Assert.Equal("missing-file.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
77+
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<string>(@"e => {
78+
const reader = new FileReader();
79+
const promise = new Promise(fulfill => reader.onload = fulfill);
80+
reader.readAsText(e.files[0]);
81+
return promise.then(() => reader.result);
82+
}", input));
83+
Assert.Contains("Promise was collected", exception.Message);
84+
}
85+
86+
[Fact]
87+
public async Task ShouldNotUploadTheFileIfPathIsWrongAndResolveFilePathIsFalse()
88+
{
89+
await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");
90+
var filePath = TestConstants.FileToUpload.Replace("file-to-upload.txt", "missing-file.txt");
91+
var input = await Page.QuerySelectorAsync("input");
92+
await input.UploadFileAsync(false, filePath);
93+
Assert.Equal("missing-file.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));
94+
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<string>(@"e => {
95+
const reader = new FileReader();
96+
const promise = new Promise(fulfill => reader.onload = fulfill);
97+
reader.readAsText(e.files[0]);
98+
return promise.then(() => reader.result);
99+
}", input));
100+
Assert.Contains("Promise was collected", exception.Message);
101+
}
102+
53103
[Fact]
54104
public async Task ShouldResizeTheTextarea()
55105
{

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,18 @@ public async Task ClickAsync(ClickOptions options = null)
203203
/// <param name="filePaths">Sets the value of the file input to these paths. Paths are resolved using <see cref="Path.GetFullPath(string)"/></param>
204204
/// <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>
205205
/// <returns>Task</returns>
206-
public Task UploadFileAsync(params string[] filePaths)
206+
public Task UploadFileAsync(params string[] filePaths) => UploadFileAsync(true, filePaths);
207+
208+
/// <summary>
209+
/// Uploads files
210+
/// </summary>
211+
/// <param name="filePaths">Sets the value of the file input to these paths. Paths are resolved using <see cref="Path.GetFullPath(string)"/></param>
212+
/// <param name="resolveFilePaths">Set to true to resolve paths using <see cref="Path.GetFullPath(string)"/></param>
213+
/// <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>
214+
/// <returns>Task</returns>
215+
public Task UploadFileAsync(bool resolveFilePaths, params string[] filePaths)
207216
{
208-
var files = filePaths.Select(Path.GetFullPath).ToArray();
217+
var files = resolveFilePaths ? filePaths.Select(Path.GetFullPath).ToArray() : filePaths;
209218
var objectId = RemoteObject.ObjectId;
210219
return Client.SendAsync("DOM.setFileInputFiles", new DomSetFileInputFilesRequest
211220
{

0 commit comments

Comments
 (0)