Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium IE change event not fired

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?

like image 748
domidc Avatar asked Dec 22 '10 10:12

domidc


2 Answers

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()");

like image 61
domidc Avatar answered Oct 12 '22 00:10

domidc


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()");
like image 26
Andrew Orsich Avatar answered Oct 12 '22 01:10

Andrew Orsich