How to access the WPF custom control's elements?
The problem
Our custom control is designed in drawing architecture. i.e. every elements in a control are drawn using DeviceContext's Draw methods. We are not arranging elements as UI elements. To provide the accessibility support, we have provided the AutomationPeer support for control (not inner elements).
Can you please let us know how we can provide the testing support for inner elements to read using Appium?
We have tried the followings,
AutomationElement datagrid = desktop.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "gridControl"));
if (datagrid == null)
Assert.Fail("GridControl is null");
else
{
var pattern = datagrid1.GetCurrentPattern(GridPatternIdentifiers.Pattern) as GridPattern;
var item = pattern.GetItem(1, 1);
if (item != null)
{
object value = null;
item.TryGetCurrentPattern(ValuePattern.Pattern, out value);
var valueProvider = value as ValuePattern;
var currentValue = valueProvider.Current.Value;
}
}
AutomationElement class provides support to access the inner elements.
While trying to access the WPF custom control, i m getting the following exception,
WindowsDriver<WindowsElement> session;
var appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability("app", "ApplicationPath");
session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(5000);
WindowsElement element = session.FindElementByClassName("GridControl");
session.Dispose();
Exception details,
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Appium.AppiumDriver`1.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Appium.AppiumDriver`1.FindElementByClassName(String className)
at UnitTestProject1.UnitTest1.TestMethod1() in GridControlDemo\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 37
Result Message:
Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
OpenQA.Selenium.WebDriverException: An element could not be located on the page using the given search parameters.
As like the above methods, can we get any solution to read the inner elements?
Environment
- WPF platform
Hi @NeelakandanKannan,
For your WPF custom control, you're using
WindowsElement element = session.FindElementByClassName("GridControl");
But "GridControl" is in camel case on the AutomationElement class, and you are using Automation ID to access it. So we would recommend to try the following:
WindowsElement element = session.FindElementByAccessibilityId("gridControl");
If that doesn't work, can you also provide the pagesource and a snapshot of Inspect.exe?
Thanks
Hi @hassanuz ,
Thanks for the update.
We have checked with the accessibility id an its now working for this control. But, the same is not working for another control. Please find the requested details for that control,
pagesource

Snapshot of inspect.exe
We have set the automation id in application level, <local: GridDataControl x:Name="gridDataControl" AutomationProperties.AutomationId="gridDataControl" ItemsSource="{Binding Source}" NotifyPropertyChanges="True" AutoPopulateColumns="False" AutoPopulateRelations="False">
Also, we have the requirement to access the GetItem method to access inner cell level like AutomationElement. Is it possible to expose any custom methods to provide the support to perform our operation in Appium? var pattern = datagrid1.GetCurrentPattern(GridPatternIdentifiers.Pattern) as GridPattern; var item = pattern.GetItem(1, 1); if (item != null) { object value = null; item.TryGetCurrentPattern(ValuePattern.Pattern, out value); var valueProvider = value as ValuePattern; var currentValue = valueProvider.Current.Value;
Hi @NeelakandanKannan, Please take a look at the workaround suggested on #629.