Forum Discussion

jmcwhinney's avatar
jmcwhinney
Contributor
11 years ago

Focus Issues clicking JButton in Java Application

Below is my code to click a given button:

 

function ClickButton(TheButton){
  //Works Intermittantly
  TheButton.grabFocus();
  aqUtils.Delay(1000);
  TheButton.Click();
}

 

 

If i click the parent window with my mouse before starting the test, the code works.

 

However, if I do not, it seems like the button is clicked, but nothing happens.

My assumption is this has to do with focus.

 

Is there a way I can ensure the button has focus before clicking it?

 

Programatically clicking the button twice seems to work, but that approach seems less than Ideal and I think it could cause other problems.

 

Thanks,

- James

2 Replies

  • vik33's avatar
    vik33
    Occasional Contributor
    Try clicking on the table where the button is by fetching its X, Y co-ordinates. or try fetching the X,Y coordinates for that button. I had made use of these properties of table to select some values in the rows of the table. var_Program_Name_On_Panel = Cstr(obj_tbl_Prof_Service.getValueAt(obj_tbl_Prof_Service.getRowMargin + var_Tbl_Rows ,obj_tbl_Prof_Service.getColumnMargin)) Call obj_tbl_Prof_Service.Click( 85 ,obj_tbl_Prof_Service.getRowHeight * var_Tbl_Rows + 6) Call obj_Btn_Launch.ClickButton Let us know how it goes. -Vik
  • Lage's avatar
    Lage
    Contributor

    Hi,

     

    I have a script called Common_PersistentActions with some functions to retry operations...

     

    If the button must exist after being clicked I can recommend you use this code:  

    'LAGE' Persistent Click until ObjExpected exist
    Function Click_UntilObjExpectedExist(ObjToAction, ObjExpected) 'Ensure that the given object exist If (ObjToAction.Exists) Then 'Try to perform Click operation until the ObjExpected exist While (not ObjExpected.Exists) and (i < 5) '5 is the retries for this operation (you can put this in a project variable i = i + 1 ObjToAction.Click 'performing the L.Click WEnd Else 'the ObjToAction don't exist Log.Error "Object not found. " & ObjToAction.FullName End If End Function

     

     

     

    If the button must Not Exist after click I recommend you use this other function: 

    'LAGE' This function perform an action until Obj Not Exist
    Function Click_WhileObjExist(ObjToAction) 
        'Ensure that the given object exist
        If (ObjToAction.Exists) Then
            'Try to perform some action while the given object Not exist
            While (ObjToAction.Exists) and (i < 5) '5 is the retries for this operation (you can put this in a project variable
              i = i + 1
              ObjToAction.Click 'performing the L.Click
            WEnd
            
        Else 'the objtoAction don't exist
          Log.Error "Object not found. " & ObjToAction.FullName
        End If
        
    End Function

     

    Note Important!: be carefull In the way how pass the objects to this functions. They should be 'Onscreen Object

    2015_05_18_PersistentClick.png