Compute Fibonacci numbers with Google Calculator and WebTest

You’ve surely written programs to compute the Fibonacci numbers using different programming languages (if not or if you’ve forgotten, have a look at the Wikipedia article “Fibonacci number“). Nevertheless I’m quite sure that you’ve never seen the following method: it’s an idea of my colleague Tomi Schütz and it uses WebTest to drive Google Calculator (if you don’t know it, just search for instance something like “48 + 89” with Google).

The idea is quite simple:

  • start with the initial values F0 = 0 and F1 = 1
  • invoke Google to compute the sum of these values
  • extract the result from the html page, this is F2
  • invoke Google to compute F1 + F2

Expressed as a WebTest this looks like:

<webtest name="Fibonacci">

<invoke url="http://www.google.com/ncr" description="Go to Google (in English)"/>
<verifyTitle text="Google" />
<setInputField name="q" value="WebTest" />
<clickButton label="Google Search" description="dummy search to cope only with one kind of submit button in the repeat"/>
<storeProperty name="previous" value="0" description="F0"/>
<storeProperty name="current" value="1" description="F1"/>
<repeat startCount="2" endCount="10">

<setInputField name="q" value="#{previous} + #{current}" />
<clickButton label="Search" />
<storeProperty name="previous" value="#{current}"/>
<storeXPath xpath="substring-after(//tr[.//img[@src='/images/calc_img.gif']]/td[3]//b/text(), '= ')" property="current" description="Store Fibonacci #{count}"/> <!-- ugly XPath but it's Google's fault!-->

</repeat>
<verifyProperty name="current" value="55" description="10th Fibonacci number"/>

</webtest>
(sorry for the formatting, WordPress.com is not the best blog hoster for code examples)

Running this script you get results like this: WebTest Fibonacci – Test Results

Ok, it was just for fun because, even if WebTest and Google are very fast, computing the 10th number of the Fibonacci sequence involved getting 11 html response pages and took 2496 ms here!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: