Sarthak
Sarthak

Reputation: 11

test property of casperjs

What exactly does this code statement (given in the casperjs documentation) do?

return /message sent/.test(document.body.innerText);

The documentation for the same doesn't explain it quite as much. Hence, the trouble.

I'm trying to check if I'm successfully logged into a website using the casperjs fill() method.

Here's my script if it matters:

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

casper.start('http://example.com', function() {
    this.fill('form[action="login.php"]', {
        'username': 'myname',
        'password': 'mypass'
    }, true);
});

casper.then(function() {
    this.evaluateOrDie(function() {
        return /message sent/.test(document.body.innerText);
    }, 'sending message failed');
});

casper.run(function() {
    this.echo('message sent').exit();
});

I need help in casper.then() and casper.run() to check for my login attempt. I'm fairly new to both javascript and casperjs, so please pardon me if its a very basic question.

Upvotes: 0

Views: 451

Answers (1)

hexid
hexid

Reputation: 3811

This isn't a CasperJS feature, but a Javascript one.

What this is doing is testing a string (document.body.innerText) if it matches a regular expression (/message sent/)

In this case, CasperJS will exit if it can't find message sent in the body of the remote page.

You may also want to try using the following as it will make sure that the page has been given time to load before proceeding.

casper.then(function() {
    this.waitForText("message sent", function then() {
        this.echo('message sent');
    }, function timeout() {
        this.die('sending message failed');
    });
});

Upvotes: 3

Related Questions