无法使用casperjs运行基本测试

时间:2013-09-23 13:40:58

标签: javascript casperjs

我正在尝试使用casperjs提交表单,这是一个根据文档的简单任务。 但是,它只是不起作用。

以下是代码:

casper.test.begin('Logging in', nbTests, function(test) {
    casper.start(A_SERVER);
    casper.then(function() {
    this.fill('form', {
        'username': GOOD_LOGIN,
        'password': GOOD_PASSWORD}, true);
this.waitUntilVisible('.success');
this.waitForText('Successful login', function (){
    casper.test.assertExists('.success');    
});
    });

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

然而,这样做会返回:

# Logging in
PASS Found an element matching: form
FAIL Redirection to dashboard after successfull login.
#    type: assertExists
#    file: /home/fx/SRC/server/tests_integration/ti_login.js:28
#    code: test.assertExists('.success', "Redirection to dashboard after successfull login.");
#    subject: false
#    selector: ".success"
Unsafe JavaScript attempt to access frame with URL http://127.0.0.1:8000/accounts/login/ from frame with URL file:////etc/local/bin/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

第一个麻烦当然是失败。但是,我也担心另一个警告(随机发生)。 你对此有所了解吗?

编辑:我重构了代码,现在它可以工作了。但是,我不明白为什么。是否有人愿意解释这个问题?

2 个答案:

答案 0 :(得分:1)

您可以尝试像这样运行CasperJS:

casperjs --web-security=false test test.js

这将允许跨域XHR,这可能对您的情况有用。

答案 1 :(得分:0)

事实上,我刚刚使用casperjs。以下是我设法生成的功能代码:

casper.test.begin('Logging in', nbTests, function(test) {
    casper.start(A_SERVER + A_LOGIN);

    casper.then(function goodInput(){
    this.fill('form', {
        'username': GOOD_LOGIN,
        'password': GOOD_PASSWORD}, true);

    this.waitUntilVisible('#id_file_field');
    this.waitForText('Successful login', function (){
        casper.test.assertExists('#id_file_field', "Login successful");    
    });

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

但是,我仍然不知道造成XHR问题的原因。

相关问题