Skip to content

Commit

Permalink
chore: support proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng committed Oct 27, 2023
1 parent a2ef81a commit b0449c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 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,20 @@ 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 (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 +83,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

0 comments on commit b0449c2

Please sign in to comment.