Skip to content

Instantly share code, notes, and snippets.

@kpavlov
Last active July 5, 2018 09:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpavlov/9774493fe8f905d2c9ea to your computer and use it in GitHub Desktop.
Save kpavlov/9774493fe8f905d2c9ea to your computer and use it in GitHub Desktop.
Running headless Selenium server via Xvfb

Running headless Selenium server via Xvfb

Original idea from here

Selenium command-line reference:

  Usage: java -jar selenium-server.jar [-interactive] [options]
  
    -port <nnnn>: the port number the selenium server should use
      (default 4444)
    -timeout <nnnn>: an integer number of seconds we should allow a
      client to be idle
    -browserTimeout <nnnn>: an integer number of seconds a browser is
      allowed to hang
    -interactive: puts you into interactive mode.  See the tutorial for
      more details
    -singleWindow: puts you into a mode where the test web site
      executes in a frame. This mode should only be selected if the
      application under test does not use frames.
    -profilesLocation: Specifies the directory that holds the profiles
      that java clients can use to start up selenium.  Currently
      supported for Firefox only.
    -forcedBrowserMode <browser>: sets the browser mode to a single
      argument (e.g. "*iexplore") for all sessions, no matter what is
      passed to getNewBrowserSession
    -forcedBrowserModeRestOfLine <browser>: sets the browser mode to
      all the remaining tokens on the line (e.g. "*custom
      /some/random/place/iexplore.exe") for all sessions, no matter what
      is passed to getNewBrowserSession
    -userExtensions <file>: indicates a JavaScript file that will be
      loaded into selenium
    -browserSessionReuse: stops re-initialization and spawning of the
      browser between tests
    -avoidProxy: By default, we proxy every browser request; set this
      flag to make the browser use our proxy only for URLs containing
      '/selenium-server'
    -firefoxProfileTemplate <dir>: normally, we generate a fresh empty
      Firefox profile every time we launch.  You can specify a directory
      to make us copy your profile directory instead.
    -debug: puts you into debug mode, with more trace information and
      diagnostics on the console
    -browserSideLog: enables logging on the browser side; logging
      messages will be transmitted to the server.  This can affect
      performance.
    -ensureCleanSession: If the browser does not have user profiles,
      make sure every new session has no artifacts from previous
      sessions.  For example, enabling this option will cause all user
      cookies to be archived before launching IE, and restored after IE
      is closed.
    -trustAllSSLCertificates: Forces the Selenium proxy to trust all
      SSL certificates.  This doesn't work in browsers that don't use the
      Selenium proxy.
    -log <logFileName>: writes lots of debug information out to a log
      file and disables logging to console
    -logLongForm: writes information out to console in long format (for
      debugging purpose)
    -htmlSuite <browser> <startURL> <suiteFile> <resultFile>: Run a
      single HTML Selenese (Selenium Core) suite and then exit
      immediately, using the specified browser (e.g. "*firefox") on the
      specified URL (e.g. "http://www.google.com").  You need to specify
      the absolute path to the HTML test suite as well as the path to the
      HTML results file we'll generate.
    -proxyInjectionMode: puts you into proxy injection mode, a mode
      where the selenium server acts as a proxy server for all content
      going to the test application.  Under this mode, multiple domains
      can be visited, and the following additional flags are supported:
  
      -dontInjectRegex <regex>: an optional regular expression that
        proxy injection mode can use to know when to bypss injection
      -userJsInjection <file>: specifies a JavaScript file which will
        then be injected into all pages
      -userContentTransformation <regex> <replacement>: a regular
        expression which is matched against all test HTML content; the
        second is a string which will replace matches.  These flags can
        be used any number of times.  A simple example of how this could
        be useful: if you add "-userContentTransformation https http"
        then all "https" strings in the HTML of the test application will
        be changed to be "http".
  
  This synopsis lists options available in standalone role only. To
    get help on the options available for other roles run the server with
    -help option and the corresponding -role option value.
#!/usr/bin/env bash
SELENIUM_JAR=selenium-server-standalone-2.50.1.jar
wget -N http://selenium-release.storage.googleapis.com/2.50/${SELENIUM_JAR}
export DISPLAY=:99
rm -f selenium.log
touch selenium.log
xvfb-run -s "-screen 0 1600x1200x24" java -jar ${SELENIUM_JAR} -trustAllSSLCertificates -ensureCleanSession -debug -log selenium.log & export SELENIUM_PID=$!
echo "Started selenium process: PID=$SELENIUM_PID"
echo "Now tracing Selenium log"
echo "==========================================="
tail -f selenium.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment