Sahi headless execution with Google Chrome
Turn On CDP
Section titled “Turn On CDP”Headless Chrome needs the Chrome DevTools Protocol (CDP) to take page screenshots. Turn it on before you set up headless Chrome:
-
Add this line to
<SahiPro>/userdata/config/userdata.propertiesand save the file:sahi.browser.cdp.enable = true -
In
<SahiPro>/userdata/config/browser_types.xml, add--remote-debugging-port=0 --remote-allow-origins=*to the end of the<options>of normal Chrome, then save the file. For example, on Windows:<browserType><name>chrome</name><displayName>Chrome</displayName><icon>chrome.png</icon><path>$ProgramFiles\Google\Chrome\Application\chrome.exe</path><options>--no-default-browser-check --user-data-dir=$userDir\browser\chrome\profiles\sahi$threadNo --proxy-server=127.0.0.1:$port --disable-popup-blocking --always-authorize-plugins --allow-outdated-plugins --enable-npapi --incognito --disable-background-networking --disable-component-update --disable-renderer-backgrounding --disable-backgrounding-occluded-windows --disable-features=OptimizationGuideModelDownloading,OptimizationHintsFetching,OptimizationTargetPrediction,OptimizationHints --remote-debugging-port=0 --remote-allow-origins=*</options><processName>chrome.exe</processName><capacity>5</capacity></browserType>The
chromeHL(headless) entries below already end with these options.
For Windows:
Section titled “For Windows:”-
Download the latest version of Google Chrome from https://www.google.com/chrome/
-
On the Dashboard, click Add Browser. In
browser_types.xml, which opens, add:<browserType><name>chromeHL</name><displayName>ChromeHL</displayName><icon>chrome.png</icon><path>$ProgramFiles\Google\Chrome\Application\chrome.exe</path><options>--no-default-browser-check --user-data-dir=$userDir/browser/chrome/profiles/sahi$threadNo--proxy-server=127.0.0.1:$port --disable-popup-blocking--headless --disable-gpu --no-sandbox --remote-debugging-port=0 --remote-allow-origins=*</options><processName>chrome.exe</processName><capacity>5</capacity></browserType> -
Save and restart Sahi.
-
To run a test in Chrome headless: Click on “Bin” on the dashboard (or navigate to Sahi/userdata/bin on a command prompt) and run the command
Terminal window testrunner.bat sahitests/sahi_demo.sah http://sahi.co.in/demo/ chromeHL
For Linux:
Section titled “For Linux:”-
To install chrome, open terminal and enter the following command
sudo apt-get install libxss1 libappindicator1 libindicator7wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debsudo dpkg -i google-chrome*.deb -
If error messages pop up after running the command “sudo dpkg -i google-chrome*.deb” then run the command
sudo apt-get install -f -
and then re-run the previous command. The error messages mentioned should include something similar to “google-chrome-stable depends on [lib-example]; however; Package [lib-example] is not installed.”
-
To check if completed, just type (without quotes): “google-chrome –version” and it must appear Google Chrome 59 or above.
-
On the Dashboard, click Add Browser. In
browser_types.xml, which opens, add this entry if it isn’t there:<browserType><name>chromeHL</name><displayName>ChromeHL</displayName><icon>chrome.png</icon><path>/usr/bin/google-chrome</path><options>--no-default-browser-check --user-data-dir=$userDir/browser/chrome/profiles/sahi$threadNo--proxy-server=127.0.0.1:$port --disable-popup-blocking--headless --disable-gpu --no-sandbox --remote-debugging-port=0 --remote-allow-origins=*</options><processName>chrome</processName><capacity>5</capacity></browserType> -
Save and restart Sahi.
-
To run a test in Chrome headless: Open terminal, navigate to Sahi/userdata/bin and run the command
Terminal window sh testrunner.sh sahitests/sahi_demo.sah http://sahi.co.in/demo/ chromeHL
For Mac:
Section titled “For Mac:”-
To check Google Chrome version, Open terminal and type (without quotes): “google-chrome –version” and it must appear Google Chrome 59 or above.
-
On the Dashboard, click Add Browser. In
browser_types.xml, which opens, add this entry if it isn’t there:<browserType><name>chromeHL</name><displayName>ChromeHL</displayName><icon>chrome.png</icon><path>open -n -a "Google Chrome.app"</path><options>--args --no-default-browser-check --no-first-run --user-data-dir=$userDir/browser/chrome/profiles/sahi$threadNo--proxy-server=127.0.0.1:$port --disable-popup-blocking--headless --disable-gpu --no-sandbox --remote-debugging-port=0 --remote-allow-origins=*</options><processName>"Google Chrome"</processName><capacity>5</capacity><force>true</force></browserType> -
Save and restart Sahi.
-
To run a test in Chrome headless: Open terminal, navigate to Sahi/userdata/bin and run the command
Terminal window sh testrunner.sh sahitests/sahi_demo.sah http://sahi.co.in/demo/ chromeHL
This will run the Sahi script without opening a browser.
Take Screenshots in Headless Chrome
Section titled “Take Screenshots in Headless Chrome”In headless Chrome, take screenshots on errors and failures with _cdpTakePageScreenShot(). This needs CDP turned on (see Turn On CDP).
Add this code at the start of every script you run in chromeHL:
onScriptError = function($e){ _cdpTakePageScreenShot();}onScriptFailure = onScriptError;Or, to do it for every script, change these two functions in <SahiPro>/userdata/scripts/global_include.sah.
From:
function onScriptError($e) { if(_sahi.isTFCModeEnabled()) _tfcLog($__tfcix+ "__tfc__:onScriptError", "NO_LOG"); return onScriptErrorDefault($e);}function onScriptFailure($e) { if(_sahi.isTFCModeEnabled()) _tfcLog($__tfcix+ "__tfc__:onScriptFailure", "NO_LOG"); onScriptFailureDefault($e);}To:
function onScriptError($e) { _cdpTakePageScreenShot();}function onScriptFailure($e) { _cdpTakePageScreenShot();}Then run the scripts in chromeHL.

