Problem using PhantomJS to test Meteor app


#1

I’ve got a fairly simple Meteor app:

<template name="homePage">
  <form>
    <input id="dummy" value="Wow">
    <input id="data">
    <button type="submit">Do It</button>
  </form>
</template>
Template.homePage.events({
  'submit form': function (event, template) {
    var v1 = template.$('#data').val();
    event.preventDefault();

    console.log('Click event triggered ', v1);
    Things.insert({thing: v1});
  }
});

And I have the following PhantomJS script:

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

page.onConsoleMessage = function (msg) {
  console.log(msg);
};

page.open('http://localhost:3000/', function () {
    page.evaluate(function () {
        console.log($('#dummy').val());
        $('#data').val('Yo yo yo');
        $('form button').click();
    });

    phantom.exit();
});

I can see in the log that the click event is being triggered, but the Things.insert() call is not working apparently, because the database is empty when I check it. Is there some special way to have PhantomJS test Meteor apps? (the plan is to script Phantom to simulate 10 users filling out the form differently and submitting, about 2 seconds apart from each other)

If I open this in Firefox and open a console and type the below, it works as expected.

$('#data').val('test 123'); $('form button').click();

#2

Okay, so casper was the way to go. It now properly inserts into the database, though the thing value is an empty string for some reason.

var casper = require('casper').create();

casper.onConsoleMessage = function (msg) {
    console.log(msg);
}

casper.start('http://localhost:3000/');

casper.then(function () {
    this.sendKeys('form input#data', 'abcdef');
});

casper.then(function () {
    this.click('form button');
});

casper.run();

The sendKeys call doesn’t seem to be doing its job.


#3

Solution:

var casper = require('casper').create();

casper.onConsoleMessage = function (msg) {
    console.log(msg);
}

casper.start('http://localhost:3000/', function () {
    this.evaluate(function () {
        $('form input#data').val('abcdef');
    });
});

casper.then(function() {
    this.click('form button');
});

casper.run();