Solved

Using Robot to test an InputDialog

Posted on 2004-10-11
3
216 Views
Last Modified: 2010-03-31
Hi experts,
I am a java newbie and I'm trying to use the Robot class to test an InputDialog. The problem I run into is when I call the .showInputDialog the dialog box waits for a click on the ok or cancel button from me and doesnt get to the code i've written for the Robot in my test. Is there a way to get around this? If so may i see some sample code. Thanks in advance. Here is the code I have...
public class testMaxWidthDialog extends TestCase
{
      public void testDialog() throws AWTException
      {
            JMaxWidth test = new JMaxWidth();
            Robot r2d2 = new Robot();
            
            r2d2.keyPress(KeyEvent.VK_1);
            r2d2.keyRelease(KeyEvent.VK_1);
            r2d2.keyPress(KeyEvent.VK_ENTER);
            r2d2.keyRelease(KeyEvent.VK_ENTER);
            assertNotNull(test.input);
      }
}
//and the class
public class JMaxWidth extends JPanel
{
      protected String input;
      
      public JMaxWidth() throws AWTException
      {
                      input = JOptionPane.showInputDialog(null, "Enter maximum width.");
                }      
}
0
Comment
Question by:gface
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 100 total points
Comment Utility
Thread roboCodeThread = new Thread(new Runnable(){
public void run(){
//Call to method with robo code you want executed
}
});
roboCodeThread.start();

After the roboCodeThread, put your dialog box and you're set to go. Out of curiosity, why do you want the box to pop up while the robo code is running? If you need the data from this box for any of the robo code execution stuff, wouldn't it be better to get the data first? Or, if you don't need the data for the robo code, wouldn't it be better to get it when you do need it?

P.S.
input = JOptionPane.showInputDialog(null, "Enter maximum width.");
doesn't need the "null."
0
 

Author Comment

by:gface
Comment Utility
Thank you for the reply. I wanted to get in touch with you to let you know I got ypur response. I probably won't be able to test the code until later though. This is for a test driven development class in college and I have to write a test that will pass for all the classes / methods I write. Thats why i need to have the dialog pop up and simulate the data entry with the robot running. Its proven to be kinda frustrating because I know the code will pass if I click on it with the mouse but I need a test to prove it. Eventually I will need to retrieve the data from the dialog box but that will be another test. Gotta get past this one first. :). Be in touch soon.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
Comment Utility
Glad to help. :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now