0

recently I tried to use CoffeeScript for CasperJS tests. So this code below doesn't throw any error, and seems to hang every time I fire it up in 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()

Also, is that possible to use JS2Coffee with casperjs tests

VonAxt
  • 95
  • 9

2 Answers2

0

There are multiple problems with your code.

  1. You define/overwrite casper.test.begin, but you need to call it.
  2. Named functions are not supported in CoffeeScript. I'm talking about suite.
  3. The casper.test.begin execution is asynchronous, so calling casper.exit() at the end will exit the whole script before casper.test.begin execution is finished.
Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Actually I rewritted the script to js, but still no luck to debug it. F.E: "casper.test.begin('Initial script', landing_pages.length * 6, function suite(test) {" - here I assume about 10 tests to be done, but in console it everytime returns me 1 testsuite failed. – VonAxt Jan 05 '15 at 12:19
  • You can ask a new question. – Artjom B. Jan 05 '15 at 12:24
  • Ok, I've managed to fix it. 1-deleted first 3 lines (variables); 2-used casper.each(); 3-deleted casper.exit() – VonAxt Jan 05 '15 at 15:40
  • I don't see why you would use `casper.each`. You can provide your own answer with your fixed code, but keep in mind that it has to make sense with your question. – Artjom B. Jan 05 '15 at 15:49
0

An updated and rewritten version.

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();
    });
});
VonAxt
  • 95
  • 9