intimate/utils.go

77 lines
1.6 KiB
Go
Raw Normal View History

package intimate
import (
2020-07-24 10:48:33 +00:00
"fmt"
"log"
2020-07-24 10:48:33 +00:00
"runtime"
"time"
2020-07-24 10:48:33 +00:00
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
var zeroTime time.Time
func init() {
tm, err := time.Parse("15:04:05", "0:00:00")
if err != nil {
log.Println(err)
}
zeroTime = tm
}
// ParseDuration time to duration eg: 1:40:00 -> time.Duration
func ParseDuration(dt string) (time.Duration, error) {
var parse []byte = []byte("00:00:00")
j := len(parse) - 1
for i := len(dt) - 1; i >= 0; i-- {
c := dt[i]
if c != ':' {
parse[j] = dt[i]
} else {
for parse[j] != ':' {
j--
}
}
j--
}
tdt, err := time.Parse("15:04:05", string(parse))
if err != nil {
return time.Duration(0), err
}
return tdt.Sub(zeroTime), nil
}
2020-07-24 10:48:33 +00:00
func GetChromeDriver(port int) selenium.WebDriver {
caps := selenium.Capabilities{"browserName": "chrome"}
chromecaps := chrome.Capabilities{}
err := chromecaps.AddExtension("/home/eson/test/ssh-key/0.1.2_0.crx")
if err != nil {
panic(err)
}
chromecaps.Args = append(chromecaps.Args, "--disk-cache-dir=/tmp/chromedriver-cache")
chromecaps.ExcludeSwitches = append(chromecaps.ExcludeSwitches, "enable-automation")
caps.AddChrome(chromecaps)
_, err = selenium.NewChromeDriverService("/usr/bin/chromedriver", port)
if err != nil {
panic(err)
}
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
runtime.SetFinalizer(wd, func(obj interface{}) {
if err := wd.Close(); err != nil {
log.Println(err)
}
})
wd.ExecuteScript("windows.navigator.webdriver = undefined", nil)
if err != nil {
panic(err)
}
return wd
}