Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Robot to test an InputDialog

Posted on 2004-10-11
3
Medium Priority
?
232 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 300 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

916 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