Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Robot to test an InputDialog

Posted on 2004-10-11
3
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

715 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