From 345b2020e3ebcf20af30e25a5bf550238a3b83b3 Mon Sep 17 00:00:00 2001 From: nicholasvuono Date: Wed, 23 Feb 2022 23:24:10 -0500 Subject: [PATCH] feat: add launch with custom browser option supressing the installation process, updated tests --- playwright.go | 21 +++++++++++++++++++++ playwright_test.go | 15 +++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/playwright.go b/playwright.go index 5bf9c03..a503f06 100644 --- a/playwright.go +++ b/playwright.go @@ -40,6 +40,27 @@ func (p *Playwright) Launch(args []string) { p.Browser = browser } +func (p *Playwright) LaunchCustomBrowser(args []string, browserPath string) { + runOptions := playwright.RunOptions{ + DriverDirectory: "./", + SkipInstallBrowsers: true, + Browsers: []string {browserPath}, + } + pw, err := playwright.Run(&runOptions) + if err != nil { + log.Fatalf("could not start playwright: %v", err) + } + browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(false), + Args: args, + }) + if err != nil { + log.Fatalf("could not launch browser: %v", err) + } + p.Self = pw + p.Browser = browser +} + //NewPage opens a new page within the browser func (p *Playwright) NewPage() { page, err := p.Browser.NewPage() diff --git a/playwright_test.go b/playwright_test.go index 06e4640..b165692 100644 --- a/playwright_test.go +++ b/playwright_test.go @@ -9,6 +9,7 @@ import ( var tests = []func(t *testing.T){ TestPlaywright, + TestPlaywrightCustomBrowser, } func TestPlaywright(t *testing.T) { @@ -21,6 +22,20 @@ func TestPlaywright(t *testing.T) { pw.NewPage() pw.Goto("https://www.google.com", opts2) pw.WaitForSelector("//html/body/div[1]/div[2]", opts3) + pw.Kill() +} + +func TestPlaywrightCustomBrowser(t *testing.T) { + var pw Playwright + var opts []string + var opts2 playwright.PageGotoOptions + var opts3 playwright.PageWaitForSelectorOptions + + pw.LaunchCustomBrowser(opts, "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe") + pw.NewPage() + pw.Goto("https://www.google.com", opts2) + pw.WaitForSelector("//html/body/div[1]/div[2]", opts3) + pw.Kill() } func TestAll(t *testing.T) {