Can't get tab title from WPF TabControl when using custom TabItem template
In my WPF application, I'm using a TabControl with a custom style for the TabItems (the buttons you click to switch tabs). When I use this custom style, I'm unable to access the TextBox element to test what text the TabItem is displaying.
For example, here is the XAML code for my TabControl:
<TabControl TabStripPlacement="Left" Background="{x:Null}" ItemsSource="{Binding Tabs}" SelectedIndex="0">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Text="{Binding Key}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Value}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
When I run my app and use the Inspect tool, I can view the TextBlock inside the TabItem. But when I try to get this element through the Appium test framework, it doesn't exist.
I thought anything visible through Inspect should be available to test, so I guess this is a bug. I have a test project to demonstrate, if you want to see it let me know how/where to upload it.
Hi @Frakur,
Sure - we'd love to take look at your test project. You can even zip it up and attach it here.
Hi @hassanuz, sorry for the delay - here it is.
Details
First rebuild the solution so that Nuget goes and downloads the packages.
The first project, WpfAppiumTest is a simple WPF application with a TabControl that has two tabs. If you just run this project and use the Inspect tool, you'll see the window contains two TabItems, and each TabItem contains a TextBlock with the name of the tab. (The currently selected TabItem also contains the view being displayed).
The second project is the unit test project. Open the UnitTest1.cs file in the unit test project, and run the TestSecondTab test. It tries to get a TabItem that contains a child element with the name "Tab Two", but fails. Given what you can see in the Inspect tool, there's no reason this should fail.
I've also included a test method called ListAllChildrenOfTabItems which is supposed to print out details of all the children of the TabItems. You'll notice the list of children here is not the same as you can see in the Inspect tool - those TextBlock items containing the tab's name are missing. (The list of the children of each TabItem also contains the TabItem itself - I think that's just a result of my inaccurate xpath query so you can probably ignore that).
Hope that helps.
Is this issue is resolved?
We're having a similar issue. If I use the driver to try to get one of the objects based on the style attached, we only get back the Images, not the TextBlocks.
To examine the collection within the item, I'm pulling the objects from the element like: driver.find_elements_by_xpath(".//*")
Obviously I'd much prefer to pull only the TextBlock items.
When I pull the whole collection from within the element, as above, I get back the element itself, plus the Images. The TextBlocks don't show up at all. Something about the style wrapper seems to shield them.
Issue is with the custom tabcontrol style. Try to set PART_SelectedContentHost as content presenter name. Inspect tool inspects the tabitem content only if it is named as PART_SelectedContentHost.
ContentPresenter x:Name="PART_SelectedContentHost"
Hi @Venkattkvl. Did you try this fix in my example project? I just did, and I don't see any difference. You mention this is a fix for the Inspect tool, but I had no issues with the Inspect tool - the TabItem content shows up there just fine. My issue is being able to run unit tests on that content using the UI testing framework.
@craigbrown Did you solve the problem? If yes, please give here answer, i need it too.
@noob001 No, sorry. I provided the developers with an example project over a year ago but it seems like no one has looked at it. Our solution was to give up on using Appium for testing.
@craigbrown this is blocking us as well. Really wanted this architecture (WinAppDriver - Selenium in xUnit tests) to work but I can't get past this issue. Exact same problems for me. I've tried two different versions of WinAppDriver but it just won't see anything inside my derived button control, which gets its content from a style which sets the ContentTemplate (with textblocks and stuff that I can't see). Everything is seen in inspect just fine. I've tried reloading the driver (switching back to the running window in a new session), I've tried reloading the button, I've tried hovering the mouse over the button before reloading it (FindElement). I also wrote a customer AutomationPeer to make sure the children are added to the list returned by GetChildrenCore (they are and the GetChildrenCore is being called) but the return from the GetElements on the custom button just returns the single Element for the custom button, none of those children.
@donbymathieu we had this problem too. The first ListBox>>ListItem>>ComboBox, is fine, but the subsequent ListBox>>ListItem>>ComboBox's are seen as ListBox>>ListItem. Fixed by some creative code that is product dependent. It would be nice if all elements that show up in inspect.exe would work in WinAppDriver, but there are a few that don't.
@craigbrown this is blocking us as well. Really wanted this architecture (WinAppDriver - Selenium in xUnit tests) to work but I can't get past this issue. Exact same problems for me. I've tried two different versions of WinAppDriver but it just won't see anything inside my derived button control, which gets its content from a style which sets the ContentTemplate (with textblocks and stuff that I can't see). Everything is seen in inspect just fine. I've tried reloading the driver (switching back to the running window in a new session), I've tried reloading the button, I've tried hovering the mouse over the button before reloading it (FindElement). I also wrote a customer AutomationPeer to make sure the children are added to the list returned by GetChildrenCore (they are and the GetChildrenCore is being called) but the return from the GetElements on the custom button just returns the single Element for the custom button, none of those children.
I'm attaching a sample wpf project that has a single button with a style. You can see the textblock that says ON or OFF in inspect, but can't see it within the button elements in WinAppDriver. This is slightly different than your issues but I'm guessing they are related. I attached this project in another issue specifically about buttons as well winappdriverbug.zip .