Skip to content

Java WinAppDriver: Application does not have NativeWindowHandle property, how to attach using process name? #1242

@anukulsinghal29

Description

@anukulsinghal29

Hi,

I am using WinAppDriver release 1.2 on a Classic Windows application using Java. Since my Windows application has a splash screen, I wanted to add the Attach to existing Window functionality so I could start building my test scripts on the attached window more rapidly.

Here is the application login window details on using inspect.exe

How found:	Mouse move (813,120)
	hwnd=**0x000000000023187E** 32bit class="#32770" style=0xFFFFFFFF94C008C4 ex=0x10101
ControlType:	UIA_TitleBarControlTypeId (0xC375)
LocalizedControlType:	"title bar"
BoundingRectangle:	{l:661 t:108 r:1249 b:143}
IsEnabled:	true
IsOffscreen:	false
IsKeyboardFocusable:	true
HasKeyboardFocus:	false
ProcessId:	32356
RuntimeId:	[2A.23187E.3.80000001.23187E.FFFFFFFE.0]
AutomationId:	"TitleBar"
IsContentElement:	false
ProviderDescription:	"[pid:43300,providerId:0x0 Annotation:Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main(parent link):Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]"
IsPassword:	false
IsDialog:	[Not supported]
LegacyIAccessible.ChildId:	0
LegacyIAccessible.Description:	"Displays the name of the window and contains controls to manipulate it"
LegacyIAccessible.Role:	title bar (0x1)
LegacyIAccessible.State:	focusable (0x100000)
LegacyIAccessible.Value:	"Flight Explorer logon"
Value.IsReadOnly:	false
Value.Value:	"Flight Explorer logon"
IsAnnotationPatternAvailable:	false
IsDragPatternAvailable:	false
IsDockPatternAvailable:	false
IsDropTargetPatternAvailable:	false
IsExpandCollapsePatternAvailable:	false
IsGridItemPatternAvailable:	false
IsGridPatternAvailable:	false
IsInvokePatternAvailable:	false
IsItemContainerPatternAvailable:	false
IsLegacyIAccessiblePatternAvailable:	true
IsMultipleViewPatternAvailable:	false
IsObjectModelPatternAvailable:	false
IsRangeValuePatternAvailable:	false
IsScrollItemPatternAvailable:	false
IsScrollPatternAvailable:	false
IsSelectionItemPatternAvailable:	false
IsSelectionPatternAvailable:	false
IsSpreadsheetItemPatternAvailable:	false
IsSpreadsheetPatternAvailable:	false
IsStylesPatternAvailable:	false
IsSynchronizedInputPatternAvailable:	false
IsTableItemPatternAvailable:	false
IsTablePatternAvailable:	false
IsTextChildPatternAvailable:	false
IsTextEditPatternAvailable:	false
IsTextPatternAvailable:	false
IsTextPattern2Available:	false
IsTogglePatternAvailable:	false
IsTransformPatternAvailable:	false
IsTransform2PatternAvailable:	false
IsValuePatternAvailable:	true
IsVirtualizedItemPatternAvailable:	false
IsWindowPatternAvailable:	false
IsCustomNavigationPatternAvailable:	false
IsSelectionPattern2Available:	false
FirstChild:	[null]
LastChild:	[null]
Next:	[null]
Previous:	"" document
Other Props:	Object has no additional properties
Children:	Container has no children
Ancestors:	"Flight Explorer logon" dialog
	"Desktop 1" pane
	[ No Parent ]

My problem is this with the code I am writing below to attach to this:

DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.setCapability("platformName", "Windows");
desktopCapabilities.setCapability("app", "Root");
desktopCapabilities.setCapability("deviceName", "WindowsPC");
desktopCapabilities.setCapability("ms:experimental-webdriver", true);
WindowsDriver<WindowsElement> windowsDriver = new WindowsDriver<WindowsElement>(new URL("http://127.0.0.1:4723"), desktopCapabilities);
WebElement appWindow = windowsDriver.findElementByXPath("//Window[@Name='logon'][@ClassName='#32770']/TitleBar[@AutomationId='TitleBar']");
String appWindowHandle = appWindow.getAttribute("NativeWindowHandle");

Since the inspect does not have NativeWindowHandle, I am getting the value of this as 0. Hence, this does not allow me to attach to my application. This is the header of that Window, so if I try to use some other Windows Element in that login page, even though it provides some Native Window Handle, it is not the top level window, and hence the converted Hex based Window Handle does not work out for any other element when used instead of the Title Bar element.

However, I tried one thing, I directly copied the hwnd from inspect output above - 0x000000000023187E, and used that hardcoded hwnd to attach. Then it worked fine:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "WindowsPC");
capabilities.setCapability("appTopLevelWindow", "**0x000000000023187E**");
windowsDriver = new WindowsDriver<WindowsElement>(new URL("http://127.0.0.1:4723"), capabilities);
windowsDriver.findElement(By.name("Connect")).click();

But I am not able to figure out how I can get the value of this hwnd property since NativeWindowHandle of my top level window is not defined in inspect, and always returns a 0 when accessed through code.

Other thing I noted in inspect.exe is "ProcessId". I noted that "ProcessId" property is the same for all inspected elements, so I thought there must be some way to figure out the window handle from process id in Java. But I am unable to find one.

Have the following questions:

  1. Is there a way to access hwnd property through Java for my element (as shown in the How Found section of inspect.exe), and use that to attach each time my test runs?
  2. If the above is not possible, is there a way in Java that I could use the ProcessId I grabbed from inspect using getAttribute, and then reverse look up the Window Handle or hwnd based on fetched ProcessId?
  3. Any other way you could recommend for attaching when the top level window does not have a NativeWindowHandle property in inspect?

Anything which works should help.

Any help would be greatly appreciated on this. Please let me know if there is any information needed from my side.

Thanks,
Anukul

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions