Add DontLaunchOnConnectFailure
and refine server connection logic
Introduced the `DontLaunchOnConnectFailure` option to prevent browser launch on connection failure when connecting to a Playwright server. Enhanced environment variable handling for server address based on browser type, improving flexibility and reliability in connection scenarios.
This commit is contained in:
@@ -59,6 +59,10 @@ type PlayWrightBrowserOptions struct {
|
||||
// PlayWrightServerAddress is the address of a PlayWright server to connect to.
|
||||
// Defaults to the value of the environment variable PLAYWRIGHT_SERVER_ADDRESS.
|
||||
PlayWrightServerAddress string
|
||||
|
||||
// DontLaunchOnConnectFailure will, if set, not launch the browser if the connection to the PlayWright server,
|
||||
// and return an error if the connection fails.
|
||||
DontLaunchOnConnectFailure bool
|
||||
}
|
||||
|
||||
func cookieToPlaywrightOptionalCookie(cookie Cookie) playwright.OptionalCookie {
|
||||
@@ -90,7 +94,7 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
Browser: PlayWrightBrowserSelectionFirefox,
|
||||
Timeout: &thirtySeconds,
|
||||
DarkMode: false,
|
||||
PlayWrightServerAddress: os.Getenv("PLAYWRIGHT_SERVER_ADDRESS"),
|
||||
PlayWrightServerAddress: "",
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@@ -112,7 +116,9 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
if o.DarkMode {
|
||||
opt.DarkMode = true
|
||||
}
|
||||
|
||||
if o.PlayWrightServerAddress != "" {
|
||||
opt.PlayWrightServerAddress = o.PlayWrightServerAddress
|
||||
}
|
||||
opt.ShowBrowser = o.ShowBrowser
|
||||
}
|
||||
|
||||
@@ -137,24 +143,41 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
switch opt.Browser {
|
||||
case PlayWrightBrowserSelectionChromium:
|
||||
bt = pw.Chromium
|
||||
if opt.PlayWrightServerAddress == "" {
|
||||
opt.PlayWrightServerAddress = os.Getenv("PLAYWRIGHT_SERVER_ADDRESS_CHROMIUM")
|
||||
}
|
||||
|
||||
case PlayWrightBrowserSelectionFirefox:
|
||||
bt = pw.Firefox
|
||||
if opt.PlayWrightServerAddress == "" {
|
||||
opt.PlayWrightServerAddress = os.Getenv("PLAYWRIGHT_SERVER_ADDRESS_FIREFOX")
|
||||
}
|
||||
|
||||
case PlayWrightBrowserSelectionWebKit:
|
||||
bt = pw.WebKit
|
||||
if opt.PlayWrightServerAddress == "" {
|
||||
opt.PlayWrightServerAddress = os.Getenv("PLAYWRIGHT_SERVER_ADDRESS_WEBKIT")
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, ErrInvalidBrowserSelection
|
||||
}
|
||||
var browser playwright.Browser
|
||||
|
||||
var launch = true
|
||||
if opt.PlayWrightServerAddress != "" {
|
||||
launch = false
|
||||
browser, err = bt.Connect(opt.PlayWrightServerAddress, playwright.BrowserTypeConnectOptions{})
|
||||
|
||||
if err != nil {
|
||||
if opt.DontLaunchOnConnectFailure {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
launch = true
|
||||
}
|
||||
}
|
||||
|
||||
if launch {
|
||||
browser, err = bt.Launch(playwright.BrowserTypeLaunchOptions{
|
||||
Headless: playwright.Bool(!opt.ShowBrowser),
|
||||
})
|
||||
|
Reference in New Issue
Block a user