Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't trigger events work in my phantomjs script?

I am writing some automated ui tests for a single page wizard style form that I am building. I am trying to simulate a keyup on a particular input element but the .trigger jquery method doesn't seem to work. This is what I have.

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';



page.open('http://localhost:6543/signup', function (status) {

    if (status !== 'success') {
        console.log('Unable to access network');
    } 
    else {

        var test = page.evaluate(function () {

            //entersnumber
            $('#number').val('2223443');
            //keyup triggers ajax call validating that number is not already in the db
            $('#number').trigger('keyup');
            //the radio button is clicked
            $('input:radio[name=salesBroker]').filter('[value=0]').click();

        });

        page.render('thing.png')        
    }

    phantom.exit();
});

The keyup is important because it will trigger an ajax call to check if the number exists. When I look at the thing.png image the validation error is not showing. This means that the keyup is not being triggered.

the image being saved:

enter image description here

If the keyup works there should be a validation error showing.

like image 893
Spencer Cooley Avatar asked Nov 29 '25 15:11

Spencer Cooley


1 Answers

It looks like one should use a slightly different approach for triggering keyboard-related events: first set focus at a specific input, then call page.sendEvent method with the relevant params:

page.evaluate(function() {
    $('#number').val('2223443').focus();
});
page.sendEvent('keyup', someKey);

By the way, that's a fairly new feature - it debuted in version 1.7.

like image 172
raina77ow Avatar answered Dec 02 '25 05:12

raina77ow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!