Skip to content

Commit

Permalink
Marionette startup: catch error rather than wait
Browse files Browse the repository at this point in the history
It appears that Firefox doesn't always log its Marionette startup, so
instead this commit catches the network error when Marionette is yet to
start and just retries until Marionette is ready.
  • Loading branch information
tombh committed Jun 12, 2018
1 parent 2577ea8 commit a8ea195
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions interfacer/src/browsh/firefox.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

var (
marionette net.Conn
isMarionetteListening = false
ffCommandCount = 0
defaultFFPrefs = map[string]string{
"browser.startup.homepage": "'https://www.google.com'",
Expand Down Expand Up @@ -54,7 +53,6 @@ var (
)

func startHeadlessFirefox() {
var isMarionette, isListening bool
checkIfFirefoxIsAlreadyRunning()
Log("Starting Firefox in headless mode")
ensureFirefoxBinary()
Expand All @@ -81,9 +79,6 @@ func startHeadlessFirefox() {
}
in := bufio.NewScanner(stdout)
for in.Scan() {
isMarionette = strings.Contains(in.Text(), "Marionette")
isListening = strings.Contains(in.Text(), "Listening on port")
if isMarionette && isListening { isMarionetteListening = true }
Log("FF-CONSOLE: " + in.Text())
}
}
Expand Down Expand Up @@ -157,10 +152,29 @@ func startWERFirefox() {
// I've used Marionette here, simply because it was easier to reverse engineer
// from the Python Marionette package.
func firefoxMarionette() {
var (
err error
conn net.Conn
)
connected := false
Log("Attempting to connect to Firefox Marionette")
conn, err := net.Dial("tcp", "127.0.0.1:2828")
if err != nil {
Shutdown(err)
start := time.Now()
for time.Since(start) < 30 * time.Second {
conn, err = net.Dial("tcp", "127.0.0.1:2828")
if err != nil {
if !strings.Contains(err.Error(), "connection refused") {
Shutdown(err)
} else {
time.Sleep(10 * time.Millisecond)
continue
}
} else {
connected = true
break
}
}
if !connected {
Shutdown(errors.New("Failed to connect to Firefox's Marionette within 30 seconds"))
}
marionette = conn
readMarionette()
Expand Down Expand Up @@ -240,21 +254,11 @@ func setupFirefox() {
if (*timeLimit > 0) {
go beginTimeLimit()
}
waitForMarionette()
firefoxMarionette()
setDefaultPreferences()
installWebextension()
}

func waitForMarionette() {
start := time.Now()
for time.Since(start) < 60 * time.Second {
if isMarionetteListening { return }
time.Sleep(10 * time.Millisecond)
}
Shutdown(errors.New("Marionette didn't start within 60 seconds."))
}

func startFirefox() {
if !*isUseExistingFirefox {
writeString(0, 1, "Waiting for Firefox to connect...", tcell.StyleDefault)
Expand Down

0 comments on commit a8ea195

Please sign in to comment.