用coffeescript写的casperjs测试挂起

时间:2014-12-30 17:33:44

标签: javascript testing coffeescript casperjs

最近我尝试使用CoffeeScript进行CasperJS测试。 因此,下面的代码不会抛出任何错误,并且每次在CLI中启动它时似乎都会挂起。

casper = require("casper")
  .create
      verbose: true,
      logLevel: "debug"

casper_utils = require("utils")
colorizer = require("colorizer").create 'Colorizer'

common_link = "http://0.0.0.0:6543/"

landing_pages = ['g',
            'em',
            'm1',
            'm4',
            'mv4',
            'mv5',
            'mp',
            'm2',
            'm3',
            'rp',
            'rc']

reg_hash = '#reg'
reg_modal = '.registration'

pay_hash = '#pay'
pay_modal = '.payment'

checkRegVisibility = ->
  @test.assertExists reg_modal
  @test.assertVisible reg_modal
  @test.assertNotVisible pay_modal

checkPayVisibility = ->
  @test.assertExists pay_modal
  @test.assertVisible pay_modal
  @test.assertNotVisible reg_modal

casper.on 'run.start', ->
  @.log 'Casper started'

casper.on 'error', ->
  @.log 'error'

casper.on 'http.status.404', ->
  @.log '404'

casper.test.begin = ('Initial test', landing_pages.length * 3, suite(test)) ->
  landing_pages.forEach (lp, index) ->
    casper.start common_link+"?lp="+lp, ->
        casper.echo lp
        checkRegVisibility()
    casper.then common_link+"?lp="+lp+reg_hash, ->
        casper.echo lp
        checkRegVisibility()
    casper.run, ->
        test.done()

casper.exit()

此外,是否可以将JS2Coffee与casperjs测试一起使用

2 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。

  1. 您定义/覆盖casper.test.begin,但您需要调用它。
  2. Named functions are not supported in CoffeeScript。我正在谈论suite
  3. casper.test.begin执行是异步的,因此在casper.exit()执行完成之前,最后调用casper.test.begin将退出整个脚本。

答案 1 :(得分:0)

更新和重写版本。

var common_link = "http://0.0.0.0:6543/";

var landing_pages = ['gen',
                'em2',
                'm01',
                'm0v4',
                'm1v4',
                'm_v5',
                'mo4p',
                'm2',
                'm03',
                're',
                'rec'];

var reg_hash = '#register';
var reg_modal = '.modal.registration';

var pay_hash = '#payment';
var pay_modal = '.modal.payment';


function checkRegVisibility() {
    casper.test.assertExists(reg_modal, 'reg form exists');
    casper.test.assertVisible(reg_modal, 'reg form is visible');
    casper.test.assertExists('#registration-form input[name="email"]', 'email input exists');
    casper.test.assertExists('#registration-form input[name="password"]', 'password input exists');
    casper.test.assertExists('#registration-form input[name="password_confirm"]', 'password confirmation input exists');
    casper.test.assertExists('#registration-form input[type="checkbox"]', 'checkbox is present');
    if (casper.evaluate(function() {return document.querySelector('#registration-form input[type="checkbox"]').checked;}))
    {
        casper.echo('checkbox is checked');
    } else {
        casper.echo('checkbox is unchecked');
    }
    casper.test.assertExists('#registration-form input[type="checkbox"]', 'submit is present');
    casper.test.assertNotVisible(pay_modal, 'payment form isn\'t visible');
};

function checkPayVisibility() {
    casper.test.assertExists(pay_modal);
    casper.test.assertVisible(pay_modal);
    casper.test.assertNotVisible(reg_modal);
};

casper.test.begin('Initial script', landing_pages * 6, function suite(test) {

    casper.start();
    casper.each(Object.keys(landing_pages), function(casper, land_page) {
        this.thenOpen(common_link+"?lp="+land_page+reg_hash, function() {
            casper.echo(landing_pages[land_page]+reg_hash);
            checkRegVisibility();
        });
        this.thenOpen(common_link+"?lp="+land_page+pay_hash, function() {
            casper.echo(landing_pages[land_page]+pay_hash);
            checkPayVisibility();
        });
    });

    casper.run(function() {
        test.done();
    });
});