0

我正在尝试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.

第一个麻烦当然是失败。但是,我也担心另一个警告(这是随机发生的)。你知道这件事吗?

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

4

2 回答 2

1

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

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

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

于 2013-09-23T15:44:28.300 回答
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 问题。

于 2013-09-23T15:56:52.483 回答