如何在CasperJS中测试和获取http身份验证错误

时间:2015-04-20 20:29:14

标签: javascript casperjs http-status

如何在访问具有错误凭据的网页时测试http身份验证错误并使用CasperJS获取错误代码(不能使用SlimerJS等任何其他工具)?

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"]');
    });
    ...

但当然这失败了:

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

如何使用CasperJS进行测试?

BTW,在我的情况下,我首先联系网页的登录页面以填写身份验证。凭据然后加载“欢迎”页面。所以在开始时HTTP状态已经是200OK。

我无法使用status().currentHTTPStatusassertHttpStatus

目前我将代码更改为:

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');
        });
    });

0 个答案:

没有答案