-
Notifications
You must be signed in to change notification settings - Fork 567
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
Added support for browser-type and browser-executable-path #816
Changes from all commits
4786882
18bb93f
5429e6b
f45c5f8
6620c45
62fabce
50f0a14
44723ca
4bbc2a0
aa51011
e5d5822
935a519
fa5d8e7
86cd2fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ package browser | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/playwright-community/playwright-go" | ||
"github.com/sirupsen/logrus" | ||
|
@@ -17,7 +19,9 @@ const DEFAULT_TIMEOUT float64 = 300000 | |
|
||
// Client client for browser based Identity Provider | ||
type Client struct { | ||
Headless bool | ||
BrowserType string | ||
BrowserExecutablePath string | ||
Headless bool | ||
// Setup alternative directory to download playwright browsers to | ||
BrowserDriverDir string | ||
Timeout int | ||
|
@@ -26,12 +30,24 @@ type Client struct { | |
// New create new browser based client | ||
func New(idpAccount *cfg.IDPAccount) (*Client, error) { | ||
return &Client{ | ||
Headless: idpAccount.Headless, | ||
BrowserDriverDir: idpAccount.BrowserDriverDir, | ||
Timeout: idpAccount.Timeout, | ||
Headless: idpAccount.Headless, | ||
BrowserDriverDir: idpAccount.BrowserDriverDir, | ||
BrowserType: strings.ToLower(idpAccount.BrowserType), | ||
BrowserExecutablePath: idpAccount.BrowserExecutablePath, | ||
Timeout: idpAccount.Timeout, | ||
}, nil | ||
} | ||
|
||
// contains checks if a string is present in a slice | ||
func contains(s []string, str string) bool { | ||
for _, v := range s { | ||
if v == str { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
func (cl *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) { | ||
runOptions := playwright.RunOptions{} | ||
if cl.BrowserDriverDir != "" { | ||
|
@@ -56,10 +72,37 @@ func (cl *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) | |
Headless: playwright.Bool(cl.Headless), | ||
} | ||
|
||
// currently using Chromium as it is widely supported for Identity providers | ||
validBrowserTypes := []string{"chromium", "firefox", "webkit", "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary"} | ||
if len(cl.BrowserType) > 0 && !contains(validBrowserTypes, cl.BrowserType) { | ||
return "", fmt.Errorf("invalid browser-type: '%s', only %s are allowed", cl.BrowserType, validBrowserTypes) | ||
} | ||
|
||
if cl.BrowserType != "" { | ||
logger.Info(fmt.Sprintf("Setting browser type: %s", cl.BrowserType)) | ||
launchOptions.Channel = playwright.String(cl.BrowserType) | ||
} | ||
|
||
// Default browser is Chromium as it is widely supported for Identity providers, | ||
// It can also be set to the other playwright browsers: Firefox and WebKit | ||
browserType := pw.Chromium | ||
if cl.BrowserType == "firefox" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eliat123 would there be more browsertypes mapped here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gliptak Nope. Only these browsertypes are supported. According to https://playwright.dev/docs/browsers :
Chromium will be the default option, but if "webkit" or "firefox" are selected, they will be used instead. This ensures the most available options. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will be browserType be pw.Chromium when cl.BrowserType == 'msedge' and is this as expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gliptak The answer is yes. Here is the section from Playwright docs about Google Chrome & Microsoft Edge:
ref: https://playwright.dev/docs/browsers#google-chrome--microsoft-edge |
||
browserType = pw.Firefox | ||
} else if cl.BrowserType == "webkit" { | ||
browserType = pw.WebKit | ||
} | ||
|
||
// You can set the path to a browser executable to run instead of the playwright-go bundled one. If `executablePath` | ||
// is a relative path, then it is resolved relative to the current working directory. | ||
// Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk. see: | ||
if len(cl.BrowserExecutablePath) > 0 { | ||
logger.Info(fmt.Sprintf("Setting browser executable path: %s", cl.BrowserExecutablePath)) | ||
launchOptions.ExecutablePath = &cl.BrowserExecutablePath | ||
} | ||
|
||
// currently using the main browsers supported by Playwright: Chromium, Firefox or Webkit | ||
// | ||
// this is a sandboxed browser window so password managers and addons are separate | ||
browser, err := pw.Chromium.Launch(launchOptions) | ||
browser, err := browserType.Launch(launchOptions) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why differentiate all these
browsertypes
if playwright only supports firefox, chrome and webkit?I see #816 (comment)
maybe the if statement below is to be updated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @gliptak, Thank you for your review.
Playwright does support all of those browser types, see:
https://playwright.dev/docs/browsers and https://github.com/playwright-community/playwright-go/blob/main/generated-structs.go#L691C4-L695
The main idea behind this PR is that the user can choose a browser type which is already installed on their system.
If you choose a browser you do not have on your system, you will get a relevant error message.
For example, I tried to set
--browser-type=msedge
. Since I did not havemsedge
installed I got the following notification:After I installed
msedge
, it used the installed one:Furthermore, If your browser is installed on a non-default location, you can utilize
--browser-executable-path
, and it will start the browser from that path.Hope this explains it.