【问题标题】:How can I access the phantom object from CasperJS?如何从 CasperJS 访问幻像对象?
【发布时间】:2017-06-22 02:35:03
【问题描述】:

我是第一次使用 CasperJS,我想在加载页面之前添加一个 cookie。我正在使用此代码:

casper.test.setUp(function () {
  casper.echo('Cookies enabled?: ' + phantom.cookiesEnabled);
  phantom.addCookie({
    domain: '.localhost',
    name: 'sessionId',
    value: '12345abcd6789efg'
});

问题似乎出在我的代码中的phantom。我收到以下错误:

TypeError: undefined is not a constructor

如何定义phantom

【问题讨论】:

    标签: javascript testing cookies phantomjs casperjs


    【解决方案1】:

    CasperJS 建立在 PhantomJS 之上,这意味着您的 Casper 环境实际上是一个带有一些附加功能的 PhantomJS 环境...

    您可以编写来检查您的环境的最基本脚本如下:

    console.log('PhantomJS version: ' + phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch);
    phantom.exit();
    

    通常,您应该能够使用phantomjscasperjs 命令执行此脚本。这将为您提供与phantomjs --version 相同的输出。

    在您的脚本中,您正在使用 CasperJS 的 测试器模块。但是我们可以从那里访问phantom 对象吗?是的,当然!

    casper.test.setUp(function () {
      console.log('PhantomJS version: ' + phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch);
    });
    
    casper.test.begin('Example', function (test) {
      casper.start('http://example.com');
    
      casper.then(function () {
        test.assertEquals(this.getTitle(), 'Example Domain');
      });
    
      casper.run(function () {
        test.done();
      });
    });
    

    这次您不能使用phantomjs 命令,而必须使用casperjs test。如果您尝试运行此脚本,您应该会在测试开始前看到 PhantomJS 版本...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2011-01-15
      • 2018-09-20
      • 1970-01-01
      相关资源
      最近更新 更多