2

How can I test http authentication error and fetch the error code with CasperJS (can't use any other tool like SlimerJS) when accessing a web page with wrong credentials?

My code start with:
var casper = require('casper').create();

if (casper.cli.has("ip") === false) {
  casper.echo("Usage: casper.js test sample.js --ip=<x.x.x.x>").exit();
}

casper.test.begin('Get info', 1, function suite(test) {
    casper.start("http://" + casper.cli.get("ip") + ":xxxx/menta/", function() {
        test.assertTitle("Operation & Management", "Correct title");
        test.assertExists('form[name="loginForm"]', "login form is found");
        this.fill('form[name="loginForm"]', {
                            'userId': 'root',
                            'password': '12345678'
        }, false);
    });

    casper.then(function() {
        test.assertTextExists("Welcome", "Login");
        this.click('a#menu.menu1itemUnSel[tabindex="4"]');
    });
    ...

But of course this fails:

# Get info
PASS Correct title
PASS login form is found
FAIL Login
#    type: assertTextExists
#    subject: false
#    text: "Welcome"

How can I test this with CasperJS?

BTW, in my case i first contact the login page of the web-page to fill in auth. credentials and then a "Welcome" page is loaded. So at the beginning the HTTP status is already 200OK.

I couldn't manage to use the status().currentHTTPStatus or assertHttpStatus.

For the moment i changed my code to:

casper.test.begin('Get info', 1, function suite(test) {
    casper.start("http://" + casper.cli.get("ip") + ":xxxx/menta/", function() {
        test.assertTitle("Operation & Management", "Correct title");
        test.assertExists('form[name="loginForm"]', "login form is found");
        this.fill('form[name="loginForm"]', {
                            'userId': 'root',
                            'password': '12345678'
        }, false);
        this.waitForText("Welcome", function then() {
          this.echo('message sent');
        }, function timeout() {
            this.die('sending message failed');
        });
    });
FotisK
  • 1,055
  • 2
  • 13
  • 28
  • possible duplicate of [Casperjs test if page opens](http://stackoverflow.com/questions/24818166/casperjs-test-if-page-opens) – Artjom B. Apr 20 '15 at 20:44
  • Well, make up you mind. Is the test based on the HTTP status code or not? Only you know how the test should be written and how the page responds. – Artjom B. Apr 21 '15 at 07:29
  • Well, it seems that HTTP status is not in this case suitable. My solution works of course, but i was thinking of catching if possible also the error slogan that is printed at the same login page (credentials are cleared also). So, that's how the page is working, actually, like the most login web-pages out there, correct! – FotisK Apr 21 '15 at 10:36

0 Answers0