github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/webdriver/chrome.go (about)

     1  package webdriver
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/tebeka/selenium"
     9  	"github.com/tebeka/selenium/chrome"
    10  )
    11  
    12  var (
    13  	chromeDriverPath = flag.String("chromedriver_path", "", "Path to the chromedriver binary")
    14  	chromePath       = flag.String("chrome_path", "", "Path to the chrome binary")
    15  )
    16  
    17  // ChromeWebDriver starts up ChromeDriver on the given port.
    18  func ChromeWebDriver(port int, options []selenium.ServiceOption) (*selenium.Service, selenium.WebDriver, error) {
    19  	if *chromePath == "" {
    20  		panic("-chrome_path not specified")
    21  	}
    22  	if *chromeDriverPath == "" {
    23  		panic("-chromedriver_path not specified")
    24  	}
    25  
    26  	service, err := selenium.NewChromeDriverService(*chromeDriverPath, port, options...)
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  
    31  	// Connect to the WebDriver instance running locally.
    32  	seleniumCapabilities := selenium.Capabilities{
    33  		"browserName": "chrome",
    34  	}
    35  
    36  	ChromeCapabilities := chrome.Capabilities{
    37  		Args: []string{"no-sandbox", "disable-dev-shm-usage"},
    38  	}
    39  	chromeAbsPath, err := filepath.Abs(*chromePath)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	ChromeCapabilities.Path = chromeAbsPath
    44  	seleniumCapabilities.AddChrome(ChromeCapabilities)
    45  
    46  	// selenium.NewChromeDriverService unconditionally specifies --url-base=wd/hub.
    47  	wd, err := selenium.NewRemote(
    48  		seleniumCapabilities,
    49  		fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port))
    50  	return service, wd, err
    51  }