0

I'm trying to run this test:

 it("should not select anything (emptiness) by default", function() {
        // given
        //phantomjs.open("google.com");  // just guessing

        // when
        var text = wordHighlighter.getSelectedText();

        // then
        expect(text).toEqual("");  // nothing is selected (yet)
    });

With commented phantomjs.open("google.com") the test passes.

The code I'm trying to test is:

var getSelectedText = function () {
        var text = "";
        if (window.getSelection) {   // windows suppose to be open
            text = window.getSelection().toString();
        } 
        return text;
    };

Seems I have my npm dependecy set up. At least my test is starting by "grunt" command.

My package.json (for example) looks like this:

{
  "name": "project-name",
  "version": "0.0.1",
  "devDependencies": {
    "express": "~4.4.3",
    "grunt": "~0.4.5",
    "grunt-cli": "~0.1.13",
    "grunt-karma": "~0.9.0",
    "karma": "~0.12.28",
    "karma-jasmine": "~0.3.2",
    "karma-junit-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "~0.1.4",
    "phantomjs": "~1.9.7-10",
    "karma-ng-html2js-preprocessor" : "~0.1"
  }
}

(also I have proper Gruntfile.js and karma.conf.js)

UPDATE: I end up with an Error (if uncomment phantomjs line):

ReferenceError: Can't find variable: phantomjs

(I'm not sure how I can access phantomjs if it comes from npm-packages I depend on)

If I do with:

  var webPage = require('webpage');
  var page = webPage.create();
  page.open(...

Then:

ReferenceError: Can't find variable: require

I found this as possible solution: but I do use jasmine 2.x And that suggests me to split my specs.. Still try to figure it out.

Q: How to open the window (make use of phantomjs) to make the test working with the window'open/not undefined ?

Community
  • 1
  • 1
ses
  • 13,174
  • 31
  • 123
  • 226
  • similar (not really answered): http://stackoverflow.com/questions/11891998/phantomjs-cant-find-variable-require-when-using-phantomjasmine – ses Apr 21 '15 at 20:12

2 Answers2

0

phantomjs.open is not a synchronous function.

Once done, it calls the callback you pass as second — or third — argument.

You probably want to do something like this:

it("should not select anything (emptiness) by default", function (/* status */) {
    phantomjs.open("google.com", function () {
        var text = wordHighlighter.getSelectedText();

        // then
        expect(text).toEqual("");  // nothing is selected (yet)
    });
});
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
0

Ok. I got it. Seems there is no way to open browser window like I wanted. I've been mixing concepts (phantom-js specific script and jasmine).

So, instead of opening new page, we may inject whatever DOM/piece of page to our window (opened for us by PhantomeJS conf) with all injected scripts we specified in karma.conf and GruntFile.js

it("should get selection if text in the DOM is selected", function() {
        // given
        var  body = document.getElementsByTagName('body')[0];

        // inject text element to the page body and select that text
        var textElement = document.createTextNode("Hello Hello World!");
        textElement.id = "textElementId";
        body.appendChild(textElement);

        selectElementText(textElement); // select element

        // when
        var text = wordHighlighter.getSelectedText();

        // then
        expect(text).toEqual("Hello Hello World!");  

}

( I got selectedElementText() from this post (nice one) )

Community
  • 1
  • 1
ses
  • 13,174
  • 31
  • 123
  • 226