I'm having trouble interacting with a jquery auto-complete input box with casperjs. I've tried many different ways, but I can't seem to select the auto complete option when the list of options pops up.
My code is as follows:
casper.thenEvaluate(function() {
$('#myInput').val('cars'); // fill in the text box
$('#myInput').blur(); // should trigger the autocomplete ajax call
$('.ui-autocomplete li.ui-menu-item:nth-of-type(1)').click(); // should click the first item in the list
});
// take a picture to make sure it worked
casper.then(function() {
this.captureSelector('pics/test1.png', '#theForm');
});
This doesn't work at all, even though it looks like it should. By playing around with it, I've found that triggering a down arrow keypress a few times triggers the auto complete to show up, so here is a version that is closer to working. This works in the browser, but not in a casper.thenEvaluate block for some reason.
$('#myInput').val('cars'); // fill in the text box
var e = jQuery.Event("keydown");
e.which = 40; // press down arrow a few times, not sure why this works
$("#myInput").trigger(e);
$("#myInput").trigger(e);
$('.ui-autocomplete li.ui-menu-item:nth-of-type(1)').click();
This is not based on jQuery UI's autocomplete, but rather Typeahead which is related to Bootstrap, but the mechanics should be the same.
I have had success with the following:
This runs in a
casper.then()
function, thesendKeys
selects a field by ID and passes "a" to it. ThekeepFocus
is important, otherwise it will deselect the input after inserting the text!Wait for
.typeahead
to appear, that waits for the AJAX call to the server, then click the firsta
element in the Typeahead.And lastly test the value of the field. Note that the field selector is its name, not a CSS3 selector.