I am scraping a page which uses socket.io to populate some select tag options. How can I wait for the socket to receive data before evaluating the page? I am using casperJS
the socket code (loaded by the target site):
socket.on('list', function (data) {
$.each(data.match_names, function (id, name) {
if (some condition) {
/*nothing*/
} else {
if (typeof( varname ) == 'function') {
$('#myselector').append('<option value="' + id + '">' + name + " " + get_tournament_name(id.substr(0, 4)) + '</option>');
} else {
$('#myselector').append('<option value="' + id + '">' + name + '</option>');
}
match_count++;
}
});
I check that the socket.io script has loaded:
casper.waitForResource("socket.io.js", function() {
this.echo('socket.io has been loaded.'); //is printed
//how can I check that data has arrived from 'socket.on('list', function (data)' ?
});
But the option tags are not on the page, presumably because I am evaluating the page too soon
casper.then(function() {
baseTargetUrl = this.evaluate(function() {
return __utils__.getElementByXPath('//*[@id="wrapper"]/div[1]/a[2]')["href"];
});
console.log('logging: '+baseTargetUrl); // works
casper.thenOpenAndEvaluate(baseTargetUrl ,function() { //baseTargetUrl is no longer undefined, it's a closure now
$(function(){ // DOM is ready
var myOptions = [] ;
$('select#myselector option').each(function(){
myOptions.push( $(this).text() + ' : '+$(this).val() ); //additional options have not yet been added
});
});
});
});