none
UI automation RRS feed

  • Question

  • Hello,

    I am new to UI automation. I have a open source UI automation tool which I want to enhance for my QA team.

    This tool is developed in C# using Microsoft UI automation APIs. 

    Now here is the problem:

    Suppose, I want to run below test cases with this tool:

    1. Maximize a windows desktop application let's say XYZ

    2. Open a file let's say file.txt in that windows application

    3. Close the application window

    Now, before running the test cases, for each of these test cases, I have to give control information.

    For example, for 1st and 3rd test case, I have to give control information like window title and for 2nd test case, I have to give control information of open button and send keystrokes of file.txt to open dialog box.

    I hope, you understood the flow so far.

    Now, I already set the same control information for 1st and 3rd test case. Let's say windows title for this moment was <application name>

    But when 2nd test case run, that means when file.txt opens in the application, window title changes.

    It becomes something like - <application name - file.txt> 

    Now, when third test runs, it does not find the window because window title has been changed in test case 2.

    So, I want the test case 3 anyhow detect the window. 

    How can I achieve this?

    Thank you in advance

    Best regards,

    Navnath

     

    Monday, January 22, 2018 5:06 PM

All replies

  • A window is usually identified by its class name or class name + title

    For example with FindWindow and Notepad class name :

    IntPtr hWndNotepad = FindWindow("Notepad", null);
    if (hWndNotepad != IntPtr.Zero)
    {
        MessageBox.Show(string.Format("Notepad Window handle = {0}", hWndNotepad.ToString("X")));
    }
    

    With :

    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
          public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


    Monday, January 22, 2018 5:30 PM
  • Hello,

    Thank you for the feedback.

    I could detect a window with the help of your code.

    However, it is not able to find the window in the below sequence of test cases:

    1. Open a new notepad window. Here, window will have title Untitled - Notepad

    2. Change window state - maximize

        For this test case, I have set the control information of the window with title Untitled - Notepad, hence it will maximize the window that is opened in test case 1

    3. Open a txt file in notepad that is opened in test case 1

    Now, at this time, title of window will be changed. It would be something like :

    file.txt - Notepad

    And it will create a problem for 4th test case

    4. Close the window. For this test case I have set the control information of new notepad window which always has title of Untitled - Notepad

    But this time, it will not find that window since window title has been changed.

    Now coming to my question, is there any way to detect a window using some regex match.

    For example, If I say .*Notepad.* then it should detect the window in 4th test case.

    Thank you in advance!

    Navnath

    Tuesday, January 23, 2018 5:08 PM
  • I don't understand very well your case, as FindWindow with class name ignores the title (if there is only 1 window opened)

    Otherwise, you can filter class name + title with EnumWindows  (GetClassName + GetWindowText) to find all windows

    Declarations :

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
    
    [DllImport("User32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowTextLength(IntPtr hWnd);
    
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
    
    [DllImport("User32.dll")]
    public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, ref IntPtr lParam);
    
    public delegate bool EnumWindowsProc(IntPtr hWnd, ref IntPtr lParam);l EnumWindowsProc(IntPtr hWnd, ref IntPtr lParam);

    Tuesday, January 23, 2018 5:18 PM