Interaction with a pop-up window when parent window is the owner
My WPF application has several pop-up windows that I use WAD to navigate through. For a subset of windows, when we call them, we assign ownership to their parent window like so:
public void DisplayWindow()
{
_newWindow = new Window()
{
Owner = this
};
_newWindow.Show();
}
Normally I use the driver's .WindowHandles to get all open windows and switch focus to the newest pop-up.
However, when ownership is assigned to the parent window as above, .WindowHandles count only shows 1 window handle. Note that using Microsoft's inspect tool, I see a unique window handle for the new window (same process ID), even when I'm not getting it from the driver. If I take that ownership code out, WAD gets both window handles just fine (not a real option in my case unfortunately.)
So now I have no way to interact with this new window.
I've found that I can use WAD's PageSource property to read through the the WPF code for the new window, getting some info about the current state. But it is extremely long, and very difficult to sort through, and of course I cannot use that to actually interact with the new window.
Any ideas resolving this issue would be much appreciated.
try using desktop session
I am using a desktop session to start the app. The issue isn't that I can't interact with any of the application windows, only the ones that popped up that are explicitly owned by their parent window. For some reason WAD no longer sees the new window handle when it comes up, although though the Inspect tool can see it.
if the pop up is coming out of the same window then you do not need to switch to that window, directly using session.findelement any option to click. In case there are multiple elements of the same name create a list and then try to click, the count starts from the parent window. i.e if "OK" is there on both the parent window and the pop up window, the "OK" element will have index as 0 for the parent window and "OK" on pop up will have index 1.
That doesn't seem to be the case. I'll list my steps more clearly.
I start my app. Now WAD's WindowHandles shows a count of 1, 0x44028C. Inspect.exe shows NativeWindowHandle: 0x44028C.
I open a normal (not owned) window in my app from the first window. Now WAD's WindowHandles shows a count of 2, 0x44028C, and 0x000904CA. For the new window Inspect.exe shows NativeWindowHandle: 0x000904CA. All is well.
I now open a slave window of the second window. By slave, I mean when it is called I use { Owner = this } There are 3 windows open now. WAD WindowHandles still shows a count of 2, same handles. But using Inspect, it shows NativeWindowHandle: 0x381730, a separate window handle.
At this point I use FindElementByAutoID("tvFeedWindow"). I get an Element Not Found exception. But using Inspect, again, I see AutomationId: "tvFeedWindow" where I expect it to be. So Inspect sees it. But WAD does not, because I am not using the correct window handle, which is hidden from WAD somehow.
If I take out that "ownership code", it works fine. Unfortunately that is not an option for me in production.
There may not be a resolution, and I may be stuck trying to just parse through the source code to see the window's current state, left unable to interact with the slave window. Just checking.
I ignore handles and try to access things by name. It probably executes slowly but it works. In C# (implicit timeout set to 3 seconds)
I haven't figured out how WinAppDriver polls for window status yet, so I run loops with timeouts. The purpose of the timeout is to allow for any application computation delays between the test event which triggers the popup, and the actual popup display
Checking the control type is imperative. You never know what you may find. (say the label of a command button on the window which is used to activate the window you are trying to locate)
public static OpenQA.Selenium.Appium.AppiumWebElement GetRootWindow(string windowName, Int32 intTimeoutInSeconds) { const string NoSuchElement = "Currently selected window has been closed"; const string ElementNotFound = "An element could not be located on the page using the given search parameters."; const string WebAbort = "The request was aborted: The operation has timed out."; const string WebTimeout = "The operation has timed out"; const string NotAtttachedToDOM = "An element command failed because the referenced element is no longer attached to the DOM.";
Int32 intRetryCount;
Int32 intRetryLimit;
OpenQA.Selenium.Appium.AppiumWebElement foundElement;
string strControlType;
intRetryCount = 0;
intRetryLimit = (intTimeoutInSeconds + 2) / 3;
foundElement = null;
while ((intRetryCount < intRetryLimit) && (foundElement == null))
{
try
{
foundElement = rootSession.FindElementByName(windowName) as WindowsElement;
strControlType = foundElement.TagName;
if (strControlType != "ControlType.Window")
{
foundElement = null;
intRetryCount = intRetryCount + 1;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1.0));
}
}
catch (InvalidOperationException exception)
{
if (exception.Message == NoSuchElement)
{
intRetryCount = intRetryCount + 1;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1.0));
}
else if (exception.Message == ElementNotFound)
{
intRetryCount = intRetryCount + 1;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1.0));
}
else if (exception.Message == NotAtttachedToDOM)
{
throw;
}
else
{
Assert.Fail(exception.Message);
}
}
catch (OpenQA.Selenium.WebDriverException exception)
{
if (exception.InnerException.Message == WebTimeout)
{
intRetryCount = intRetryCount + 1;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1.0));
}
else if (exception.InnerException.Message == WebAbort)
{
intRetryCount = intRetryCount + 1;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1.0));
}
else
{
Assert.Fail(exception.Message);
}
}
catch (Exception ex )
{
Assert.Fail(ex.Message);
}
}
return (foundElement);
}
public static OpenQA.Selenium.Appium.AppiumWebElement FindAnchoredPopupWindow(string parentwindowName, string popupName, Int32 intDelayInSeconds) { Int32 intTimeout; OpenQA.Selenium.Appium.AppiumWebElement buttonElement; OpenQA.Selenium.Appium.AppiumWebElement CreateNewProjectWindow; Int32 loopCount; Int32 loopLimit;
intTimeout = intDelayInSeconds + 10;
CreateNewProjectWindow = GetRootWindow(parentwindowName, intTimeout);
Assert.IsNotNull(CreateNewProjectWindow);
loopCount = 0;
loopLimit = (intDelayInSeconds + 2) / 3;
buttonElement = null;
while ((buttonElement == null) && (loopCount < loopLimit))
{
try
{
buttonElement = CreateNewProjectWindow.FindElementByName(popupName) as WindowsElement;
}
catch
{
loopCount = loopCount + 1;
buttonElement = null;
}
}
return (buttonElement);
}
Check this out https://www.evoketechnologies.com/blog/how-to-handle-windows-based-popups-using-winappdriver-in-selenium-webdriver/
session.FindElementByXPath("//Button[@Name='Cancel'][@AutomationId='2']").Click(); This works for me.