ebrehault / resurrectio

CasperJS test recorder Chrome extension
GNU General Public License v2.0
721 stars 106 forks source link

select fields change value #20

Open shacky opened 10 years ago

shacky commented 10 years ago

It wasn't implemented, so quick work around in casper.js:

CasperRenderer.prototype.change = function(item) { var tag = item.info.tagName.toLowerCase(); if(tag=='select') { var selector; selector = this.getFormSelector(item) + this.getControl(item); selector = '"' + selector + '"'; this.stmt('casper.waitForSelector('+ selector + ','); this.stmt(' function success() {'); this.stmt(' test.assertExists('+ selector + ');'); this.stmt(' this.evaluate(function(valueOptionSelect){'); this.stmt(' $('+ selector +').val(valueOptionSelect);'); this.stmt(' $('+ selector +').trigger("change");'); this.stmt(' },"'+ item.info.value +'");'); this.stmt(' },'); this.stmt(' function fail() {'); this.stmt(' test.assertExists(' + selector + ');') this.stmt('});'); } }

ebrehault commented 10 years ago

Thank you for your proposal. Unfortunately there are two problem here:

I will try to find a solution to enable a correct select behaviour in Resurrectio.

herryhou commented 8 years ago

ebrehault, have you find solutions to avoid the trigger change event?

For now, I follow shacky's idea to work around this issue (without the jQuery part)

CasperRenderer.prototype.change = function(item) {
  // the submit has been called somehow (user, or script)
  // so  trigger 'SELECT' only

  var tag = item.info.tagName.toLowerCase();
  if(tag=='select' && item.info.value) {
    var selector;
    selector = this.getFormSelector(item) + this.getControl(item);
    selector = '"' + selector + '"';
    this.stmt('casper.waitForSelector('+ selector + ',');
    this.stmt('    function success() {');
    this.stmt('        test.assertExists('+ selector + ');');
    this.stmt('        this.evaluate(function(valueOptionSelect){');
    this.stmt('            document.querySelector('+selector+').value = "'+item.info.value+'";');
    this.stmt('            return true;');
    this.stmt('        });');
    this.stmt('        // Firing onchange event
    this.stmt('        this.evaluate(function() {');
    this.stmt('            var element = document.querySelector(' + selector + ');');
    this.stmt('            var evt = document.createEvent("HTMLEvents");');
    this.stmt('            evt.initEvent("change", false, true);');
    this.stmt('            element.dispatchEvent(evt);');
    this.stmt('        });');
    this.stmt('    },');
    this.stmt('    function fail() {');
    this.stmt('        test.assertExists(' + selector + ');');
    this.stmt('});');
  }
}