mercredi 1 juin 2016

How to scrap data from api and use it in CasperJS

because I didn't found a method to read data from SQL database directly, I'm trying to create API that will response with an array of URLs and I want to check their response's codes.

var casper = require('casper').create();

casper.start('http://localhost:8000', function() {
    this.fill('form', {
        'login':    'login',
        'password':    'password',
    }, true)
});

casper.then(function() {
    this.open('http://localhost:8000/linkcheck/fetch-links/', {
        method: 'get',
        headers: {
            'Accept': 'application/json'
        }
    });
});

casper.waitForUrl('http://localhost:8000/dashboard/',function() {
    //var urls = ['http://localhost:8000/dashboard/', 'http://localhost:8000/linkcheck/index/']
    var urls = require('utils').dump(JSON.parse(this.getPageContent()).links);
    casper.eachThen(urls, function(response) {
      this.thenOpen(response.data, function(response) {
        console.log('Opened', response.url);
        this.capture('screen.png');
      });
    });
});

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

And it won't work. Then I just pass URLs and comment out fetch function it works as expected:

var casper = require('casper').create();

casper.start('http://localhost:8000', function() {
    this.fill('form', {
        'login':    'login',
        'password':    'password',
    }, true)
});
//commented
/*
casper.then(function() {
    this.open('http://localhost:8000/linkcheck/fetch-links/', {
        method: 'get',
        headers: {
            'Accept': 'application/json'
        }
    });
});*/

casper.waitForUrl('http://localhost:8000/dashboard/',function() {
    var urls = ['http://localhost:8000/dashboard/', 'http://localhost:8000/linkcheck/index/']
    //var urls = require('utils').dump(JSON.parse(this.getPageContent()).links);
    casper.eachThen(urls, function(response) {
      this.thenOpen(response.data, function(response) {
        console.log('Opened', response.url);
        this.capture('screen.png');
      });
    });
});

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

So, how can I retrieve data from API and pass it to Casper eachThen() function?

Aucun commentaire:

Enregistrer un commentaire