Now that you have the basics of how to set up Fest with your program I should explain more indepth about how Fest can interact with it. Although this may be sound like it will take alot of time to do it is actually quite simple once you know what the methods are and how they can be used. In all the examples from this point on we will assume the main window fixture is called frame.
First bring your main window to the front.
This line will get the main window of your application and bring it to the front of the screen. As we are mimicing user input we do need the window to be on top of the other windows.
How to get access to user interface.
To access the various buttons, combo boxs, lists, radio buttons, check boxs and any other user interface object you can think of (assuming they are in the main frame) you would use one of the following methods passing in the name of the component you wish to interact with. On top of the following there are more methods that will return fixtures for other components that can be found at in the fest swing api found here http://easytesting.org/swing/apidocs/index.html
frame.button(String name); frame.checkBox(String name); frame.comboBox(String name); frame.list(String name); frame.menuItem(String name); frame.radioButton(String name); frame.tree(String name); frame.toggleButton(String name); frame.textBox(String name); frame.tabbedPane(String name);
What do I do with fixtures?
Well that depends what you want to simulate. If you wish to click a button you would use the above method (frame.button(String name)) to get the ButtonFixture (lets call it button). Once you have this object you can do many things such as…
button.click(MouseButton.LEFT_BUTTON); button.doubleClick(); button.focus(); //Button gains focus button.pressKey(KeyEvent.VK_ENTER) //Keep the key pressed button.releaseKey(KeyEvent.VK_ENTER); //Release key if pressed button..pressAndReleaseKeys(KeyEvent.VK_ENTER); //Press the release key
I want to open a file. How do I do this?
First of all you need to click any buttons to bring up your filechooser. Once the button has been pressed you need to get a fixture for the fileChooser. This can be done by doing the following.
JFileChooserFixture fileChooser; fileChooser = JFileChooserFinder.findFileChooser().withTimeout(10000).using(robotf);
These lines of code will look for a JFileChooser for a given period of time (thats what withTimeout(10000) does) using a specified robot which you should have already created.
Then once you have a JFileChooserFixture you need to do the following in order to open the file.
fileChooser.selectFile(new File("C:\filename.pdf")); fileChooser.approve();
These two lines pass the filechooser fixture a file to open and selects the approve option. This will open the file in the correct native way your program usually would.
Now you know how to do the basics and even have an idea how to open files as the user would using fest. Next time I will go into more details that will show you how you can use fest for testing purposes beyond waiting for an exception to occur. If you wish to try Fest yourself it can be found at http://fest.easytesting.org/
I’ll be covering it in more detail in my coming articles.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.