2

I have an application window open. This window contains a list of connections. I want to uniquely retrieve the text(i.e. connection name) which is in read-only state from the list using java with the help of autoIt. I have got two connections in the list.
For this project I am using tools and technologies as follows:
1) Java SE 8
2) AutoIt

Although the window which contains the list of connections has a unique ID, individual elements from the list doesn't have any. So searching and getting the required unique text by ID is not an option. I have used ControlListView method of autoIt to get the desired result. Using above method in java with the help of autoIt I was successfully able to use methods viz., controlListViewSelectClear(), controlListViewSelectAll(). But I was getting error for methods viz., controlListViewGetItemCount(), controlListViewFindItem(), controlListViewIsSelected(). Also methods viz., controlListViewGetText(),

Below is the code sample and its output:

public class TestAutoIt {

    /*Choose the 'JACOB' dll based on the JVM bit version.*/
    .
    .
    .
    final String APPLICATION_TITLE = "ABC";
    final String CONNECTION_NAME = "APP Connection 1";

    private AutoItX control;

    {
        /*Load the jacob dll.*/
        .
        .
        .
        control = new AutoItX();
    }

    public static void main(String[] args) {
        try {
            TestAutoIt obj = new TestAutoIt();

            /*Returns the ControlRef# of the control that has keyboard focus within a specified window.*/
            String stringFocus = obj.control.controlGetFocus(APPLICATION_TITLE);
            System.out.println("stringFocus: " + stringFocus);
            if(stringFocus == "") {
                System.out.println("Unable to get keyboard focus.");
                throw new Exception("Unable to get keyboard focus.");
            }

            /*Get total connections present in the list*/
            System.out.println("totalConnections: " + obj.control.controlListViewGetItemCount(APPLICATION_TITLE, "", stringFocus)); // Error

             /*Check if required connection is present or not in the connection list. If present, returns index position of element else -1.*/
            System.out.println("Index of desired connection: " + obj.control.controlListViewFindItem(APPLICATION_TITLE, "", stringFocus, CONNECTION_NAME, "")); // Error

            /*Check if desired connection in the list is selected or not*/
            System.out.println("Connection selected: " + obj.control.controlListViewIsSelected(APPLICATION_TITLE, "", stringFocus, CONNECTION_NAME)); // Error

            /*Get text of required connection*/ // not working
            obj.control.sleep(2000);
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"0\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "0"));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"1\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "1"));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"1\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"1\", \"1\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "1"));
            obj.control.sleep(2000);

            /*Change view to list view*/ // not working
            obj.control.controlListViewSelectViewChange(APPLICATION_TITLE, "", stringFocus, "list"); // No error but there was no change in the view

            /*Clear list selection*/
            obj.control.controlListViewSelectClear(APPLICATION_TITLE, "", stringFocus); // Worked successfully

            /*Select All*/
            obj.control.controlListViewSelectAll(APPLICATION_TITLE, "", stringFocus, "0", "1"); // Worked successfully

        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
    }
}

Output of the above code is as follows:

/*Output for controlListViewGetItemCount() method*/
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewGetItemCount(AutoItX.java:1518)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:88)

/*Output for controlListViewFindItem() method*/
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewFindItem(AutoItX.java:1488)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:101)

/*Output for controlListViewIsSelected() method*/   
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewIsSelected(AutoItX.java:1567)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:129)

/*Output for controlListViewGetText() method*/
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "", ""): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", ""): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "0"): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "1"): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "")): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "1"): 

To solve above error I referred to source code of AutoItX.java, com.jacob.com.Variant Class and com.jacob.activeX.ActiveXComponent Class. I updated the above code as below:

public class TestAutoIt {

    /*Choose the 'JACOB' dll based on the JVM bit version.*/
    .
    .
    .
    final String APPLICATION_TITLE = "ABC";
    final String CONNECTION_NAME = "APP Connection 1";

    private AutoItX control;

    {
        /*Load the jacob dll.*/
        .
        .
        .
        control = new AutoItX();
    }

    public static void main(String[] args) {
        try {
            TestAutoIt obj = new TestAutoIt();

            /*Returns the ControlRef# of the control that has keyboard focus within a specified window.*/
            String stringFocus = obj.control.controlGetFocus(APPLICATION_TITLE);
            System.out.println("stringFocus: " + stringFocus);
            if(stringFocus == "") {
                System.out.println("Unable to get keyboard focus.");
                throw new Exception("Unable to get keyboard focus.");
            }

            /*Get total connections present in the list*/
            Variant vTitle1 = new Variant(APPLICATION_TITLE);
            Variant vText1 = new Variant("");
            Variant vControl1 = new Variant(stringFocus);
            Variant vCommand1 = new Variant("GetItemCount");
            Variant vFrom1 = new Variant("");
            Variant vTo1 = new Variant("");
            Variant[] params1 = new Variant[]{vTitle1, vText1, vControl1, vCommand1, vFrom1, vTo1};
            Variant variantInst1 = activeXComponentInst.invoke("ControlListView", params1);
            //System.out.println("variantInst1.toString(): " + variantInst1.toString());
            int totalConnections = Integer.parseInt(variantInst1.toString());
            System.out.println("totalConnections: " + totalConnections);

            /*Check if required connection is present or not in the connection list. If present, returns index position of element else -1.*/
            Variant vTitle2 = new Variant(APPLICATION_TITLE);
            Variant vText2 = new Variant("");
            Variant vControl2 = new Variant(stringFocus);
            Variant vCommand2 = new Variant("FindItem");
            Variant vConnectionName2 = new Variant(CONNECTION_NAME);
            Variant vOption2 = new Variant("");
            Variant[] params2 = new Variant[]{vTitle2, vText2, vControl2, vCommand2, vConnectionName2, vOption2};
            Variant variantInst2 = activeXComponentInst.invoke("ControlListView", params2);
            System.out.println("variantInst2.toString(): " + variantInst2.toString());

            /*Check if desired connection in the list is selected or not*/
            Variant vTitle4 = new Variant(APPLICATION_TITLE);
            Variant vText4 = new Variant("");
            Variant vControl4 = new Variant(stringFocus);
            Variant vCommand4 = new Variant("IsSelected");
            Variant vConnectionName4 = new Variant(CONNECTION_NAME);
            Variant vOption42 = new Variant("");
            Variant[] params4 = new Variant[]{vTitle4, vText4, vControl4, vCommand4, vConnectionName4, vOption42};
            Variant variantInst4 = activeXComponentInst.invoke("ControlListView", params4);
            System.out.println("\nvariantInst4.toString(): " + variantInst4.toString());

            /*Get text of required connection*/
            Variant vTitle3 = new Variant(APPLICATION_TITLE);
            Variant vText3 = new Variant("");
            Variant vControl3 = new Variant(stringFocus);
            Variant vCommand3 = new Variant("GetText");
            Variant vOption31 = new Variant("0");
            Variant vOption32 = new Variant("0");
            Variant[] params3 = new Variant[]{vTitle3, vText3, vControl3, vCommand3, vOption31, vOption32};
            Variant variantInst3 = activeXComponentInst.invoke("ControlListView", params3);
            System.out.println("\nvariantInst3.toString(): " + variantInst3.toString());
            System.out.println("variantInst3.getString(): " + variantInst3.getString());

            /*Change view to list view*/ // not working
            obj.control.controlListViewSelectViewChange(APPLICATION_TITLE, "", stringFocus, "list"); // No error but there was no change in the view

            /*Clear list selection*/
            obj.control.controlListViewSelectClear(APPLICATION_TITLE, "", stringFocus); // Worked successfully

            /*Select All*/
            obj.control.controlListViewSelectAll(APPLICATION_TITLE, "", stringFocus, "0", "1"); // Worked successfully

        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
    }
}

Output for the above code is as below:

/*Output for "Get total connections present in the list"*/
totalConnections: 2

/*Output for "Check if required connection is present or not"*/
variantInst2.toString(): -1

/*Output for "Check if desired connection in the list is selected or not"*/
variantInst4.toString(): 1

/*Output for "Get text of required connection"*/
variantInst3.toString(): 
variantInst3.getString(): 

1) (Output 1) I got correct output i.e. 2 for "Get total connections present in the list".
2) (Output 2) Now for checking if desired connection is present in list or not, I always get output as -1. Even if I give correct connection name, I get output as -1 only.
Can anybody please tell me that what am I doing wrong here?
3) (Output 3) To check if desired connection in the list is selected or not, I always get output as 1. Here even if I give wrong connection name, I get output as 1 only i.e. desired connection is selected, which is wrong.
Can anybody please tell me that what am I doing wrong here?
4) (Output 4) Now for getting required text of a connection, I always get empty string as output.
Can anybody please tell me that what am I doing wrong here?

pranavs
  • 21
  • 2

0 Answers0