Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casper.js: press key "enter" in an ExtJs input field

I have an ExtJs text field on a page. I am filling it with some value in casper.js, which works fine.
Then I want to focus this field and press the Enter key, as there is no <form> around it to submit.

What I tried was:

casper.then(function() {
  // the text field is filled with the string
  this.sendKeys('#searchfield', 'some text');

  this.evaluate(function() {
    // this does not put the field in focus        
    document.querySelector('#searchfield').focus();

    // so 'pressing' enter has no effect at all
    var evt = document.createEvent('KeyboardEvent');
    evt.initKeyboardEvent('keypress', true, true, window, 0, 0, 0, 0, 0, 13);
    document.dispatchEvent(evt);
  });
});

Do you have any idea how to accomplish this?

like image 782
dan-lee Avatar asked Nov 13 '22 04:11

dan-lee


1 Answers

your code

evt.initKeyboardEvent('keypress', true, true, window, 0, 0, 0, 0, 0, 13);

Look at the fourth param, I guess it should be the trigger element. Here should be document.querySelector('#searchfield') .

One hint: In casper evaluate, return a true in final, and then you will get null if there is any error in evaluate.

var result = this.evaluate(function(){ /*code is here*/ return true;})

check the result, if success

like image 195
Flying Fisher Avatar answered Nov 14 '22 21:11

Flying Fisher