69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package browser
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/tebeka/selenium"
|
|
)
|
|
|
|
// Browser -
|
|
type Browser struct {
|
|
SVC *selenium.Service
|
|
Opts Options
|
|
}
|
|
|
|
// Options -
|
|
type Options struct {
|
|
SeleniumPath string
|
|
DriverPath string
|
|
Port int
|
|
}
|
|
|
|
var svc *Browser
|
|
|
|
// NewService -
|
|
func NewService(opts Options) (err error) {
|
|
opt := []selenium.ServiceOption{
|
|
selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in.
|
|
selenium.GeckoDriver(opts.DriverPath), // Specify the path to GeckoDriver in order to use Firefox.
|
|
}
|
|
|
|
svc = &Browser{}
|
|
svc.Opts = opts
|
|
svc.SVC, err = selenium.NewSeleniumService(opts.SeleniumPath, opts.Port, opt...)
|
|
if err != nil {
|
|
svc = nil
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// StopService -
|
|
func StopService() error {
|
|
if svc == nil {
|
|
return nil
|
|
}
|
|
if err := svc.SVC.Stop(); err != nil {
|
|
return err
|
|
}
|
|
svc = nil
|
|
return nil
|
|
}
|
|
|
|
// NewWD -
|
|
func NewWD() (selenium.WebDriver, error) {
|
|
if svc == nil {
|
|
return nil, errors.New("service not init")
|
|
}
|
|
|
|
caps := selenium.Capabilities{"browserName": "firefox"}
|
|
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", svc.Opts.Port))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return wd, nil
|
|
}
|