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: support proxy #46

Merged
merged 2 commits into from
Oct 27, 2023
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
43 changes: 39 additions & 4 deletions lib/macaca-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _ from './helper';
import initRedirectConsole from './redirect-console';
import controllers from './controllers';
import extraActions from './next-actions';
import os from 'os';

const DEFAULT_CONTEXT = 'DEFAULT_CONTEXT';

Expand All @@ -17,10 +18,32 @@ type TContextOptions = {
recordVideo?: object,
viewport?: object,
permissions?: string[],
proxy?: {
server: string;
username?: string;
password?: string;
};
};

type TDeviceCaps = {
port?: number;
locale?: string;
userAgent?: string;
recordVideo?: {
dir: string;
};
width?: number;
height?: number;
redirectConsole?: boolean;
proxy?: {
server: string;
username?: string;
password?: string;
};
};

class Playwright extends DriverBase {
args = null;
args: TDeviceCaps = null;
browserType = null;
browser = null;
browserContext = null;
Expand All @@ -36,16 +59,24 @@ class Playwright extends DriverBase {

static DEFAULT_CONTEXT = DEFAULT_CONTEXT;

async startDevice(caps = {}) {
async startDevice(caps: TDeviceCaps = {}) {
this.args = _.clone(caps);

const launchOptions = {
headless: true,
browserName: 'chromium',
...this.args,
};
delete launchOptions.port;
const browserName = launchOptions.browserName || 'chromium';
this.browserType = await playwright[browserName];
if (
this.args.proxy
&& launchOptions.browserName === 'chromium'
&& os.platform() === 'win32'
) {
// Browser proxy option is required for Chromium on Windows.
launchOptions.proxy = { server: 'per-context' };
}
this.browserType = await playwright[launchOptions.browserName];
this.browser = await this.browserType.launch(launchOptions);
const newContextOptions: TContextOptions = {
locale: this.args.locale,
Expand All @@ -56,6 +87,10 @@ class Playwright extends DriverBase {
],
};

if (this.args.proxy) {
newContextOptions.proxy = this.args.proxy;
}

if (this.args.userAgent) {
newContextOptions.userAgent = this.args.userAgent;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-playwright",
"version": "1.11.13",
"version": "1.11.14",
"description": "Macaca Playwright driver",
"keywords": [
"playwright",
Expand Down