Meego Wiki
Views

Quality/QA-tools/TDriver/Getting Started/Tutorial for test creation

From MeeGo wiki
Jump to: navigation, search

Contents

Testability Driver - Tutorial for test creation

This page describes how to automate a simple test.


Pre-requisites

TDriver and tests/calculator applications are installed.

In file /etc/tdriver/tdriver_parameters.xml , “sut_id” section is empty like below:

<sut id="sut_qt" template="qt"> 
	<!-- use default values --> 
</sut> 

For guidance, see http://wiki.meego.com/Quality/QA-tools/TDriver/Getting_Started.


We should ensure everything is ready for further actions by launching qttasserver, TDriver and calculator applications:

qttasserver &
tdriver_visualizer &
calculator -testability


Then clicking TDriver's “File > Refresh” menu item should lead to get the same view as below:

Visualizer's default view (click to enlarge)


Get a copy of Ruby script template

We get a copy of the template ruby script available under /etc/tdriver/visualizer to your /home/$USER folder for instance, renaming it as my_script.rb:

cp /etc/tdriver/visualizer/plain_tdriver.rb.template /home/$USER/my_script.rb

And update the file's execution attributes:

chmod ugo+x /home/$USER/my_script.rb


Open Ruby script in TDriver

From Tdriver, we click “View > Docks and toolbars > Code Editor” menu item. Code Editor window appears at the bottom of TDriver, we click “File > Open...” menu item and browse to select my_script.rb, we should get the same view as below:

Ruby script into Code Editor (click to enlarge)


Contents of Ruby script template

Now let's have a closer look at the script contents:

Template Ruby script (click to enlarge)

The first two lines mention:

require 'tdriver'
include TDriverVerify

“require” method loads the mentioned file whereas “include” method embeds the specified module into the current module (it can be used to enhance a class). More detail can be found here, here or here.


The following lines are preceeded by commented lines beginning with # character that help understanding their purpose, and we can see that the structure of a Ruby script basically consists of:

  • Defining the SUT (system under test) TDriver should interact with
  • Defining the application to run and test
  • Defining the tests to be performed within the application
  • Finally closing the application


Writing a test using TDriver's graphical interface

So let's say we want to automate a test that clicks calculator's numbered buttons only, and takes a screenshot of the whole application after each click.

First thing to do is update the application name, by completing existing line like below:

@app = @sut.run( :name => "calculator", :arguments => "-testability") 


Then from Code Editor (bottom of TDriver), we place the cursor after the "# Add your interaction and verify code here..." like below:

Writing a test from TDriver UI - 1 (click to enlarge)


From Image View (right part of TDriver), we drag the mouse over “9” button for instance. A red line displays arount it, and “sut” item unfolds to display selected button:

Writing a test from TDriver UI - 2 (click to enlarge)


We right-click and from the contextual menu, we select “Insert into editor” item:

Writing a test from TDriver UI - 3 (click to enlarge)


The following line is added to the script, as shown below:

@app.Button( :name => 'nineButton' )

Writing a test from TDriver UI - 4 (click to enlarge)


Now we need to append the method corresponding to a button click. From Properties (left part of TDriver), we select “Methods” tab, and browse its contents. To get more detail about an item, we simply drag the mouse over it:

Writing a test from TDriver UI - 5 (click to enlarge)


“Tap” method seems to answer our needs, so here again we right-click the desired method and select “Insert to editor”:

Writing a test from TDriver UI - 6 (click to enlarge)


Tap method will be appended to our button line:

Writing a test from TDriver UI - 7 (click to enlarge)


Now let's save our script and test it so far:

  • We don't forget to close calculator as our script will launch and stop it itself.
  • From Code Editor, we click “File > Save” menu item.
  • Then we click “Run > Run with Ruby” menu item.


Through this first test, we could quickly see that calculator opened, 9 number displayed in the editor, then the application closed. A new window called Script Console opened along with calculator, it displays traces resulting from our script's execution:

Writing a test from TDriver UI - 8 (click to enlarge)


Now to complete the test, we will take a snapshot of the application. From the center part of TDriver, we select “Application Calculator” line, then we browse Methods tab to find the appropriate method to use, that is "capture_screen". Finally, we write below line into Code Editor:

@app.capture_screen("PNG", "./calc_snapshot.png") 

The method's arguments are the snapshot's format (i.e. “PNG”), and its path and name.


Seems we are done?! Let's give our script another try and check the snapshot's display.


Well, despite the Script Console not showing any error, instead of displaying number 9 the snapshot may show 0. As a result, this “issue” has more to see with asynchronicity than with a bug since what we observe here is that we kind of took the snapshot too early, and the edit had no time to refresh its display. So before taking the snapshot, we should take some time to verify our input, adding below line right after the call to button 9's tap method:

verify(12, "The taped number should display") {@app.QLineEdit( :"displayText" => @app.Button( :name => 'nineButton' ).attribute("text"))}

Writing a test from TDriver UI - 9 (click to enlarge)


We set a timeout (12 seconds) during which the edit's display value is compared with the text value of taped button. If both values correspond, script continues and snapshot is taken. But if values don't match before timeout expires, then script is halted and exits with code 1, and this information is reported into Script Console. 12 seconds is an arbitrary delay and we assume it is enough for the edit to show up correctly.


Improving a test based on TDriver's features and Ruby syntax

In order to test every numbered button, we could duplicate the lines we have and just update the buttons and snapshots' name. Or we could take advantage of the functionalities offered by TDriver as well as by Ruby scripting language, and in order to prepare to write a program a bit more abstract, we can try to answer some questions like:

  • Is there a way to list every buttons at once?
  • Do numbered buttons have something in common?
  • Do numbered buttons have differences?


Below is an example of a way to write such a script:

Ruby script example (click to enlarge)


Sources and useful links

Personal tools