Making casperjs work with slimerjs on Windows
Problem
Trying to use casperjs version 1.1.0-beta3 with slimerjs version 0.9.1 on a Windows 7 machine with the command
casperjs --engine=slimerjs
results in two errors:
- CLI: The command “Files” was not found. (or something like that)
- Popup: Couldn’t read application.ini
Reasons
The command “Files” was not found.
slimerjs uses an environment variable SLIMERJSLAUNCHER to contain the path to the Firefox executable. On my environment that is “C:\Program Files (x86)\Mozilla Firefox\firefox.exe”.
Since it usually contains spaces it needs to be wrapped in quotes. That is, presumably, why the developers used
SET SLIMERJSLAUNCHER="%SLIMERJSLAUNCHER%"
at the top of their slimerjs.bat. Now, if you set the environment variable to include quotes in the first place, or if the batch file is executed twice in the same CLI session, it suddenly looks like that: “”C:\Program Files (x86)\Mozilla Firefox\firefox.exe””.
Couldn’t read application.ini
Firefox needs an application.ini to run. In the slimerjs folder there is one. If using slimerjs standalone this is fine as slimerjs.bat contains
SET SLIMERDIR=%~dp0
which is the path to the executing batch file.
When executing from casperjs, however, this fails, and %~dp0 is set to the working directory (where you execute the script).
Solution
I had to “hack” the slimerjs.bat to make it work standalone and with casperjs.
- Open slimerjs.bat and replace around line 3
SET SLIMERJSLAUNCHER="%SLIMERJSLAUNCHER%"
with
SET SLIMERJSLAUNCHER=%SLIMERJSLAUNCHER%
and make sure your environment variable includes the necessary quotes. - Add a new environment variable called SLIMERDIR and set it to your slimerjs directory. Now in slimerjs.bat replace around line 7
SET SLIMERDIR=%~dp0
with
if not exist %SLIMERDIR% (
SET SLIMERDIR=%~dp0
)
This might not be the cleanest solutions but they work. Hopefully these issues will be resolved in future versions of casperjs/slimerjs.
I would try this out on my Windows PC, thanks for the help.
Hey,
The exec path doesnt seem to be set correct either. I had to specifically add a backslash as below:
for this line : SET SLIMERJSLAUNCHER=%SLIMERJSLAUNCHER%
I keep getting the error ‘SLIMERDIR’ is not recognized as an internal or external command,
operable program or batch file.
Did you create the
SLIMERDIR
environment variable in windows? (if not see e.g. http://www.computerhope.com/issues/ch000549.htm)Also, slimer & casper have been updated so it might be different in the new version.
Or we could copy the application.ini and omni.ja to the script folder.
Thank you for this. !