microsoft / WinAppDriver

Windows Application Driver
MIT License
3.69k stars 1.4k forks source link

Not able to select an item in the context menu after right clicking #1428

Open anileapen opened 3 years ago

anileapen commented 3 years ago

Hi I am having an issue with WinAppDriver in Visual Studio c#. I am trying to select an item in the context menu after right clicking an item. I am using the below code. I can see the context menu after right clicking but not able to select an option in the context menu. Is there any way I can give focus on the context menu or something to select the option [TestMethod] public void TestMethod() {

        var sites = session.FindElementByName("Sites");
        new Actions(session).DoubleClick(sites).Build().Perform();
         Actions builder = new Actions(session);
        builder.ContextClick(sites).Build().Perform();
        Thread.Sleep(2000);
         session.Mouse.MouseMove(cxProSession.FindElementByName("Site").Coordinates, 1968, 325);
        session.Mouse.ContextClick(null);

} Could someone please help ?

anunay1 commented 3 years ago

is the context menu part of the same UI tree in inspect.

anileapen commented 3 years ago

@anunay1 , This is how it looks like in inspect. When I right click the "Sites" tree item it bring the Context Menu in the application. But it doesn't allow to select menu item

image

shoaibmansoor commented 3 years ago

@anileapen When you start a session for a given application let's say 'abc.exe' then winappdriver cannot find any element outside the pane/context of the application. To interact with all elements outside of the application session, you need to use a desktop session , and in this case attach the context menu using native_window_handle Refer: https://github.com/microsoft/WinAppDriver/wiki/Frequently-Asked-Questions#when-and-how-to-attach-to-an-existing-app-window

anileapen commented 3 years ago

@shoaibmansoor . Thanks a lot for the details above. I am new to Win App Driver. Could you elaborate more on the solution above please?

This is my code below:

using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Windows; using System; using System.Diagnostics; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Appium.Service; using System.Threading; using OpenQA.Selenium.Interactions; using OpenQA.Selenium.Support.UI; using System.Configuration;

namespace ApplicationAuto { [TestClass] public class Application { static WindowsDriver session; public static string APPPATH = "C:\APPLICATIONPATH\"; public static string APP= APPPATH + "APP.exe"; private Configuration _conf = null;

    [ClassInitialize]
    public static void OpenAPP(TestContext testContext)
    {
        //  Debug.WriteLine("Hello ClassInitialize");
        var appiumLocalService = new AppiumServiceBuilder().UsingPort(4723).Build();
        appiumLocalService.Start();
        AppiumOptions app= new AppiumOptions();
        app.AddAdditionalCapability("ms:waitForAppLaunch", "10");
        app.AddAdditionalCapability("app", APP);
        session= new WindowsDriver<WindowsElement>(appiumLocalService, app);
        Thread.Sleep(2000);

    }

    [ClassCleanup]

    public static void CleanupAfterAllAPPTests()
    {

        if (session!= null)
        {
            session.Quit();
        }
    }

[TestMethod] public void ContextMenuSelection() { var sites = session.FindElementByName("Sites"); new Actions(session).DoubleClick(sites).Build().Perform(); Actions builder = new Actions(session); builder.ContextClick(sites).Build().Perform(); Thread.Sleep(2000); session.Mouse.MouseMove(session.FindElementByName("Site").Coordinates, 1968, 325); session.Mouse.ContextClick(null); }

I tried changing the Test to :

[TestMethod]

    public void ContextMenuSelection()

    {

        var sites = session.FindElementByName("Sites");

        new Actions(session).DoubleClick(sites).Build().Perform();

        //sites.Click();

        Actions builder = new Actions(session);

        builder.ContextClick(sites).Build().Perform();

        Thread.Sleep(2000);

        var appiumOptionsNew = new AppiumOptions();

        appiumOptionsNew.AddAdditionalCapability("app", "Root");

        WindowsDriver<WindowsElement> sessionDesktop = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptionsNew);

        var ContextItem = sessionDesktop.FindElementByName("Test");

        WebDriverWait waitForOption= new WebDriverWait(session, TimeSpan.FromSeconds(10));

        waitForOption.Until(pred => ContextItem .Displayed);

        ContextItem .Click();

This still didn't work. I can get the Context menu open but not able to Click the option. Could you tell me what need to be added to the above code for selecting the option in the Context Menu after a right click on the root element in the tree please?

Thanks Anil