当前位置: 首页 > 知识库问答 >
问题:

Jasmine在幻想曲中运行时从不执行它

常睿范
2023-03-14

我正在尝试在PhantomJS中运行Jasmine。经过很多努力,我可以归结为:

    page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/jasmine.js");
    page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/jasmine-html.js");
    page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/boot.js");
    page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/console.js");
    var result = page.evaluate(function (done) {
        var ConsoleReporter = jasmineRequire.ConsoleReporter();
        var options = {
            timer: new jasmine.Timer,
            print: function () {
                console.log.apply(console, arguments)
            }
        };
        consoleReporter = new ConsoleReporter(options);
        jasmine.getEnv().addReporter(consoleReporter);

        describe("test1", function () {
            console.log("in test 1");
            it("should do something", function () {
                console.log("NEVER GETS HERE"); // <-- never gets there
            });
        });
    });

代码一直执行到it(),但回调永远不会执行:|

[编辑]我正在尝试将phantom与jasmine一起用于端到端测试。我已经有了一个应用程序服务器,并且我正在使用Karma进行单元测试。所以我认为testRunner.html不会有帮助。PhantomJS应该登录我的应用程序并做一些事情,我会用Jasmine进行测试。

共有1个答案

方和宜
2023-03-14

用愚蠢的运气蛮力方法得到了它。我从来没有找到有用的文档,但这有点帮助

我的文件是这样的:

testrunner.js

var system = require("system");
var page = require("webpage").create();

var loginUrl = system.args[1];

page.onConsoleMessage = function (msg) {
    console.log("FROM PAGE: " + msg);
};

page.onError = function (err) {
    console.log("ERR FROM PAGE: " + JSON.stringify(err));
}

var loggedIn = false;
function onLogin_atHomePage() {
    if (loggedIn)
        return; // dont run twice if a developer is programatically logging in 
        page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/jasmine.js");
        page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/boot.js");
        page.injectJs("/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/console.js");
        var result = page.evaluate(function (done) {
            var ConsoleReporter = jasmineRequire.ConsoleReporter();
            var options = {
                timer: new jasmine.Timer,
                print: function () {
                    console.log.apply(console, arguments)
                }
            };
            consoleReporter = new ConsoleReporter(options); // initialize ConsoleReporter
            jasmine.getEnv().addReporter(consoleReporter); //add reporter to execution environment

            describe("test1", function () {
                console.log("in test 1");
                it("should do something", function () {
                    console.log("NEV");
                });
            });

            jasmine.getEnv().execute();
        });
}

page.open(loginUrl, function () {
    console.log("LOGGING IN...");
    page.onLoadFinished = onLogin_atHomePage;
    page.evaluate(function () {
        $(document).ready(function () {
            var $u = $("#login_id");
            var $p = $("[name='password']");
            $u.val("me");
            $p.val("666");
            submitForm();
        });
    });
});

启动.js

/**
 Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.

 If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.

 The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.

 [jasmine-gem]: http://github.com/pivotal/jasmine-gem
 */

(function() {

  /**
   * ## Require &amp; Instantiate
   *
   * Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
   */
  window.jasmine = jasmineRequire.core(jasmineRequire);

  /**
   * Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
   */
  //jasmineRequire.html(jasmine);

  /**
   * Create the Jasmine environment. This is used to run all specs in a project.
   */
  var env = jasmine.getEnv();

  /**
   * ## The Global Interface
   *
   * Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged.
   */
  var jasmineInterface = {
    describe: function(description, specDefinitions) {
      return env.describe(description, specDefinitions);
    },

    xdescribe: function(description, specDefinitions) {
      return env.xdescribe(description, specDefinitions);
    },

    it: function(desc, func) {
      return env.it(desc, func);
    },

    xit: function(desc, func) {
      return env.xit(desc, func);
    },

    beforeEach: function(beforeEachFunction) {
      return env.beforeEach(beforeEachFunction);
    },

    afterEach: function(afterEachFunction) {
      return env.afterEach(afterEachFunction);
    },

    expect: function(actual) {
      return env.expect(actual);
    },

    pending: function() {
      return env.pending();
    },

    spyOn: function(obj, methodName) {
      return env.spyOn(obj, methodName);
    },

    jsApiReporter: new jasmine.JsApiReporter({
      timer: new jasmine.Timer()
    })
  };

  /**
   * Add all of the Jasmine global/public interface to the proper global, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`.
   */
  if (typeof window == "undefined" && typeof exports == "object") {
    extend(exports, jasmineInterface);
  } else {
    extend(window, jasmineInterface);
  }

  /**
   * Expose the interface for adding custom equality testers.
   */
  jasmine.addCustomEqualityTester = function(tester) {
    env.addCustomEqualityTester(tester);
  };

  /**
   * Expose the interface for adding custom expectation matchers
   */
  jasmine.addMatchers = function(matchers) {
    return env.addMatchers(matchers);
  };

  /**
   * Expose the mock interface for the JavaScript timeout functions
   */
  jasmine.clock = function() {
    return env.clock;
  };

  /**
   * ## Runner Parameters
   *
   * More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface.
   */

  /*var queryString = new jasmine.QueryString({
    getWindowLocation: function() { return window.location; }
  });

  var catchingExceptions = queryString.getParam("catch");
  env.catchExceptions(typeof catchingExceptions === "undefined" ? true : catchingExceptions);
  */

  /**
   * ## Reporters
   * The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any).
   */
    /*
  var htmlReporter = new jasmine.HtmlReporter({
    env: env,
    onRaiseExceptionsClick: function() { queryString.setParam("catch", !env.catchingExceptions()); },
    getContainer: function() { return document.body; },
    createElement: function() { return document.createElement.apply(document, arguments); },
    createTextNode: function() { return document.createTextNode.apply(document, arguments); },
    timer: new jasmine.Timer()
  });
  */

  /**
   * The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results  from JavaScript.
   */
  env.addReporter(jasmineInterface.jsApiReporter);
  //env.addReporter(htmlReporter);

  /**
   * Filter which specs will be run by matching the start of the full name against the `spec` query param.
   */
  /*var specFilter = new jasmine.HtmlSpecFilter({
    filterString: function() { return queryString.getParam("spec"); }
  });

  env.specFilter = function(spec) {
    return specFilter.matches(spec.getFullName());
  };*/

  /**
   * Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack.
   */
  window.setTimeout = window.setTimeout;
  window.setInterval = window.setInterval;
  window.clearTimeout = window.clearTimeout;
  window.clearInterval = window.clearInterval;

  /**
   * ## Execution
   *
   * Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
   */
  var currentWindowOnload = window.onload;

  window.onload = function() {
    if (currentWindowOnload) {
      currentWindowOnload();
    }
    htmlReporter.initialize();
    env.execute();
  };

  /**
   * Helper function for readability above.
   */
  function extend(destination, source) {
    for (var property in source) destination[property] = source[property];
    return destination;
  }

}());
 类似资料:
  • 问题内容: 如何从命令行在Node.js上运行Jasmine测试?我已经通过npm安装了jasmine- node并编写了一些测试。我想在目录中运行测试并在终端中获取结果,这可能吗? 问题答案: 编辑 由于不再维护该软件包,因此这似乎不再是当前的最佳答案。请参阅下面的答案 你可以这样做 从您的测试目录 这会将茉莉花安装到../node_modules/jasmine-node 然后 从我的演示中做

  • 我想在ubuntu 14.04LTS的引导上运行一个python脚本。 我的rc.local文件如下: sudo /首页/hduser/morey/动物园管理员-3.3.6/bin/zkServer.sh启动 回声“测试” sudo/home/HD user/Morey/Kafka/bin/Kafka-server-start . sh/home/HD user/Morey/Kafka/confi

  • 我已经通过libav-tools对安装了ffmpeg的应用程序进行了dockerize。该应用程序启动时没有问题,但是当Fluent-ffmpeg npm模块试图执行ffmpeg命令时出现了问题,但没有找到。当我想检查ffmpeg的版本和图像中设置的linux发行版时,我使用了命令,但它给出了以下错误: 然后我意识到,我尝试在图像或容器中运行的所有命令都会出现同样的错误。 这是我的Dockerfi

  • 所以一切都很好,就像2到3周前一样,突然我所有的jar文件都无法通过双击打开 我检查了我的注册表,但一切正常,关联的. jar应用程序可以使用javaw运行,但我仍然无法双击运行可执行的. jar文件 我可以用javaw -jar文件名在cmd中运行它们.jar但它不适用于我的世界,我不能用那个cmd运行我的世界,我想让它,所以每个可执行文件.jar文件都可以打开双击 我的 java 命令在 cm

  • 我正在Jenkins CI建立一个带自动测试的Angular 4 SPA。SPA是一个更大的、由Maven管理的项目的一部分,因此构建也是由Maven进行管理的。到目前为止,我已经: 在Jenkins上安装NodeJS插件,使用安装来自版本为8.6.0的nodejs.org 配置“要安装的全局npm软件包”=“karma-cli phantomjs-预构建的jasmine-core karma-j

  • 问题内容: 我有一个“长期运行的”清理行动,我需要执行我的。做这个的最好方式是什么? 如果我使用a 这样做,我将立即返回;但是线程引用发生了什么?我正在寻找有关此处需要了解的任何影响/陷阱/绊网的建议,因为我认为即使活动被销毁,该流程仍将继续存在。 背景: 我在我的应用程序中使用JmDNS。当用户使用完我的应用程序后,我想清理JmDNS实例。我使用类方法进行此操作。但是,此方法需要 5秒钟以上 才