Solved

Using Robot to test an InputDialog

Posted on 2004-10-11
3
221 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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