5

I couldn't get my casperjs asserts colored on my cmd.exe (Windows 7, x64). I followed ansicon.

If I understood right, asserts are colored automatically if ansicon is installed casperjs

Windows users will get colorized output if ansicon is installed.

Any ideas?

UPDATE

var casper = require('casper').create(),
    utils = require('utils'),
    http = require('http'),
    fs = require('fs'),
    colorizer = require('colorizer').create('Colorizer');

var xpath = require('casper').selectXPath;

casper.start('http://google.com/').then(function(response) {
    casper.echo('This is supposed to be green', 'INFO');
});

casper.run();

CMD:

C:\Users\itsme\Desktop>casperjs test test.js
Test file: test.js
This is supposed to be green

C:\Users\itsme\Desktop>casperjs test test.js
smarber
  • 4,829
  • 7
  • 37
  • 78
  • 1
    Have you download and installed ansicon? What steps did you take to use it and what happens when you do? – Ross Ridge Aug 27 '14 at 16:59
  • I've downloaded [ansicon](https://github.com/downloads/adoxa/ansicon/ansi160.zip), put it under `C:`, added x64 to my environment variable (path), run `ansicon.exe -i` (no output), closed cmd and opened it again and finally I've run my casper test where I've put `casper.echo('green message', 'INFO');`. I've got no colors – smarber Aug 27 '14 at 17:22
  • Did you see something like `←[36m[info]←[39m [phantom] green message` or just `[info] [phantom] green message`? Do you set Casper's `verbose` option to true? – Ross Ridge Aug 27 '14 at 17:42
  • Actually I only got the message `green message`, and that's it (no [info], no [phantom]) – smarber Aug 28 '14 at 07:54
  • Ah, sorry, I was looking at examples that use `casper.log`. Try making small but complete example that demonstrates the problem and include it in your question. Show the command you use to run it and the output you get in the console. You can just cut & paste that from the Windows command window. – Ross Ridge Aug 28 '14 at 12:55
  • I've inserted the script I've been trying into my question :) – smarber Aug 28 '14 at 13:22
  • @RossRidge, any suggestions if output still looks like "←[36m[info]..."? – Bill Aug 05 '15 at 20:33
  • @Bill That would actually be a different problem than original poster's. He apparently never saw output like that. All I can suggest is make sure you have ansicons installed, assuming you're using Windows. You might want to post your own question. – Ross Ridge Aug 05 '15 at 20:39
  • @RossRidge, thank you. Will do. – Bill Aug 06 '15 at 14:22
  • This is related: http://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences - It shows the same behavior. It also proves that neither CasperJs nor PhantomJs are the problem really, the .NET wrapper (casperjs.exe) seems to be the culprit. When I run PhantomJS manually, in the same console window, with exactly the same command line that `casperjs.exe` generates, the coloring works without problems. – Tomalak Dec 03 '15 at 17:53

2 Answers2

2

I think your problem is that casperjs tests to see if the environment variable ANSICON is set on Windows, and if it's not set it doesn't try to colourize the output. Unfortunately when you install ansicon the way you did it doesn't actually set this variable in the environment in any normal way. Instead it uses a crude hack to set it in the environment of the initial cmd.exe process of a window, but only if the that cmd process tries to retrieve its value. Since nothing normally uses the ANSICON environment variable this means it won't normally be visible to other process (eg. casperjs) run in that window.

If this is the problem then all you need to do is set the ANSICON variable before running casperjs:

set ANSICON=%ANSICON%

You can also make this a permanent part of the environment by using setx ANSICON=foo.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • If you used `setx` you'll need to close the command window and open a new one for it to take effect. – Ross Ridge Aug 28 '14 at 15:30
  • Try adding second `echo` call to your example where the string is `"\033[32mThis is supposed to be green\033[m"`. If that works then ansicon is working correctly and the problem is with casper.js. If it doesn't work then it's probably because ansicon isn't working for some reason. – Ross Ridge Aug 29 '14 at 01:28
2

The answer is over here: ANSI-Coloring Console Output with .NET

The CasperJS binary package comes with a pre-compiled casperjs.exe that targets the x86 platform. Unfortunately, that has the effect you mention on x64 versions of ansicon.

Solutions:

  1. Use the x86 version of ansicon.exe
  2. Recompile casperjs.exe for the x64 platform, which is quick and painless:

    C:\>cd casperjs\src
    C:\casperjs\src>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /platform:x64 casperjs.cs
    

Copy the resulting .exe to the bin directory and you're done.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628