To Support Cookies, Using Flexmojos with Firefox to Run Unittests for CI Server(Hudson) in Headless Environment

February 20th, 2010

Flexmojos is a convenient tool used to compile and test flex applications. However, when running unittests in headless linux with Hudson, I met a problem caused by cookies. The server side needs cookies to make sure requests are valid. I googled a lot and find such a solution finally.

  1. you are required to install xvfb according the instructions in this article.
  2. you must have firefox 3.x.x installed.
  3. navigate to “about:config” in firefox and set “dom.allow_scripts_to_close_windows” to “true”;
  4. register executions for flexmojos-maven-plugin like this:
    <executions>
    	<execution>
    		<id>default-test-compile</id>
    		<configuration>
    			<useNetwork>false</useNetwork>
    		</configuration>
    	</execution>
    
    	<execution>
    		<id>default-test-run</id>
    		<configuration>
    			<flashPlayerCommand>firefox</flashPlayerCommand>
    		</configuration>
    	</execution>
    </executions>
  5. download proper SWC according to the flexmojos version here. For example, I am using flexmojos 3.5.0, so I choose to download flexmojos-unittest-support-3.5.0.swc.
  6. checkout source code of flex-unittests-support with svn here.
  7. create a flex library project with flex(flash) builder called “flexmojos-unittest-support”, a sample project you can find here.

      flexmojos-unittest-support.rar (40.6 KiB, 106 hits)

  8. modify function exitFP defined in class org.sonatype.flexmojos.unitestingsupport.ControlSocket:
    		private function exitFP( event:* ):void
    		{
    			//Exiting
    			trace("Exiting");
    			if(ExternalInterface.available)
    			{
    				var functionStr:String = "function()" +
    					"{" +
    						"window.opener=null;" +
    						"window.open('','_self');" +
    						"window.close();" +
    					"}";
    
    				ExternalInterface.call(functionStr);
    			}
    			else
    			{
    				fscommand("quit");
    			}
    		}
  9. add flexmojos-unittests-support-x.x.x.swc as a library of this project.
  10. compile and build flexmojos-unittest-support.swc.
  11. install flexmojos-unittest-support.swc into maven repository according to the version of the flexmojos being used. For example, I am using flexmojos-3.5.0, so I run the command below under the bin directory of the flex library project:
    mvn install:install-file -DgroupId=org.sonatype.flexmojos -DartifactId=flexmojos-unittest-support
    -Dversion=3.5.0 -Dpackaging=swc -Dfile=flexmojos-unittest-support.swc -DcreateChecksum=true
  12. now, you can run unittests with hudson supporting cookies.

NOTE: The javascript code used to close the browser can work with firefox, can not work with IE8. I do not test the other browsers.

    6 Responses to “To Support Cookies, Using Flexmojos with Firefox to Run Unittests for CI Server(Hudson) in Headless Environment”

    1. VELO Says:

      Nice,

      I never thought that was even possible!

      This could be used on a plugin that runs on integration-test phase…. I did like what you did, what do you think in making it part of flexmojos?

      VELO

    2. Edison Says:

      I think you just need to update your ControlSocket class to support different browsers. However, maybe Firefox support is enough :D. Javascript compatibility is quite annoying.

      Then add a note here to indicate how to solve the cookies problem. The users merely set flashPlayerCommand to “Firefox” and it would work well.

      Edison

    3. StUnT Says:

      I read a article under the same title some time ago, but this articles quality is much, much better. How you do this?

    4. Edison Says:

      Seems your comment is treated as spam by Akismet. What do you mean about “quality”? format? content? or something else?

    5. Thomas Says:

      Thanks for the tutorial, works well on Linux. Does anyone has this working on Mac osx? The problem we encounter on Mac may be due to Firefox not shutting down after the (last) window is closed.

    6. Edison Says:

      Sorry, I have no Mac to test. I think you can google for some information. If you can get an idea, please send me a note. Thank you :D.

    Leave a Reply