Genrating a Selenium Test Script from Fusebox 5.5 CFC circuit

I was just playing about with Selenium and wondering how I could be sure that I tested all the fuseactions in my application. The application has been converted from an old Fusebox 3 application and the original test scripts were written a long tiem ago and were the kind where you had to manually do everything. Since then some major changes have happened to tha application and the test script has not been updated.

It seemed to me that it should be possible to easily get a list of fuseactions from the application itself. From there it was only a short setp to creating the test script.

Because of the way I have converted the application every circuit in the controller is actually a dummy which points to the real circuit in a subdirectory of the application root.

See my earlier article on the conversion Converting an Application from Fusebox 3 to Fusebox 5.5 (Part 2) for more information.

So I wrote:

<cfset var aFuseactions = getMetaData(this).Extends.Functions>
to get an array of the fuseactions. Each member of the array is a structure containing the name and hint from the code with a list of the arguments for the function call.

Taking the name it was easy to create an HTML table of all the entries following each one with a waitForTextPresent. In many cases in my application the heading in the page is the same as the fuseaction with any underscores converted to spaces.

Once the file is written it needed some editing, such as adding additional parameters and it seemed sensible to remove any fuseactions that would not be able to be tested that simply, such as those that expect form variables. I habitually name those _Action or _Delete so I added a couple of lines to remove them and show the user a list.

Here is the full code which is added to a fuseaction in the circuit.

<cffunction name="Selenium_Test" output="Yes">
      <cfargument name="myFusebox" />
      <cfargument name="event" />
      <cfset var i = 0>
      <cfset var aFuseactions = getMetaData(this).Extends.Functions>
      <cfset var aFuseactionActionNames = arrayNew(1)>
      <cfset var aFuseactionNames = arrayNew(1)>
      <cfset var timeStamp = DateFormat(now(),"yyyymmdd") & TimeFormat(now(),"HHmmss")>
      <cfloop from="1" to="#ArrayLen(aFuseactions)#" index="i">
         <cfif aFuseactions[i].name DOES NOT CONTAIN "Action" AND aFuseactions[i].name DOES NOT CONTAIN "Delete" AND aFuseactions[i].name IS NOT "Selenium_Test" >
            <cfset arrayAppend(aFuseactionNames,aFuseactions[i].name)>
         <cfelseif aFuseactions[i].name IS NOT "Selenium_Test">
            <cfset arrayAppend(aFuseactionActionNames,aFuseactions[i].name)>
         </cfif>
      </cfloop>
      <cfset arraySort(aFuseactionNames,"text","asc")>
      <cfsavecontent variable="request.page.file">
         <cfoutput>
         <table>
         <cfloop from="1" to="#ArrayLen(aFuseactionNames)#" index="i">
            <tr><td>open</td><td>#request.self#?fuseaction=#myFusebox.originalCircuit#.#aFuseactionNames[i]#&amp;grou_select=#attributes.grou_select#</td><td></td></tr>
            <tr><td>waitForTextPresent</td><td>#replace(aFuseactionNames[i],"_"," ","all")#</td><td></td></tr>
         </cfloop>
         </table>
         </cfoutput>
      </cfsavecontent>
      
      <cffile action="WRITE" file="#getDirectoryFromPath(getCurrentTemplatePath())##myFusebox.originalCircuit#_SeleniumTest_#timeStamp#.htm" output="#request.page.file#" addnewline="Yes" fixnewline="No">
      <cfsavecontent variable="request.page.content">
         <cfoutput>
         <p>Selenium Test Script for #ArrayLen(aFuseactionNames)# fuseactions in this circuit written.</p>
         <cfif arrayLen(aFuseactionActionNames) GT 0>
         <p>Unable to create tests for the following:</p>
         <ul>
            <cfloop from="1" to="#arrayLen(aFuseactionActionNames)#" index="i"><li>#myFusebox.originalCircuit#.#aFuseactionActionNames[i]#</li></cfloop>
         </ul>
         </cfif>
         </cfoutput>
      </cfsavecontent>
   </cffunction>

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.