Skip to content

Commit

Permalink
feat: Support setting files on inputs (#118)
Browse files Browse the repository at this point in the history
* feat: Support setting files on inputs

* add tests

* fix tests

* fix again
  • Loading branch information
ebebbington authored May 30, 2022
1 parent 67ac788 commit dd5e657
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 75 deletions.
71 changes: 71 additions & 0 deletions src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,77 @@ export class Element {
this.#protocol = protocol;
}

/**
* Sets a file for a file input
*
* @param path - The remote path of the file to attach
*
* @example
* ```js
* import { resolve } from "https://deno.land/std@0.136.0/path/mod.ts";
* const fileInput = await page.querySelector("input[type='file']");
* await fileInput.file(resolve("./logo.png"));
* ```
*/
public async file(path: string): Promise<void> {
return await this.files(path);
}

/**
* Sets many files for a file input
*
* @param files - The list of remote files to attach
*
* @example
* ```js
* import { resolve } from "https://deno.land/std@0.136.0/path/mod.ts";
* const fileInput = await page.querySelector("input[type='file']");
* await fileInput.files(resolve("./logo.png"));
* ```
*/
public async files(...files: string[]) {
if (files.length > 1) {
const isMultiple = await this.#page.evaluate(
`${this.#method}('${this.#selector}').hasAttribute('multiple')`,
);
if (!isMultiple) {
throw new Error(
"Trying to set files on a file input without the 'multiple' attribute",
);
}
}

const name = await this.#page.evaluate(
`${this.#method}('${this.#selector}').nodeName`,
);
if (name !== "INPUT") {
throw new Error("Trying to set a file on an element that isnt an input");
}
const type = await this.#page.evaluate(
`${this.#method}('${this.#selector}').type`,
);
if (type !== "file") {
throw new Error(
'Trying to set a file on an input that is not of type "file"',
);
}

const { node } = await this.#protocol.send<
ProtocolTypes.DOM.DescribeNodeRequest,
ProtocolTypes.DOM.DescribeNodeResponse
>("DOM.describeNode", {
objectId: this.#objectId,
});
await this.#protocol.send<ProtocolTypes.DOM.SetFileInputFilesRequest, null>(
"DOM.setFileInputFiles",
{
files: files,
objectId: this.#objectId,
backendNodeId: node.backendNodeId,
},
);
}

/**
* Get the value of this element, or set the value
*
Expand Down
1 change: 1 addition & 0 deletions tests/deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as Drash from "https://deno.land/x/drash@v2.5.4/mod.ts";
export { resolve } from "https://deno.land/std@0.136.0/path/mod.ts";
export { delay } from "https://deno.land/std@0.126.0/async/delay.ts";
14 changes: 14 additions & 0 deletions tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ class PopupsResource extends Drash.Resource {
}
}

class FileInputResource extends Drash.Resource {
public paths = ["/file-input"];

public GET(_r: Drash.Request, res: Drash.Response) {
return res.html(`
<p></p>
<input id="text" type="text" />
<input type="file" multiple id="multiple-file" />
<input type="file" id="single-file" />
`);
}
}

class WaitForRequestsResource extends Drash.Resource {
public paths = ["/wait-for-requests"];

Expand Down Expand Up @@ -55,6 +68,7 @@ export const server = new Drash.Server({
JSResource,
PopupsResource,
WaitForRequestsResource,
FileInputResource,
],
protocol: "http",
port: 1447,
Expand Down
Loading

0 comments on commit dd5e657

Please sign in to comment.