Solved

Using Robot to test an InputDialog

Posted on 2004-10-11
3
217 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
ID: 12283601
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
ID: 12287349
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
ID: 12293353
Glad to help. :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 104
login jsp example 24 51
How to convert from xls to xlsx using java 7 40
Java Restore security prompts not working 10 13
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

803 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