casperjs не распознает глобальную переменную, хранящуюся между операторами - PullRequest
0 голосов
/ 23 июня 2018

, поэтому я вызываю первый URL-адрес ... он возвращает объект JSON, я сохраняю объект JSON в глобальной переменной с именем global_input ...., а затем открываю ссылку, используя global_input.token

  var global_input = {'token' : 'xxx'} ;


    casper.start('http://localhost/client/charg/que' , function (content) {


     })
    .then(function() {
       global_input  = JSON.parse(this.getPageContent());
       casper.log( ' ==== token === > ' + global_input.token    , 'debug');

    })
    .thenOpen('http://localhost/client/charg/go/' + global_input.token , function() {

    })

    .run(function(){
        this.echo("DONE1");
        this.exit();
    });

вот лог

page init .....
[info] [phantom] Step anonymous 2/5 http://localhost/client/charg/que (HTTP 200)
[info] [phantom] Step anonymous 2/5: done in 725ms.
[info] [phantom] Step anonymous 3/5 http://localhost/client/charg/que (HTTP 200)
[debug] [phantom]  ==== token === > e608e91335fd622f430692d40e7ddf0f4b63428d
[info] [phantom] Step anonymous 3/5: done in 750ms.
[debug] [phantom] opening url: http://localhost/client/charg/go/xxx, HTTP GET

, как вы можете видеть, хотя в журнале показано, что token устанавливается на новое значение

==== token === > e608e91335fd622f430692d40e7ddf0f4b63428d

на следующем шаге я по-прежнему получаю значение токена по умолчанию, равное xxx

[debug] [phantom] opening url: http://localhost/client/charg/go/xxx, HTTP GET

я что-то упустил?

1 Ответ

0 голосов
/ 23 июня 2018

Ваш global_input зарегистрирован с начальным значением в методе thenOpen.

как это

  casper.start(static url , callback method)
        .then(callback method)
        .thenOpen(static url (here, initial global object is used) , callback method)
        .run(callback method);

Таким образом, если вы измените что-либо в static_url, casperjs не будет знать, поскольку он уже зарегистрировал эти URL в стеке выполнения casperjs.

Вам нужно сделать вот так

var global_input = {'token' : 'xxx'} ;

// this method will be called again when evaluating the url
function getGlobalToken() {
    return global_input.token;
}

Теперь вызовите метод get, подобный этому

thenOpen('http://localhost/client/charg/go/' + getGlobalToken() , function() {

    })
...