diff --git a/example/tests/elementActions.test.js b/example/tests/elementActions.test.js index 021563a..67d82c4 100644 --- a/example/tests/elementActions.test.js +++ b/example/tests/elementActions.test.js @@ -304,4 +304,36 @@ describe("Element Actions", () => { expect(coordinates.x).toEqual(expectedXCoordinate); expect(coordinates.y).toEqual(expectedYCoordinate); }); + + it("should upload a file when an absolute path is provided", async () => { + //Arrange + const filePath = process.cwd() + "\\testFiles\\Dummy.txt"; + const remotePath = "C:\\fakepath\\Dummy.txt"; + const uploadElement = new Element("#uploadFile"); + const resultElement = new Element("#uploadedFilePath"); + await page.goto("https://demoqa.com/upload-download"); + + //Act + await uploadElement.uploadFile(filePath, true); + + //Assert + expect(await resultElement.text()).toEqual(remotePath); + }); + + it("should upload a file when a relative path is provided", async () => { + //Arrange + const filePath = "\\testFiles\\Dummy.txt"; + const remotePath = "C:\\fakepath\\Dummy.txt"; + const uploadElement = new Element("#uploadFile"); + const resultElement = new Element("#uploadedFilePath"); + await page.goto("https://demoqa.com/upload-download"); + + //Act + await uploadElement.uploadFile(filePath, false); + + //Assert + expect(await resultElement.text()).toEqual(remotePath); + }); + + }); diff --git a/framework/Element.js b/framework/Element.js index 2411c0e..9678ae1 100644 --- a/framework/Element.js +++ b/framework/Element.js @@ -195,4 +195,11 @@ export default class Element { context.fill(); }); } + + async uploadFile(filePath, isAbsolutePath) { + if (!isAbsolutePath) filePath = process.cwd() + filePath; + console.log(`Uploading a file with path ${filePath}`); + const elementHandle = await this.wait(); + await elementHandle.setInputFiles(filePath); + } } \ No newline at end of file diff --git a/testFiles/Dummy.txt b/testFiles/Dummy.txt new file mode 100644 index 0000000..acb8807 --- /dev/null +++ b/testFiles/Dummy.txt @@ -0,0 +1 @@ +Dummy test \ No newline at end of file