0

CasperJS doesn't seem to be able to see values passed from express via app.settings. Is there something I am leaving out?

Thanks in advance to anyone who can point me to a solution!

Here is a hello world node server:

hello.js
var http = require('http'),
    express = require('express'),
    app = express();

app.configure(function()
{
    app.set('view engine', 'ejs');
    app.set('views', __dirname);
    app.set('message', 'hello world!');
    app.set('secretmessage', 'foobar');

    app.get("/", function(req, res)
    {
        res.render('index.ejs', { layout:false });
    });
});

http.createServer(app).listen(3000, function(){
  console.log("Express server listening on port 3000");
});

In the same folder:

index.ejs
<!DOCTYPE html>
<html>
   <body>
      <input type="hidden" id="secret" value="<%= settings.secretmessage %>">
      <p><%= settings.message %></p>
   </body>
</html>

When I start with node hello.js it tells me appropriately that the server is listening:

Express server listening on port 3000

Here is the html returned, as seen by Firefox - it is as you would expect:

<!DOCTYPE html>
<html>
   <body>
      <input type="hidden" id="secret" value="foobar">
      <p>hello world!</p>
   </body>
</html>

I am trying to test this with casperjs. Here is my test code:

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

casper.start( 'http://localhost:3000', function start()
{
    var secretExists = this.evaluate( function() { return __utils__.exists( '#secret' ) });
    var secretValue = this.evaluate( function() { return __utils__.getFieldValue( 'secret' ) });
    casper.test.comment( 'Confirming hidden field secret exists and has value' );
    casper.test.assert( secretExists, 'secret exists' );
    casper.test.assertTruthy( secretValue, 'secret has value' );
});

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

And here are the results:

$ casperjs indextest.js
# Confirming hidden field secret exists and has value
PASS secret exists
FAIL secret has value
#    type: assertTruthy
#    subject: ""

Instead of seeing the value of secret as "foobar" it is seeing it as "".

4

1 回答 1

0

事实证明,虽然__utils__.exists()使用 CSS 选择器,但__utils__.getFieldValue()使用了 'name' 属性。

name="secret"因此在 index.ejs 中添加属性:

<input type="hidden" id="secret" name="secret" value="<%= settings.secretmessage %>"

导致两个测试都通过。感谢VP的解决方案!

于 2013-04-22T02:03:35.023 回答