Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support setting files on inputs #118

Merged
merged 6 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 +1,2 @@
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";
15 changes: 14 additions & 1 deletion tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ 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" />
`);
}
}

export const server = new Drash.Server({
resources: [HomeResource, JSResource, PopupsResource],
resources: [HomeResource, JSResource, PopupsResource, FileInputResource],
protocol: "http",
port: 1447,
hostname: "localhost",
Expand Down
Loading