I am trying to start selenium and the headless Xvfb X server using a init script and the daemon tool.
But when I start my environment using this method, Firefox fails to launch. It looks like it can not find Firefox 2 and then stops. When I start that environment normally, it also does not find Firefox 2 but then it searches for Firefox 3, finds it and starts properly.
This is my init script:
#!/bin/bash
#
# Starts a Selenium-RC headless environment
# License: https://github.com/amenk/SelfScripts/blob/master/LICENSE.md
#
# BETA
# Fixmes:
# - We should take care of running the environment under a non-priviledged user
#
# Source function library.
. /lib/lsb/init-functions
LOG=/var/log/selenium.log
DISPLAY_NO=99
JAVA=/usr/bin/java
XVFB=/usr/bin/Xvfb
SELENIUM_JAR=/opt/selenium-server-standalone-2.20.0.jar
FIREFOX_TEMPLATE=/home/amenk/.mozilla/firefox/aph73r3f.selenium/
start() {
log_daemon_msg "Starting Xvfb"
daemon --dbglog=$LOG --errlog=$LOG --stdout=$LOG --stderr=$LOG\
--name Xvfb -- $XVFB :$DISPLAY_NO -ac -screen 0 1024x768x8
log_end_msg $?
log_daemon_msg "Starting Selenium RC"
daemon --dbglog=$LOG --errlog=$LOG --stdout=$LOG --stderr=$LOG\
--name Selenium-RC --env=DISPLAY=$DISPLAY_NO\
-- java -jar $SELENIUM_JAR -log /var/log/selenium-debug.log -browserSideLog -firefoxProfileTemplate $FIREFOX_TEMPLATE
log_end_msg $?
return
}
stop() {
log_daemon_msg "Stopping Selenium RC"
daemon --stop --name Selenium-RC
log_end_msg $?
log_daemon_msg "Stopping Xvfb"
daemon --stop --name Xvfb
log_end_msg $?
return
}
status() {
daemon -v10 --running --name Xvfb
daemon -v10 --running --name Selenium-RC
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac
exit $?
Starting the whole thing works fine, but when I run a selenium test on that server I get the following message in /var/log/selenium-debug.log:
13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovering Firefox 2... 13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Minefield.app/Contents/MacOS/firefox-bin' is valid... 13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Firefox-2.app/Contents/MacOS/firefox-bin' is valid... 13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Firefox.app/Contents/MacOS/firefox-bin' is valid... 13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/usr/lib/firefox/firefox-bin' is valid... 13:21:29.209 INFO [11] org.openqa.selenium.server.SeleniumDriverResourceHandler - Got result: Failed to start new browser session: java.lang.NullPointerException on session null
When I run selenium and xvfb with the same parameters under the same user, I get
... 13:31:16.413 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/usr/lib/firefox/firefox-bin' is valid... 13:31:16.414 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.CombinedFirefoxLocator - Did not find Firefox 2, now discovering Firefox 3... 13:31:16.415 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovering Firefox 3... 13:31:16.433 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-addons/firefox-bin' is valid... 13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-addons/firefox' is valid... 13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-11.0/firefox-bin' is valid... 13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-11.0/firefox' is valid... 13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovered valid Firefox 3 launcher : '/usr/lib/firefox-11.0/firefox' 13:31:16.435 INFO [11] org.openqa.selenium.server.BrowserSessionFactory - Allocated session cb49662833c84b6cb62fcd5a4a7e9f61 for http://example.com/foo/bar/, launching...
As noted in the comments above, your environment looks different when running as a daemon :)
The problem was the environment inside the daemon executing. The
--env
cleared up the enviroment variables. You can easily see this withThe solution is to start
daemon
with the "-i" switch to copy the path setting. Using this change, Firefox will be found.The working version of the script is available on GitHub.
The init scripts in here might be a solution.
But Selenium is put to the background simply with the "&" operator. So I think this init script would not survive exiting the shell that it was called from.