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

chore: Remove default url when creating a headless client #82

Merged
merged 7 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions src/chrome_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ export class ChromeClient extends Client {
// FILE MARKER - METHODS - PUBLIC ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

/**
* Entry point for creating a headless chrome browser.
*
* @param buildOptions - Any extra options you wish to provide to customise how the headless browser sub process is ran
* - hostname: Defaults to localhost
* - port: Defaults to 9293
*
* @returns An instance of ChromeClient, that is now ready.
*/
public static async build(options: BuildOptions = {}): Promise<Client> {
// Setup build options
if (!options.debuggerPort) {
options.debuggerPort = 9292;
}
if (!options.defaultUrl) {
options.defaultUrl = "https://chromestatus.com";
}
if (!options.hostname) {
options.hostname = "localhost";
}
Expand All @@ -69,7 +75,6 @@ export class ChromeClient extends Client {
"--remote-debugging-port=" + options.debuggerPort,
"--disable-gpu",
"--no-sandbox",
options.defaultUrl,
];
return await Client.create(args, {
hostname: options.hostname,
Expand Down
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { existsSync, generateTimestamp } from "./utility.ts";

export interface BuildOptions {
debuggerPort?: number; // The port to start the debugger on for Chrome, so that we can connect to it. Defaults to 9292
defaultUrl?: string; // Default url chrome will open when it is ran. Defaults to "https://chromestatus.com"
hostname?: string; // The hostname the browser process starts on. If on host machine, this will be "localhost", if in docker, it will bee the container name. Defaults to localhost
binaryPath?: string; //The Full Path to the browser binary. If using an alternative chromium based browser, this field is necessary.
}
Expand Down Expand Up @@ -566,6 +565,7 @@ export class Client {
private async sendWebSocketMessage(
method: string,
params?: { [key: string]: unknown },
// The return value could literally be anything
// because we return a packet
// deno-lint-ignore no-explicit-any
): Promise<any> {
Expand Down Expand Up @@ -641,6 +641,7 @@ export class Client {
websocket.onopen = () => promise.resolve();
await promise;
const TempClient = new Client(websocket, browserProcess, browser);

await TempClient.sendWebSocketMessage("Page.enable");
await TempClient.sendWebSocketMessage("Runtime.enable");
return new Client(websocket, browserProcess, browser, firefoxProfilePath);
Expand Down
12 changes: 4 additions & 8 deletions src/firefox_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class FirefoxClient extends Client {
* @param buildOptions - Any extra options you wish to provide to customise how the headless browser sub process is ran
* - hostname: Defaults to 0.0.0.0 for macos/linux, 127.0.0.1 for windows
* - port: Defaults to 9293
* - url: Defaults to https://developer.mozilla.org/
*
* @returns An instance of FirefoxClient, that is now ready.
*/
Expand All @@ -67,25 +66,22 @@ export class FirefoxClient extends Client {
if (!buildOptions.debuggerPort) {
buildOptions.debuggerPort = defaultBuildOptions.debuggerServerPort;
}
if (!buildOptions.defaultUrl) {
buildOptions.defaultUrl = defaultBuildOptions.defaultUrl;
}

// Create the profile the browser will use. Create a test one so we can enable required options to enable communication with it
const tmpDirName = Deno.makeTempDirSync();

// Create the arguments we will use when spawning the headless browser
const args = [
buildOptions.binaryPath || getFirefoxPath(),
"--start-debugger-server",
buildOptions.debuggerPort.toString(),
"-headless",
"--remote-debugging-port",
buildOptions.debuggerPort.toString(),
"--headless",
"-profile",
tmpDirName,
"about:blank",
];
if (buildOptions.defaultUrl) {
args.push(buildOptions.defaultUrl);
}
// Create the sub process to start the browser
return await Client.create(
args,
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/chrome_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ Rhum.testPlan("tests/unit/chrome_client_test.ts", () => {
await Sinco.done();
},
);
Rhum.testCase("Uses the url when passed in to the parameters", async () => {
const Sinco = await ChromeClient.build({
defaultUrl: "https://drash.land",
});
await Sinco.assertUrlIs("https://drash.land/");
await Sinco.done();
});
Rhum.testCase(
"Uses the hostname when passed in to the parameters",
async () => {
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/firefox_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ Rhum.testPlan("tests/unit/firefox_client_test.ts", () => {
await Sinco.done();
},
);
Rhum.testCase("Uses the url when passed in to the parameters", async () => {
const Sinco = await FirefoxClient.build({
defaultUrl: "https://drash.land",
});
await Sinco.assertUrlIs("https://drash.land/");
await Sinco.done();
});
Rhum.testCase(
"Uses the hostname when passed in to the parameters",
async () => {
Expand Down