5

My simple WPF application includes a check box. I am trying to test this application automatically with the Windwos Application Driver. After creating a session the check box is clicked. Afterwards I want to verify if the checkbox is checked. But the type of the check box object is WindowsElement so my code can not work.

In other words: How do I get access to these control types in a WinAppDriver test?

https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-controltypesoverview

<CheckBox AutomationProperties.AutomationId="CheckBox1"/>
[Test Method]
public void TestMethod1()
{
            const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
            const string SimpleWPFTestID = @"C:\Users\bla\source\repos\SimpleWPFApp\SimpleWPFApp\bin\Debug\SimpleWPFApp.exe";

            DesiredCapabilities appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", SimpleWPFTestID);

            var session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

            WindowsElement CheckBox1 = session.FindElementByAccessibilityId("CheckBox1");
            CheckBox1.Click();
            Assert.IsTrue(CheckBox1.IsChecked); // this line does not work :-(
}
  • Does validation on `CheckBox1.IsChecked` work? – Ginger Ninja Aug 12 '19 at 17:35
  • No, that is exactly the part of the line that fails. – Susan Arnold Aug 12 '19 at 19:37
  • Please try this `session.FindElementByAccessibilityId("CheckBox1").Click(); Assert.IsTrue(CheckBox1.Checked);` or `session.FindElementByAccessibilityId("CheckBox1").IsChecked = true; Assert.IsTrue(CheckBox1.IsChecked);` – Trevor Aug 12 '19 at 20:02
  • I more meant: to try `IsChecked` instead of `Checked`. I believe `Checked` is an Event not a property – Ginger Ninja Aug 12 '19 at 20:12
  • You are right: I should have written 'IsChecked'. But this does not solve my problem. Error: "'WindowsElement' does not contain a definition for 'IsChecked' and no accessible extension method 'IsChecked' accepting a first argument of type 'WindowsElement' could be found (are you missing a using directive or an assembly reference?)" – Susan Arnold Aug 13 '19 at 06:40
  • Also `session.FindElementByAccessibilityId("CheckBox1").IsChecked = true;` does not work (for the same reason). – Susan Arnold Aug 13 '19 at 06:46
  • Your problem seems to be similar to [this one](https://github.com/Microsoft/WinAppDriver/issues/198) on github. [The solution](https://github.com/microsoft/WinAppDriver/blob/master/Tests/UWPControls/CheckBox.cs) is linked in the answer from timotiusmargo. – PixelPlex Aug 13 '19 at 12:55

1 Answers1

2

Thank you very much, PixelPlex! That is the answer. I also had a look at this answer. Now it works :-)

Assert.IsTrue(CheckBox1.Selected);