admin管理员组文章数量:1327113
I'm using the following code to load some JSON data into a variable in my casperJS script:
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
pageSettings: {
userName: 'dev',
password: 'devpass',
}
});
var baseUrl = '/';
casper.start().then(function() {
this.open(baseUrl + 'JSON-stuff', {
method: 'get',
headers: {
'Accept': 'application/json'
}
});
});
casper.run(function() {
var journalJson = JSON.parse(this.getPageContent());
require('utils').dump(journalJson); //this returns my json stuff as expected
this.exit();
});
This works like I want - I have the journalJson object that I need to do my processing. However, I'm not sure how to continue with my testing. Other functions added to casper.run() do not execute as expected... for instance, if I change the casper run function to:
casper.run(function() {
var journalJson = JSON.parse(this.getPageContent());
require('utils').dump(journalJson);
this.open(baseUrl).then(function () {
this.assertExists('#header');
});
this.exit();
});
then phantomjs logs that the url is requested, but the test is never run.
My question: How can I access JSON via get, and then use it to perform tests? I think that I'm missing something here...
I'm using the following code to load some JSON data into a variable in my casperJS script:
var casper = require("casper").create({
verbose: true,
logLevel: 'debug',
pageSettings: {
userName: 'dev',
password: 'devpass',
}
});
var baseUrl = 'http://mysite./';
casper.start().then(function() {
this.open(baseUrl + 'JSON-stuff', {
method: 'get',
headers: {
'Accept': 'application/json'
}
});
});
casper.run(function() {
var journalJson = JSON.parse(this.getPageContent());
require('utils').dump(journalJson); //this returns my json stuff as expected
this.exit();
});
This works like I want - I have the journalJson object that I need to do my processing. However, I'm not sure how to continue with my testing. Other functions added to casper.run() do not execute as expected... for instance, if I change the casper run function to:
casper.run(function() {
var journalJson = JSON.parse(this.getPageContent());
require('utils').dump(journalJson);
this.open(baseUrl).then(function () {
this.assertExists('#header');
});
this.exit();
});
then phantomjs logs that the url is requested, but the test is never run.
My question: How can I access JSON via get, and then use it to perform tests? I think that I'm missing something here...
Share Improve this question asked Jul 19, 2012 at 18:14 starsinmypocketsstarsinmypockets 2,2944 gold badges35 silver badges46 bronze badges1 Answer
Reset to default 6You're calling casper.exit()
before your then
callback being possibly performed.
Try something like this instead:
casper.then(function() { // <-- no more run() but then()
var journalJson = JSON.parse(this.getPageContent());
require('utils').dump(journalJson);
});
casper.thenOpen(baseUrl, function() {
this.test.assertExists('#header'); // notice: this.test.assertExists, not this.assertExists
});
casper.run(function() {
this.test.done();
});
本文标签: javascriptHow to load JSON via get into variable in CasperJS scriptStack Overflow
版权声明:本文标题:javascript - How to load JSON via get into variable in CasperJS script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203502a2432388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论