2

I am working on a framework and I'd like to support phantomjs. Is there a way to check whether the page JavaScript is running under phantomjs or karma+phantomjs? I tested only with karma+phantomjs, but I did not find any global variable I could use to check the environment. I thought there will be a global.phantom variable or something like that. Is there a difference between these environments?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • [this question](http://stackoverflow.com/a/24471222/13226) and [this question](http://stackoverflow.com/questions/26186101/how-to-skip-the-code-execution-from-phantomjs-or-grover) might help – ebo Aug 01 '16 at 17:57
  • @ebo Nope, but I found a solution. :-) http://laputa.io/blog/2014/10/24/using-karma/ – inf3rno Aug 01 '16 at 18:00
  • @ArtjomB. Yes. ------------------------ – inf3rno Aug 01 '16 at 18:04

1 Answers1

3

According to this article: http://laputa.io/blog/2014/10/24/using-karma/ Karma runs in iframe and so phantomjs variables are not present in this iframe. So all we have to do is turning off iframe by karma. To do so we have to use the following settings:

config.set({
    client: {
        useIframe: false
    }
});

Now we can simply use !!window._phantom or !!window.callPhantom to check whether phantom is present. This is a partial solution however. I am still looking for a better one, which can see phantom even in an iframe.

I ended up with a code like this:

var global = (function () {
    return this;
})();
var phantom = global._phantom;
if (!phantom && global.__karma__ && global.frameElement)
    phantom = global.parent._phantom;
var isPhantom = !!phantom;

This works by both iframed and non-iframed Karma and I guess it works by vanilla phantomjs as well.

inf3rno
  • 24,976
  • 11
  • 115
  • 197