Sahi headless execution with PhantomJS

warning NOTE: We do not recommend headless execution. Troubleshooting scripts in a headless environment can be tricky. In many cases failures happen in PhantomJS which do not happen in real browsers. Troubleshooting and fixing them may not be worthwhile, since your customers are using real browsers. Google Chrome is as fast or faster than PhantomJS in most cases.
warning If you are looking at headless execution for integration with your CI server, read our recommended Jenkins/CI setup without headless playback.
PhantomJS is a headless browser based on WebKit. (A headless browser runs without a user interface)

Documentation since Sahi Pro V4.3.2

For Windows:

  1. Download phantomjs-1.9.x-windows.zip from http://code.google.com/p/phantomjs/downloads/list (Or choose the correct one from http://phantomjs.org/download.html) and make sure the version is 1.9.0 or above.
  2. Unzip it and copy phantomjs.exe to sahi/ext/phantomjs folder such that you have sahi/ext/phantomjs/phantomjs.exe
  3. Click "Configure" link on Dashboard, and, if not present, add
  4. <browserType>
    	<name>phantomjs</name>
    	<displayName>PhantomJS</displayName>
    	<icon>phantomjs.png</icon>
    	<path>$sahiDir\ext\phantomjs\sahi_phantomjs.bat</path>
    	<options>--proxy=127.0.0.1:$port $sahiDir\ext\phantomjs\phantomsahi.js</options>
    	<processName>phantomjs.exe</processName>
    	<capacity>100</capacity>
    	<force>false</force>
    </browserType>
  5. Save and restart Sahi.
  6. To run a test in PhantomJS: Click on "Bin" on the dashboard (or navigate to sahi/userdata/bin on a command prompt) and run the command
  7. testrunner.bat sahitests/sahi_demo.sah http://sahi.co.in/demo/ phantomjs

For Linux:

  1. To install phantomjs, open terminal and enter the following command
  2. sudo apt-get install phantomjs
    To check if completed, just type (without quotes): "phantomjs --version" and it must appear 1.9 or above.

    If the version is below 1.9 then follow the below steps or directly perform the below steps to get the version 1.9 or above.
    cd /usr/local/share
    sudo wget https://phantomjs.googlecode.com/files/phantomjs-1.9.0-linux-x86_64.tar.bz2
    tar xjf phantomjs-1.9.0-linux-x86_64.tar.bz2
    sudo ln -s /usr/local/share/phantomjs-1.9.0-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
    sudo ln -s /usr/local/share/phantomjs-1.9.0-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
    sudo ln -s /usr/local/share/phantomjs-1.9.0-linux-x86_64/bin/phantomjs /usr/bin/phantomjs


  3. Click "Configure" link on Dashboard, and, if not present, add
  4. <browserType>
    	<name>phantomjs</name>
    	<displayName>PhantomJS</displayName>
    	<icon>phantomjs.png</icon>
    	<path>$sahiDir/ext/phantomjs/sahi_phantomjs.sh</path>
    	<options>--proxy=127.0.0.1:$port $sahiDir/ext/phantomjs/phantomsahi.js</options>
    	<processName>phantomjs</processName>
    	<capacity>100</capacity>
    	<force>false</force>
    </browserType>
  5. Save and restart Sahi.
  6. To run a test in PhantomJS: Open terminal, navigate to sahi/userdata/bin and run the command
  7. sh testrunner.sh sahitests/sahi_demo.sah http://sahi.co.in/demo/ phantomjs
This will run the Sahi script without opening a browser.

For Mac:

  1. To install phantomjs, open terminal and enter the following command
  2. brew install phantomjs
    To check if completed, just type (without quotes): "phantomjs -v" and it must appear 1.9 or above.

    To install Homebrew on Mac OS.
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  3. Click "Configure" link on Dashboard, and, if not present, add
  4. <browserType>
    	<name>phantomjs</name>
    	<displayName>PhantomJS</displayName>
    	<icon>phantomjs.png</icon>
    	<path>$sahiDir/ext/phantomjs/sahi_phantomjs.sh</path>
    	<options>--proxy=127.0.0.1:$port $sahiDir/ext/phantomjs/phantomsahi.js</options>
    	<processName>phantomjs</processName>
    	<capacity>100</capacity>
    	<useSystemProxy>false</useSystemProxy>
    </browserType>
  5. Save and restart Sahi.
  6. To run a test in PhantomJS: Open terminal, navigate to sahi/userdata/bin and run the command
  7. sh testrunner.sh sahitests/sahi_demo.sah http://sahi.co.in/demo/ phantomjs
This will run the Sahi script without opening a browser.

Older Documentation (before Sahi Pro V4.3.2)

  1. Download phantomjs-1.x.x-win32-dynamic.zip from this link.
  2. Unzip it to, say C:\phantomjs-1.x.x-win32 such that phantomjs.exe is available at C:\phantomjs-1.x.x-win32\phantomjs.exe
  3. Create a file sahi.js in C:\phantomjs-1.x.x-win32 with the content below:
  4. For version 1.2.0 onwards, use:
    if (phantom.args.length === 0) {
      console.log('Usage: sahi.js <Sahi Playback Start URL>');
      phantom.exit();
    } else {
      // var address = unescape(phantom.args[0]); // use if < v1.7
      var address = phantom.args[0];
      console.log('Loading ' + address);
      var page = new WebPage();
      page.open(address, function(status) {
        if (status === 'success') {
          var title = page.evaluate(function() {
            return document.title;
          });
          console.log('Page title is ' + title);
        } else {
          console.log('FAIL to load the address');
        }
      });
    }
    For version 1.1.0, use:
    if (phantom.state.length === 0) {
      if (phantom.args.length === 0) {
        console.log('Usage: sahi.js <Sahi Playback Start URL>');
        phantom.exit();
      } else {
        var address = unescape(phantom.args[0]);
        phantom.state = "sahi script running";
        console.log('Loading ' + address);
        phantom.open(address);
      }
    } else {
      if (phantom.loadStatus == 'success') {
        console.log('Page title is ' + document.title);
      } else {
        console.log('FAIL to load the address');
      }
    }
    The above code is based on the example provided by PhantomJS

  5. Click "Configure" link on Dashboard, and add
  6. <browserType>
    	<name>phantomjs</name>
    	<displayName>PhantomJS</displayName>
    	<icon>phantomjs.png</icon>
    	<path>C:\phantomjs-1.1.0-win32\phantomjs.exe</path>
    	<options>--proxy=localhost:9999 C:\phantomjs-1.1.0-win32\sahi.js</options>
    	<processName>phantomjs.exe</processName>
    	<capacity>100</capacity>
    	<force>true</force>
    </browserType>
    before the ending tag in browser_types.xml
  7. Save and restart Sahi.
  8. To run a test in PhantomJS: Click on "Bin" on the dashboard (or navigate to sahi/userdata/bin on a command prompt) and run the command
testrunner.bat sahitests/sahi_demo.sah http://sahi.co.in/demo/ phantomjs
This will run the Sahi script without opening a browser.