diff --git a/interfacer/src/browsh/firefox.go b/interfacer/src/browsh/firefox.go index da6d6c1e..906caa22 100644 --- a/interfacer/src/browsh/firefox.go +++ b/interfacer/src/browsh/firefox.go @@ -8,7 +8,6 @@ import ( "net" "os" "os/exec" - "regexp" "runtime" "strings" "time" @@ -21,6 +20,7 @@ import ( var ( marionette net.Conn ffCommandCount = 0 + pid = -1 defaultFFPrefs = map[string]string{ "startup.homepage_welcome_url.additional": "''", "devtools.errorconsole.enabled": "true", @@ -52,7 +52,9 @@ var ( ) func startHeadlessFirefox() { - checkIfFirefoxIsAlreadyRunning() + if pid != -1 { + Shutdown(errors.New("A headless Firefox is already running")) + } Log("Starting Firefox in headless mode") firefoxPath := ensureFirefoxBinary() ensureFirefoxVersion(firefoxPath) @@ -78,23 +80,13 @@ func startHeadlessFirefox() { if err := firefoxProcess.Start(); err != nil { Shutdown(err) } + pid = firefoxProcess.Process.Pid in := bufio.NewScanner(stdout) for in.Scan() { Log("FF-CONSOLE: " + in.Text()) } } -func checkIfFirefoxIsAlreadyRunning() { - if runtime.GOOS == "windows" { - return - } - processes := Shell("ps aux") - r, _ := regexp.Compile("firefox.*--headless") - if r.MatchString(processes) { - Shutdown(errors.New("A headless Firefox is already running")) - } -} - func ensureFirefoxBinary() string { path := viper.GetString("firefox.path") if path == "firefox" { @@ -166,6 +158,7 @@ func startWERFirefox() { if err := firefoxProcess.Start(); err != nil { Shutdown(err) } + pid = firefoxProcess.Process.Pid in := bufio.NewScanner(stdout) for in.Scan() { if strings.Contains(in.Text(), "JavaScript strict") || @@ -309,5 +302,8 @@ func startFirefox() { } func quitFirefox() { - sendFirefoxCommand("quitApplication", map[string]interface{}{}) + process, err := os.FindProcess(pid) + if err == nil { + process.Kill() + } } diff --git a/interfacer/src/browsh/raw_text_server.go b/interfacer/src/browsh/raw_text_server.go index 684bd08e..b48327c2 100644 --- a/interfacer/src/browsh/raw_text_server.go +++ b/interfacer/src/browsh/raw_text_server.go @@ -80,6 +80,10 @@ func HTTPServerStart() { } } +func HTTPServerStop() { + quitFirefox() +} + func setupRateLimiter() *stdlib.Middleware { rate, err := limiter.NewRateFromFormatted(viper.GetString("http-server.rate-limit")) if err != nil { diff --git a/interfacer/test/http-server/setup.go b/interfacer/test/http-server/setup.go index e3d3d919..6e2ea17c 100644 --- a/interfacer/test/http-server/setup.go +++ b/interfacer/test/http-server/setup.go @@ -72,5 +72,5 @@ var _ = ginkgo.BeforeSuite(func() { }) var _ = ginkgo.AfterSuite(func() { - browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill") + browsh.HTTPServerStop() }) diff --git a/interfacer/test/tty/setup.go b/interfacer/test/tty/setup.go index 71ec0797..5a089638 100644 --- a/interfacer/test/tty/setup.go +++ b/interfacer/test/tty/setup.go @@ -225,5 +225,5 @@ var _ = ginkgo.BeforeSuite(func() { }) var _ = ginkgo.AfterSuite(func() { - browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill") + browsh.HTTPServerStop() }) diff --git a/webext/contrib/firefoxheadless.sh b/webext/contrib/firefoxheadless.sh deleted file mode 100755 index 82f3fdfd..00000000 --- a/webext/contrib/firefoxheadless.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -if [[ "$1" = "kill" ]]; then - kill $(ps aux|grep headless|grep 'profile /tmp'| tr -s ' ' | cut -d ' ' -f2) -else - FIREFOX_BIN=${FIREFOX:-firefox} - $FIREFOX_BIN --headless "$@" -fi