0

I am trying to make an automated test using C#/NUnit/WinAppDriver where I open the Task Scheduler and find a specific folder in the Task Scheduler Library. When I run the test, Task Scheduler opens, but then I get the following Exception:

OpenQA.Selenium.WebDriverException : Failed to locate opened application window with appId: C:\WINDOWS\System32\taskschd.msc, and processId: 3796

(NOTE: the processId seems to be random every time)

Here is the code that I am using to try this test:

using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;

namespace AutomationProject
{
    [TestFixture]
    public class ScenarioTaskSchedular
    {
        public static WindowsDriver<WindowsElement> DesktopSession;
        private const string TaskSchedularId = @"C:\\WINDOWS\\System32\\taskschd.msc";
        [Test]
        public void FindWFolder()
        {
            var TaskSchedularLibrary = DesktopSession.FindElementByName("Task Scheduler Library");
            var TreeItems = TaskSchedularLibrary.FindElementsByXPath("//[@LocalizedControlType=\"tree item\"]");
            foreach (var element in TreeItems)
            {
                Console.WriteLine(element.GetAttribute("Value.Value")); //To check each folder in the task schedular library
            }
        }

        [SetUp]
        public static void SetUp()
        {
            AppiumOptions options = new AppiumOptions();
            options.AddAdditionalCapability("app", TaskSchedularId);
            options.AddAdditionalCapability("deviceName", "WindowsPC");
            DesktopSession = new WindowsDriver<WindowsElement>(new Uri(DriverUrl), options);
            Assert.IsNotNull(DesktopSession);
        }

        [TearDown]
        public static void TearDown()
        {
           DesktopSession.Quit();
        }
    }
}

I'm not really sure why this is happening, and I could not find a solution anywhere for this issue. Any help would be greatly appreciated.

ronoldo
  • 11
  • 3

1 Answers1

0

The id is generally a int value from a Process object, the problem is you are passing the Id property as a string that's not the actual Id you need, and winappdriver doesn't have a support for that. For that job, you gonna need to do something like below, according to this documentation.

var TaskSchedularWindow = DesktopSession.FindElementByName("Task Schedular");
var TaskSchedularWindowHandle = TaskSchedularWindow .GetAttribute("NativeWindowHandle");
TaskSchedularWindowHandle = (int.Parse(TaskSchedularWindowHandle)).ToString("x"); // Convert to Hex

// Create session by attaching to TaskSchedulartop level window
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", TaskSchedularWindowHandle);
TaskSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
ojonasplima
  • 411
  • 2
  • 12