IsEnabledCondition

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

IsEnabledCondition

Postby mwa » Thu Oct 09, 2008 3:43 am

Hello,

I have written the following helper method:

Code: Select all
    public static void saveAll(final IUIContext ui) throws Exception {
        if (!new IsEnabledCondition(new MenuItemLocator("File/Save All"))
            .testUI(ui)) {
                throw new Exception(
                    "Menu item File/Save All is not enabled as expected.");
        }
        ui.click(new MenuItemLocator("File/Save All"));
    }


If the menu item is disabled, the whole application "hangs" and do not react any more.

Searching for "isenabledcondition" is forbidden in this forum, because they are too common words? Searching google on this site shows nothing to that. So try it with a new entry here. Am I doing this the right way? Has somebody any idea why this fails?


Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: IsEnabledCondition

Postby Phil Quitslund » Fri Oct 10, 2008 2:19 pm

Interesting... I just tried this locally and it worked. What version of Eclipse are you using? And platform?
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: IsEnabledCondition

Postby mwa » Wed Oct 15, 2008 1:38 am

Ok. At least I am on the right way:-) I am using eclipse ganymede on windows.

But wait: after experimenting a litte I found out that this works:
Code: Select all
   if (!new IsEnabledCondition(new MenuItemLocator("File/Save All"))
      .testUI(ui)) {
       fail("Menu item File/Save All is not enabled as expected.");
   }
   ui.click(new MenuItemLocator("File/Save All"));


Meaning for some reason the exception is not thrown. Strange. But its better to use fail() anyway. Thanks for help.

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: IsEnabledCondition

Postby mwa » Wed Oct 15, 2008 6:24 am

Another issue with IsEnabledCondition comes up. I recorded this:
Code: Select all
ui.click(new ContributedToolItemLocator("moveleftid"));

I want to test if this button is enabled. So I do this:
Code: Select all
ui.assertThat("Button moveleftid is not enabled as expected.",
new IsEnabledCondition(new ContributedToolItemLocator("moveleftid")));

The assertion fails. If I run the originally recorded ui.click it works and the Button is pressed. Now I am confused.
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: IsEnabledCondition

Postby Phil Quitslund » Wed Oct 15, 2008 2:26 pm

This looks like a bug in our enablement test. Essentially we ask the underlying widget for it's enablement state and trust the result. In some cases, we've seen some seemingly contradictory results. Clearly this merits further exploration.

Could you submit a bug report so we can schedule a fix?

Thanks!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: IsEnabledCondition

Postby 3061 » Mon Feb 23, 2009 1:10 am

For some reason, when I check for the isEnabled condition of a Button in a Wizard Window, the Window gets close and the Unit Test fails every time.

I have an application where you need to enter some data into text fields and the "Finish" button is grayed out (inactive) until the data entered is valid. Now my GUI test enters false data and I want to make sure the button is correctly grayed out. My software may take some time to verify if the entered data is valid, so I needed to build in a simple timer to avoid WindowTester's WaitTimedOutException. I made this method to check if the button is active (clickable):

Here's the code:

Code: Select all
   protected boolean buttonIsClickable(String buttonString)
   {
      long waitTimeOutInMilliseconds = 1000; // wait for 1 second tops
      int waitStepInMilliseconds = 10; // check state every 1/100 second
      boolean isClickable = true;
   
      try
      {   
         // FIXME For some reason, the wizard window gets closed during this.
         ui.wait(new ButtonLocator(buttonString).isEnabled(true), waitTimeOutInMilliseconds, waitStepInMilliseconds);
      }
      catch (WaitTimedOutException w) // happens if button is still grayed out
      {
         isClickable = false;
      }
             
      return isClickable;
   }



What my test does (in order):
- enter false data into the text fields of the wizard window
- check if the button is enabled (clickable)
- if the "Finish" button can't be clicked, the test enters a random string (just letters and numbers) into one of the text fields to pass the software's name duplication checker.
- if the "Finish" button is enabled, it just clicks it.

Here's the code for that:
Code: Select all
        String B_FINISH = "&Finish";
        [...]

   /** Fills out the name field in a wizard and clicks the "Finish" button
    *
    * @param newObjectName What is entered in a "New" Wizard in the "Name" field
    * If, for some reason, the given name can't be used (could be a duplicate),
    * this method will try a few new random names instead.
    */
   private void enterNewObjectNameAndFinishWizard(String newObjectName) throws Exception
   {
      enterNewPackeNameInNewPackageWizard(newObjectName);
      
      for(int n = 0; n < 10; n++ )
      {
         if (buttonIsClickable(B_FINISH)) // i.e. not grayed out
         {
            System.out.println("Button is clickable. Clicking...");
            clickOnFinishButtonInNewPackageWizard();
            break;
         }   
         else // button is grayed out
         {
            // FIXME the Wizard window gets closed, even when "Finish" could not be clicked
            
            System.err.println("Button not clickable. Trying again with random name");
            enterNewPackeNameInNewPackageWizard(getRandomConformantName());
         }
      }
      
      //waitForJobsComplete();
   }



The buttonIsClickable-method (code above) returns the right value when the button is grayed out and "Button not clickable. Trying again with random name" is correctly outputed to std.err. However the wizard window gets closed in the process and then WindowTester can't find the text fields when the test tries again with random name:

Code: Select all
com.windowtester.runtime.WidgetNotFoundException: Widget NOT Found:
LabeledLocator(org.eclipse.swt.widgets.Text, "Name:")
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:608)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.runtime.swt.internal.UIContextSWT.find(UIContextSWT.java:583)
   at com.windowtester.internal.runtime.selector.ClickHelper.doFind(ClickHelper.java:192)
   at com.windowtester.internal.runtime.selector.ClickHelper.click(ClickHelper.java:58)
   at com.windowtester.internal.runtime.UIContextCommon.click(UIContextCommon.java:82)
   at com.windowtester.runtime.swt.internal.UIContextSWT.click(UIContextSWT.java:296)
   at com.windowtester.internal.runtime.UIContextCommon.click(UIContextCommon.java:75)
   at com.windowtester.runtime.swt.internal.UIContextSWT.click(UIContextSWT.java:304)
   at [...].newWizards.TestNewPackageWizard.enterNewPackeNameInNewPackageWizard(TestNewPackageWizard.java:223)
   at [...].newWizards.TestNewPackageWizard.enterNewObjectNameAndFinishWizard(TestNewPackageWizard.java:166)
   at [...].newWizards.TestNewPackageWizard.createPackageByTypingParentPackage(TestNewPackageWizard.java:129)
   at [...].newWizards.TestNewPackageWizard.testSimplePackageCreationByTypingParentPackage(TestNewPackageWizard.java:68)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:164)
   at junit.framework.TestCase.runBare(TestCase.java:130)
   at com.windowtester.runtime.common.UITestCaseCommon.access$001(UITestCaseCommon.java:25)
   at com.windowtester.runtime.common.UITestCaseCommon$2.run(UITestCaseCommon.java:136)
   at com.windowtester.runtime.common.UITestCaseCommon$3.run(UITestCaseCommon.java:157)
   at com.windowtester.internal.runtime.junit.core.SequenceRunner$1.run(SequenceRunner.java:46)
3061
 
Posts: 42
Joined: Mon Feb 23, 2009 12:43 am

Re: IsEnabledCondition

Postby 3061 » Mon Feb 23, 2009 6:31 am

Nevermind, wait seems to have some internally handled exceptions that closes the window.

I fixed it by replacing with this

Code: Select all
      // Try to click on the Finish button
      IUICondition enabledCond = new ButtonLocator(B_FINISH).isEnabled(true);
      boolean finished = enabledCond.testUI(ui);

      while (!finished)
      {
         ui.pause(100);
         finished = enabledCond.testUI(ui);

         if (!finished)
         {
            System.err.println("Finish Button is grayed out. Can't click it.");
            newPackageName = getRandomConformantName();
            ui.click(new LabeledLocator(Text.class, L_NAME));
            ui.keyClick(SWT.CTRL, 'a');
            ui.enterText(newPackageName);
         }
      }
3061
 
Posts: 42
Joined: Mon Feb 23, 2009 12:43 am

Re: IsEnabledCondition

Postby Phil Quitslund » Tue Feb 24, 2009 2:14 pm

Good deal.

Thanks for following up!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: IsEnabledCondition

Postby 3061 » Thu Feb 26, 2009 7:09 am

I use this thing a lot so I pulled it up in the code. Copy and paste into your code as needed.

Code: Select all
/**
* Checks whether the given button can be clicked or is grayed out.
*
* @param buttonLabel examples: "Next >", "Finish", "OK"
* @return true if button is clickable (enabled), false if it's grayed out
*/
public boolean buttonIsClickable(String buttonLabel)
{   
   boolean isClickable = false;
   IUICondition enabledCondition = new ButtonLocator(buttonLabel).isEnabled(true);
   
   ui.pause(100); // give it some time to update the button status
   isClickable = enabledCondition.testUI(ui);
   
   return isClickable;
}
3061
 
Posts: 42
Joined: Mon Feb 23, 2009 12:43 am

Re: IsEnabledCondition

Postby Phil Quitslund » Fri Feb 27, 2009 12:39 pm

Cool. Thanks for sharing this!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: IsEnabledCondition

Postby 3061 » Mon Mar 09, 2009 2:41 am

Works similarly with MenuItemLocators. Be sure to use the imports with "swt" in them, else the isEnabledCondition might not compile with the given argument:

Use this kind of import in addition to the other WindowTester stuff
Code: Select all
import com.windowtester.runtime.swt.locator.MenuItemLocator;


for making this work:
Code: Select all
   /**
    * Checks whether the given Menu Item can be clicked or is grayed out.
    *
    * @param menuItemPath examples: "File/Save"
    * @return true if the Menut Item is clickable (enabled), false if it's grayed out
    */
   public boolean menuItemIsClickable(String menuItemPath)
   {   
      boolean isClickable = false;
      IUICondition enabledCondition = new MenuItemLocator(menuItemPath).isEnabled(true);
      
      ui.pause(100);
      isClickable = enabledCondition.testUI(ui);
      
      return isClickable;
   }
3061
 
Posts: 42
Joined: Mon Feb 23, 2009 12:43 am

Re: IsEnabledCondition

Postby Phil Quitslund » Mon Mar 09, 2009 8:04 am

Again: thanks for the elaboration!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest