How to click not-unique objects

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

How to click not-unique objects

Postby MarcJ8 » Wed Jan 20, 2010 12:25 am

Has anyone an example how to click not-unique JButtons.

In my test I need to click at least one of two not-unique JButtons. Not-unique means Names are the same, Labels are the same and even the position is the same. The last is due to the fact that they are on different pannels which are on top of eache other.
I don't mind which button would be clicked first.

IWidgetLocator[] frameCloseButtons = ui.findAll(new NamedWidgetLocator(".*_frame_Close")); // The mentioned buttons are named this way
numberofCloseButtons = frameCloseButtons.length;
for (int i=0; i<numberofCloseButtons; i++)
{
try
{
String TString = frameCloseButtons[i].toString();
System.out.println(TString);
if (!TString.contains(",disabled,"))
{
JButtonLocator nwl = (JButtonLocator)frameCloseButtons[i]; // WidgetReference cannot be cast to JButtonLocator (here is my error)
ui.click(nwl);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}

Regards,
Marc Jagt
MarcJ8
 
Posts: 3
Joined: Thu Oct 16, 2008 12:27 am

Re: How to click not-unique objects

Postby keertip » Wed Jan 20, 2010 5:00 pm

Here is a locator class to deal with your special case and get your test working

Code: Select all
class JButtonReferenceLocator extends JButtonLocator implements IWidgetReference {
   
   IWidgetReference ref;
   
   public JButtonReferenceLocator(IWidgetReference ref) {
      super("");
      this.ref = ref;
   }
   
   public IWidgetLocator[] findAll(IUIContext ui) {
      return new IWidgetLocator[] {this};
   }

   /* (non-Javadoc)
    * @see com.windowtester.runtime.locator.IWidgetReference#getWidget()
    */
   public Object getWidget() {
      return ref.getWidget();
   }
   
   public boolean matches(Object widget) {
      return getWidget() == widget;
   }
      
}



Add this to the test or as a separate class. Modify the test as follows, and use the new locator to click the buttons

Code: Select all
if (!TString.contains(",disabled,"))
{
      JButtonReferenceLocator nwl = new JButtonReferenceLocator((IWidgetReference) frameCloseButtons[i]);
      ui.click(nwl);
}

keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest