Selenium IE change event not fired

2019-02-14 05:28发布

问题:

I have a select box which has an event listener on it:

$(document).ready(function () {
    $('#edit-era-code-select').change(function() {
      $.ajax({ 
        url: Drupal.settings.basePath+'era/js', 
        type: 'POST',
        data: "era_code_select="+$(this).val(),
        context: document.body, 
        success: function(){
        window.location.reload(true);
      }
    });  
  });
});

It will works fine when using it in ie/ff/... When doing selenium tests using phpunit it I can see the selectbox being changed but it does not trigger the jquery event change. It only happens in IE. Here is the code that does the command:

AbstractSeleniumTestCaseIexplore::loginShib ( $user ['uid'] );
  $this->waitForElementPresent("//select[@id='edit-era-code-select']", 30000);
  $code = $this->getSelectedLabel("//select[@id='edit-era-code-select']");
  if ($code != 3333) {
    $this->select("edit-era-code-select", "label=3333");
    $this->waitForPageToLoad("30000");  
  }
  ...

Can anyone help?

回答1:

I solved it like this. Its not optimal but it allows you to continue and test other things.

$this->runScript("$('#edit-era-code-select').change()");



回答2:

You can get it work without some hacks, like run script that fire change event. Selenium generate javascript from your code and inject it in browser, than run it. But if you change selected option of any select via javascript it never work.

I've done small example in order to understand that(if you change selected option by hands 'change' event fire, but via javascript not. You can check workable example here):

<select  id="someTxt">
    <option value="1">Option1</option>
    <option value="2">Option2</option>
    <option value="3">Option3</option>
</select>
<div id="button">Change selected option via javascript</div>

<script type="text/javascript">
    $(document).ready(function(){
     $('#someTxt').change(function() {
        alert($("#someTxt option:selected").text());
      });
    $("#button").bind("click", function(){
      $("#someTxt option[value='3']").attr('selected', 'selected');
    });
    });
 </script>

So i think this is okay to fire event manually like:

$this->runScript("$('#edit-era-code-select').change()");


回答3:

You should set the focus first:

runScript("document.getElementById('selectboxID').focus())

This problem will happen in IE browser



回答4:

Have you tried sending keypress events to the select instead of directly modifying the value?