Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, February 17, 2011

Swingin' it

Here's a really simple demo, or tutorial if you will, about using the Robotframework SwingLibrary.

Download the latest demo and extract it to the desired directory. Oh, and you should have the Robotframework and Jython installed. Open your command line and navigate to the extracted demo directory. Try to run the finnished demo to verify that everything is installed and working ok. Execute:

run_demo.py example

The SwingLibrary jar needs to be included in the classpath in order to be found by the Robotframework. The run_demo.py does this for you and executes the test suite provided as an argument.

Examine the SUT i.e. Todo List Application. Start the application by running the following in the command line at the demo root:

java -cp lib/swinglibrary-1.1.2-SNAPSHOT-jar-with-dependencies.jar \ org.robotframework.swing.testapp.examplesut.TodoListApplication

As you can see, this a really basic todo list Swing application written in Java. Have a look around and close the application when your done.

Open your favourite text editor. Create a new test suite file (e.g. test.txt) and save it to the demo root. Take the SwingLibrary into use in the test suite, type:

*** Settings ***
Library SwingLibrary

*** Test Cases ***
Test Add Todo Item

(Note! In this plain text format the cell separator is 2 spaces)

Run the test.

run_demo.py example.txt

The test should execute and fail with an error message: "The test case contains no keywords". Ok let's add some and start the application and select it's main window.

*** settings ***
Library SwingLibrary

*** Test Cases ***
Test Add Todo Item
Start Test Application

*** User Keywords ***
Start Test Application
Start Application org.robotframework.swing.testapp.examplesut.TodoListApplication
Select Main Window

Run the test again and now the application should start and test still fails.

Ok, progress. Let's move the start-up stuff into Suite Setup. Then insert text "Buy some milk" into the text field, that the developer has named logically "description". While at it, let's push the "Add Todo Item"-button with a logical name "add".

*** settings ***
Library SwingLibrary
Suite Setup Start Test Application

*** Test Cases ***
Test Add Todo Item
Insert Todo Item Buy some milk

*** User Keywords ***
Start Test Application
Start Application org.robotframework.swing.testapp.examplesut.TodoListApplication
Select Main Window

Insert Todo Item ${arg}
Insert Into Text Field description ${arg}
Push Button add

Run the test and enjoy the greenness. Note, that the user keyword: Insert Todo Item, uses embedded variable syntax. So the "Buy some milk" is provided to the keyword in the same cell.

Great, but still no testing or assertions. We can easily fix that. Select the first (0th) item from the todolist and read the the selected value into an argument. The we can assert that the value is the same as we expect i.e. "Buy some milk".

*** settings ***
Library SwingLibrary
Suite Setup Start Test Application

*** Test Cases ***
Test Add Todo Item
Insert Todo Item Buy some milk
Select From List todolist 0
${item}= Get Selected Value From List todolist
Should Be Equal ${item} Buy some milk

*** User Keywords ***
Start Test Application
Start Application org.robotframework.swing.testapp.examplesut.TodoListApplication
Select Main Window

Insert Todo Item ${arg}
Insert Into Text Field description ${arg}
Push Button add

Run the test and it should pass.

Write tests for the todo item deletion...

Monday, October 18, 2010

Creating Robot Framework jar distribution

As of Robot Framework 2.5.2, it is also distributed as a single stand alone jar file.

The goal was to create a jar distribution with following properties:
  • Standalone executable jar file, i.e. java -jar robotframework.jar mystest.txt would suffice to run tests.
  • The exit code of the above command must be the same as if mytests.txt was executed with pybot.
  • There would also be a programmatic API to start Robot Framework test execution from any Java program
While there was a wealth of information about Jython-Java interaction available in the net, I could not find a single set of instructions describing all the necessary steps to create the required jar.  Since I was able to do it and since the process is general, I decided to write it down.

First requirement is the Jython standalone jar, which can be created with the Jython installer. Following the instructions in the Jython Book, I created three Java classes and one Python for the interaction. These classes implement the one-to-one Jython object factory described in the Jython book. Source code of these classes is available in Robot Framework source repository.

The java classes had to (of course) be compiled before inserting them into the jar file. Additionally, a MANIFEST.MF file with correct main-class had to be created. Once these were done, the files could be inserted into the Jython jar:
  • Robot Framework source code must be placed in Lib directory inside the jar
  • The java code must be inserted starting from the root of the and according to package structure
  • The MANIFEST.MF has be to in META-INF directory
That's pretty standard stuff expect for one gotcha: Zip file format (and jar files are Zip files) supports multiple entries with same path. Since the Jython jar already contains MANIFEST.MF and since Python's zipfile module does not support overriding or removing entries, the first released version of the jar distribution contained two manifest files. For reasons unknown to me, the one we inserted was effective when the jar file was executed with java, but adding files to the jar file with jar failed because of the duplicate manifest.

This was resolved by unpacking the Jython jar and replacing the MANIFEST.MF with our own on the file system before repackaging. .

All the Python source files were compiled with Jython's compileall module before they were inserted into the jar file. This presumably increases performance slightly.

I will probably do another post about how we finally got the jar distribution uploaded to Maven central, since that's an interesting story too.