【问题标题】:call phantomjs from jasmine从 jasmine 调用 phantomjs
【发布时间】:2015-06-29 00:08:27
【问题描述】:

我正在尝试运行此测试:

 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)
    });

评论phantomjs.open("google.com") 测试通过。

我要测试的代码是:

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

似乎我已经设置了我的 npm 依赖项。至少我的测试是从“grunt”命令开始的。

我的 package.json(例如)如下所示:

{
  "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"
  }
}

(我也有正确的Gruntfile.jskarma.conf.js

更新:我最终得到一个错误(如果取消注释 phantomjs 行):

ReferenceError:找不到变量:phantomjs

(如果 phantomjs 来自我所依赖的 npm-packages,我不确定如何访问它)

如果我这样做:

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

然后:

ReferenceError:找不到变量:需要

我发现this 是可能的解决方案:但我确实使用jasmine 2.x that 建议我拆分我的规格.. 仍然尝试弄清楚。

问:如何打开窗口(利用phantomjs)使测试与window'open/not undefined 一起工作?

【问题讨论】:

标签: node.js gruntjs jasmine phantomjs karma-jasmine


【解决方案1】:

好的。我得到了它。 似乎没有办法像我想要的那样打开浏览器窗口。我一直在混合概念(phantom-js 特定脚本和 jasmine)。

因此,我们可以使用我们在karma.confGruntFile.js 中指定的所有注入脚本,将任何DOM/页面片段注入我们的window(由PhantomeJS conf 为我们打开),而不是打开新页面/p>

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!");  

}

(我从this post得到selectedElementText()(不错))

【讨论】:

    【解决方案2】:

    phantomjs.open 不是同步函数。

    完成后,它会调用您作为第二个或第三个参数传递的回调。

    你可能想做这样的事情:

    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)
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-28
      • 2014-09-08
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多