<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : Tag culerity, everything about culerity</title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>Testing PUT requests with Culerity</title>
      <description>&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="/files/Image/Bildschirmfoto%202010-02-02%20um%2012.38.41.png" alt="All green!" /&gt;&lt;/p&gt;


&lt;p&gt;One major pain at testing with culerity so far was a HtmlUnit bug that causes a ScriptException with PUT AJAX requests in jQuery. When you tried to test an update you got a &amp;#8220;java.lang.IllegalArgumentException: The content cannot be null&amp;#8221;. Fortunately, this bug is fixed now. With latest HtmlUnit build culerity should run fine, and I tell you how.&lt;/p&gt;


&lt;p&gt;I assume you have culerity installed already. If not, follow &lt;a href="http://github.com/langalex/culerity"&gt;these instructions&lt;/a&gt; for testing Rails apps &lt;a href="http://upstre.am/2009/10/25/testing-couchapps-with-cucumber-and-culerity/"&gt;or these&lt;/a&gt; for testing Couchapps.&lt;/p&gt;


&lt;p&gt;1. Make sure you have up-to-date versions of celerity and culerity:&lt;br/&gt;
$ jruby -S gem install celerity #(gives you celerity-0.7.8)&lt;br/&gt;
$ gem install culerity &amp;#8211;source http://gemcutter.org #(gives you culerity-0.2.7)&lt;/p&gt;


&lt;p&gt;2. Get the latest HtmlUnit nightly build:&lt;br/&gt;
$ wget http://build.canoo.com/htmlunit/artifacts/htmlunit-2.7-SNAPSHOT-with-dependencies.zip&lt;br/&gt;
$ unzip htmlunit-2.7-SNAPSHOT-with-dependencies.zip &lt;br/&gt;
$ cd htmlunit-2.7-SNAPSHOT-with-dependencies/lib&lt;br/&gt;
These are the jar files you need to copy into your celerity gem.&lt;/p&gt;


&lt;p&gt;3. Find your celerity gem and copy the files:&lt;br/&gt;
$ jruby -S gem which celerity&lt;br/&gt;
$ cp * PATH_OF_JRUBY_INSTALLATION/lib/ruby/gems/1.8/gems/celerity-0.7.7/lib/celerity/htmlunit&lt;/p&gt;


&lt;p&gt;4. The new HtmlUnit needs JQuery 1.4, so you &lt;a href="http://docs.jquery.com/Downloading_jQuery#Current_Release"&gt;need to upgrade&lt;/a&gt; if you haven&amp;#8217;t done already. There&amp;#8217;s a &lt;a href="http://github.com/jquery/jquery-compat-1.3"&gt;backwards compatibility plugin for 1.3&lt;/a&gt; if this gives you trouble.
&lt;/p&gt;


&lt;p&gt;Have fun!&lt;/p&gt;
</description>
      <pubDate>Sun, 31 Jan 2010 21:22:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:40dcb9ab-9661-4888-bdb1-d9ac36fb7fc2</guid>
      <comments>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity#comments</comments>
      <category>testing</category>
      <category>culerity</category>
      <category>ajax</category>
      <category>htmlunit</category>
      <category>bug</category>
      <category>celerity</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=10</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity</link>
    </item>
    <item>
      <title>Testing key events with Culerity</title>
      <description>&lt;p&gt;The application I'm writing depends heavily on key events. When you press enter in a specific text field, some other field opens, when you press tab, the text fields moves, etc. Of course I need to cover this in my cucumber integration tests. The &lt;a href="http://celerity.rubyforge.org/yard/Celerity/Element.html#fire_event-instance_method"&gt;Celerity API&lt;/a&gt; is a bit thin for this purpose: You can fire a, let's say, keydown event - but it doesn't pass through the specific key code.&lt;/p&gt;


&lt;p&gt;Actually it's easy when you know how. You can execute any javascript directly on the Culerity RemoteBrowserProxy object. In order to fire a specific key event, you have to create a KeyboardEvent, initialize it with whatever you want, and dispatch it on the element.&lt;/p&gt;


&lt;p&gt;Read up the details, especially the meaning of initKeyEvent's parameters &lt;a href="http://help.dottoro.com/ljbwbehw.php"&gt;in this Javascript API&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;I ended up with this cucumber step:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I hit &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; in a text_field with id &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |key, id|
  key_code = &lt;span class="r"&gt;case&lt;/span&gt; key
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;enter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;13&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;up&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;38&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;down&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;40&lt;/span&gt;
    &lt;span class="r"&gt;else&lt;/span&gt;
      &lt;span class="i"&gt;0&lt;/span&gt;
    &lt;span class="r"&gt;end&lt;/span&gt;
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event = document.createEvent(&amp;quot;KeyboardEvent&amp;quot;);&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event.initKeyEvent(&amp;quot;keydown&amp;quot;, true, false, document.window, false, false, false, false, &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; + key_code.to_s + &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;, 0)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;document.getElementById('&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;').dispatchEvent(event)&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;I wait for the AJAX call to finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note that this is meant to work for Firefox only. For IE you can do a similar thing with createEventObject, &lt;a href="http://help.dottoro.com/ljhlvomw.php"&gt;see here how&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Sun, 22 Nov 2009 20:43:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0bb79202-702f-46ab-a4c1-4a9aecaf11b8</guid>
      <comments>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity#comments</comments>
      <category>cucumber</category>
      <category>culerity</category>
      <category>celerity</category>
      <category>testing</category>
      <category>javascript</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=7</trackback:ping>
      <link>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity</link>
    </item>
    <item>
      <title>Couchapp / Culerity glitches, part 1</title>
      <description>&lt;p&gt;Do you know what's behind these buzzwords: &lt;a href="http://couchdb.apache.org"&gt;CouchDB&lt;/a&gt; / &lt;a href="http://github.com/couchapp/couchapp"&gt;Couchapp&lt;/a&gt; / &lt;a href="http://code.quirkey.com/sammy/"&gt;Sammy.js&lt;/a&gt;? Not really? Then please look at this article now: &lt;a href="http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture"&gt;Sammy.js, CouchDB, and the new web architecture&lt;/a&gt;. The title says it all.&lt;/p&gt;


&lt;p&gt;This setup happens to be the field of my diploma thesis, friendly sponsored and supported by &lt;a href="http://upstream-berlin.com"&gt;Upstream&lt;/a&gt;. During the next months I'll develop a super duper thing with these technologies, and then I'll write 80 scientific pages about it. Yees, I'm not that much looking forward to the second part! Until then, I'm planning to semi-regularly blog about my findings.&lt;/p&gt;


&lt;p&gt;I'm not allowed to share the code before I have the diploma in my hands. But, because my workmate &lt;a href="http://twitter.com/bionadeholunder"&gt;Frank Proessdorf&lt;/a&gt; is so jealous of what I do, he and I started working on an app with a similar setup on our Upstream Research Fridays, to try things out. &lt;a href="http://github.com/lenalena/jsdoodle"&gt;Here it is&lt;/a&gt;. Don't expect anything yet.&lt;/p&gt;


&lt;p&gt;Today I'm going to tell you about some details of my testing setup. My *cough* supervising tutor &lt;a href="http://twitter.com/langalex"&gt;Alexander Lang&lt;/a&gt; already wrote down &lt;a href="http://upstream-berlin.com/2009/10/25/testing-couchapps-with-cucumber-and-culerity"&gt;all there is to know yet&lt;/a&gt; about Test-Driven Development with Cucumber-Culerity-Celerity and Couchapps.&lt;/p&gt;


&lt;p&gt;But there are a few things that will make your life hard, when the almost perfect Rails testing world has spoiled you as much as me. One thing is that we have to deal with a lot of asynchrony. Sammy renders the page, loads the data from the couch, and whenever the data is there it gets rendered into the page. The best way to do this is via callbacks. I'm going to write another blog post about that soon.&lt;/p&gt;


&lt;p&gt;In the integration test the problem is that celerity doesn't behave as it claims it does:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="gv"&gt;$browser&lt;/span&gt;.wait&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;is &lt;em&gt;supposed&lt;/em&gt; to make the test wait until everything is rendered completely. But for whatever reason it doesn't. The result is that your test fails because it searches for your expressions in an unfinished page.&lt;/p&gt;

&lt;p&gt;Because of that, in culerity we need something like:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;sleep &lt;span class="fl"&gt;0.4&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;after every follow, press and go-to-path step. When using culerity in a Rails environment, this usually is enough. Sammy behaves a bit more unpredictable - it sometimes takes more than a whole second until everything is loaded.&lt;/p&gt;

&lt;p&gt;Solution? We can either increase the sleep time a lot, but that's not very nice, the features need long enough to run as it is. Fortunately we can use another method that's build into celerity: wait_while.&lt;/p&gt;

&lt;p&gt;I created a div "spinner" somewhere in index.html:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt; &lt;span class="ta"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="an"&gt;id&lt;/span&gt;=&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="ta"&gt;&amp;gt;&lt;/span&gt;&lt;span class="ta"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;You can also add an actual spinner gif here. Just take care to place the div outside of your sammy element selector ("#main" by default), because this div gets replaced all the time.&lt;/p&gt;

&lt;p&gt;We need to show that div before every route that gets run. In application.js:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;before(&lt;span class="kw"&gt;function&lt;/span&gt;() {
  &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).show();
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;And we need to hide it after your rendering is completed. I haven't found an ideal solution where to do that, it depends on the application you are writing. As a quick and dirty fix, you can add it as a last line in sammy's partial function, in sammy.js:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;partial: &lt;span class="kw"&gt;function&lt;/span&gt;(path, data, callback) {
  &lt;span class="c"&gt;//...&lt;/span&gt;
  &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).hide();
},&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I prefer to have more control about when I hide it, as my partial calls sometimes have nested callbacks. So I put it in application.js, resp. in my controllers, after each render call or in the partial callbacks. I admit this still sucks a bit.&lt;/p&gt;

&lt;p&gt;Finally, amend the cucumber step:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;I wait for the AJAX call to finish&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt;
  sleep &lt;span class="fl"&gt;0.2&lt;/span&gt;
  puts &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Waiting for page to load ...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;if&lt;/span&gt; &lt;span class="gv"&gt;$browser&lt;/span&gt;.div(&lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).visible?
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.wait_while { &lt;span class="gv"&gt;$browser&lt;/span&gt;.div(&lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).visible?} 
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The browser still needs to sleep a bit before it actually finds the spinner div. But with this step in my common_culerity_steps.rb, the features finally run smoothly.&lt;/p&gt;</description>
      <pubDate>Wed, 04 Nov 2009 10:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3e495ea7-1021-404e-b3ba-18267d409146</guid>
      <comments>http://lenaherrmann.net/2009/11/04/couchapp-culerity-glitches-part-1#comments</comments>
      <category>cucumber</category>
      <category>culerity</category>
      <category>celerity</category>
      <category>couchapp</category>
      <category>couchdb</category>
      <category>sammy.js</category>
      <category>javascript</category>
      <category>bug</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=6</trackback:ping>
      <link>http://lenaherrmann.net/2009/11/04/couchapp-culerity-glitches-part-1</link>
    </item>
  </channel>
</rss>
