title: Simulate programmatic button click using Phantomjs and test with Jasmine
body: <p>Using the <a href="https://github.com/mrosati84/phantomjs-jasmine" rel="nofollow">phantomjs-jasmine</a> to make a simple test</p>
  
  <pre><code>//example_spec.js
  describe("Click button", function() {
    it ("should be become 3", function() {
        var i = 0;
        var button_element = $('#button');
        console.log(button_element.text());
        while(i &lt; 3 ) {
            button_element[0].click();
        console.log($('#counter').text());
          i ++;
        }
        console.log($('#counter').text());
      expect($('#counter').text()).toEqual('3');
    });
  
  });
  
  
  
  //example.js
  var main = function() {
      var button = document.getElementById('button');
  
      button.addEventListener('click', function(){
          var count = document.getElementById('counter');
          count.innerText = parseInt(count.innerText) + 1;
      });
  }
  window.addEventListener('load', main);
  
  
  
  
  window.addEventListener('load', main);
  
  //index.html
  ....
  &lt;p id='counter'&gt;0&lt;/p&gt;
  &lt;button id='button'&gt;&lt;/button&gt;
  ....
  </code></pre>
  
  <p>The testing result is really strange</p>
  
  <pre><code>hantomjs lib/run_jasmine_test.coffee spec/TestRunner.html
  Starting...
  0
  0
  0
  
  Click button : should be become 3
  Error: Expected '1' to equal '3'.
  
  Finished
  -----------------
  1 spec, 1 failure in 0.033s.
  
  ConsoleReporter finished
  </code></pre>
  
  <p>some thing must be wrong in my code, any idea?</p>
  
  <hr>
  
  <p><strong>//example-updated-jquery-version.js</strong></p>
  
  <pre><code>var main = function() {
      var button = $('#button');
  
      $('#button').on('click', function(){
          $('#counter').text(parseInt($('#counter').text()) + 1);
      })
  
  }
  </code></pre>