質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top