microsoft / WinAppDriver

Windows Application Driver
MIT License
3.7k stars 1.41k forks source link

Unable to query NavigationViewItem in the "More" menu #1204

Open mrlacey opened 4 years ago

mrlacey commented 4 years ago

When using a WinUI NavigationView in a UWP app with the horizontal mode set to "Horizontal" and more items than can be displayed at once, it's not possible to query the entries displayed in the "Pop-up" that is displayed after clicking the "More" button. I can access the items in the list as a collection, but not click on them or query any of their properties. When I try, I get this response:

An element command failed because the referenced element is no longer attached to the DOM.

I'm using this code:

var popupMenu = appSession.FindElementByName("Pop-up");

var moreMenuItemsCount = popupMenu.FindElementsByClassName("Microsoft.UI.Xaml.Controls.NavigationViewItem").Count;
moreBtn.Click(); // Close menu so get in a consistent state

for (int i = 0; i < moreMenuItemsCount; i++)
{
    moreBtn.Click();

    // Find the pop-up again as it's recreated after opening the menu each time
    popupMenu = appSession.FindElementByName("Pop-up");

    var moreMenuItems = popupMenu.FindElementsByClassName("Microsoft.UI.Xaml.Controls.NavigationViewItem");

    // BOTH/EITHER OF THE FOLLOWING FAIL!
    var entryText = moreMenuItems[i].Text;
    moreMenuItems[i].Click();

    ....  // other code omitted
}

I have similar code that looks at the contents of a Menu pop-up and that works fine.

It's also possible to query popupMenu (in the above code) multiple times and get responses, it's only when I try and query the items that are returned that there is an issue.

The following are the details of an example request. I get the list of elements and then immediately request the "Text" property of the first element ("42.3017434.4.45") but that fails. Nothing else has happened

==========================================

POST /session/E716FB0E-522A-4BF3-B8AD-541039921CFE/element/42.3017434.4.41/elements HTTP/1.1
Accept: application/json, image/png
Content-Length: 78
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"class name","value":"Microsoft.UI.Xaml.Controls.NavigationViewItem"}
HTTP/1.1 200 OK
Content-Length: 193
Content-Type: application/json

{"sessionId":"E716FB0E-522A-4BF3-B8AD-541039921CFE","status":0,"value":[{"ELEMENT":"42.3017434.4.45"},{"ELEMENT":"42.3017434.4.47"},{"ELEMENT":"42.3017434.4.49"},{"ELEMENT":"42.3017434.4.51"}]}

==========================================
GET /session/E716FB0E-522A-4BF3-B8AD-541039921CFE/element/42.3017434.4.45/text HTTP/1.1
Accept: application/json, image/png
Host: 127.0.0.1:4723

HTTP/1.1 400 Bad Request
Content-Length: 160
Content-Type: application/json

{"status":10,"value":{"error":"stale element reference","message":"An element command failed because the referenced element is no longer attached to the DOM."}}

Acknowledging that this may be an issue with the control, I'm cross-posting this in the WinUI repository: https://github.com/microsoft/microsoft-ui-xaml/issues/2736

anunay1 commented 4 years ago

You are getting an stale element exception that means the element is no longer part of the DOM i.e. you application is refreshing. You application is refreshing. Can you do a page source after

var moreMenuItems = popupMenu.FindElementsByClassName("Microsoft.UI.Xaml.Controls.NavigationViewItem"); You can do the page source using below:

var pagesource = driver.PageSource; File.WriteAllText("@C:\PageSource.xml",pagesource);

mrlacey commented 4 years ago

The PageSource shows the extra Window (below) before trying to query the items. I don't see anything obvious that could be an issue but I'm not really sure what I'm looking for.

However, based on comments on the other issue, this appears to be a problem with the control, not something with WinAppDriver.

    <Window AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Xaml_WindowedPopupClass" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="window" Name="Pop-upHost" Orientation="None" ProcessId="19344" RuntimeId="42.265484" x="2713" y="144" width="220" height="356">
        <Window AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Popup" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="window" Name="Pop-up" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.41" x="0" y="0" width="0" height="0" CanMaximize="False" CanMinimize="False" IsModal="True" WindowVisualState="Normal" WindowInteractionState="Running" IsTopmost="True" IsAvailable="True">
            <Pane AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Flyout" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="pane" Name="" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.42" x="2713" y="144" width="220" height="356">
                <Pane AcceleratorKey="" AccessKey="" AutomationId="" ClassName="ScrollViewer" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="pane" Name="" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.43" x="2715" y="162" width="216" height="320" HorizontallyScrollable="False" VerticallyScrollable="False" HorizontalScrollPercent="-1" VerticalScrollPercent="-1" HorizontalViewSize="100" VerticalViewSize="100" IsAvailable="True">
                    <Group AcceleratorKey="" AccessKey="" AutomationId="TopNavMenuItemsOverflowHost" ClassName="" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="group" Name="" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.44" x="2715" y="162" width="216" height="320">
                        <TabItem AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Microsoft.UI.Xaml.Controls.NavigationViewItem" FrameworkId="XAML" HasKeyboardFocus="True" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="tab item" Name="Camera" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.45" x="2715" y="162" width="216" height="80" ExpandCollapseState="Collapsed" IsSelected="False" SelectionContainer="{, , 42.68912.4.1}" IsAvailable="True"/>
                        <TabItem AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Microsoft.UI.Xaml.Controls.NavigationViewItem" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="tab item" Name="WebView" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.47" x="2715" y="242" width="216" height="80" ExpandCollapseState="Collapsed" IsSelected="False" SelectionContainer="{, , 42.68912.4.1}" IsAvailable="True"/>
                        <TabItem AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Microsoft.UI.Xaml.Controls.NavigationViewItem" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="tab item" Name="MediaPlayer" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.49" x="2715" y="322" width="216" height="80" ExpandCollapseState="Collapsed" IsSelected="False" SelectionContainer="{, , 42.68912.4.1}" IsAvailable="True"/>
                        <TabItem AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Microsoft.UI.Xaml.Controls.NavigationViewItem" FrameworkId="XAML" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="tab item" Name="Map" Orientation="None" ProcessId="19344" RuntimeId="42.265484.4.51" x="2715" y="402" width="216" height="80" ExpandCollapseState="Collapsed" IsSelected="False" SelectionContainer="{, , 42.68912.4.1}" IsAvailable="True"/>
                    </Group>
                </Pane>
            </Pane>
        </Window>
    </Window>