diff --git a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs index 1b69937fc..006987547 100644 --- a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs +++ b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs @@ -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("e => e.files[0].name", input)); + Assert.Equal("contents of the file", await Page.EvaluateFunctionAsync(@"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("e => e.files[0].name", input)); + var exception = await Assert.ThrowsAsync(() => Page.EvaluateFunctionAsync(@"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("e => e.files[0].name", input)); + var exception = await Assert.ThrowsAsync(() => Page.EvaluateFunctionAsync(@"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() { diff --git a/lib/PuppeteerSharp/ElementHandle.cs b/lib/PuppeteerSharp/ElementHandle.cs index ff8c2482d..936f83540 100644 --- a/lib/PuppeteerSharp/ElementHandle.cs +++ b/lib/PuppeteerSharp/ElementHandle.cs @@ -203,9 +203,18 @@ public async Task ClickAsync(ClickOptions options = null) /// Sets the value of the file input to these paths. Paths are resolved using /// This method expects elementHandle to point to an input element /// Task - public Task UploadFileAsync(params string[] filePaths) + public Task UploadFileAsync(params string[] filePaths) => UploadFileAsync(true, filePaths); + + /// + /// Uploads files + /// + /// Sets the value of the file input to these paths. Paths are resolved using + /// Set to true to resolve paths using + /// This method expects elementHandle to point to an input element + /// Task + 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 {