microsoft / WinAppDriver

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

WinAppDriver - Application crashing frequently #1274

Open rupalibyte opened 4 years ago

rupalibyte commented 4 years ago

I have created small program using Visual Studio, C# and Appium. Program is enter few values in the desktop application and click on "Submit" button. Using "SendKeys" to enter values in windows application. It seems SendKeys is sending values to wrong objects and my application is crashing multiple times and not able to complete the test.

anunay1 commented 4 years ago

Can you share the code? And also what is shown in inspect.exe?

rupalibyte commented 4 years ago

Can you share the code? And also what is shown in inspect.exe?

Here is code.... Application crashes at different places every time. WinAppDriver.ext log is also attached.

private static void POC()
{
    if (driver == null)
    {
        var appiumOptions = new AppiumOptions();
        appiumOptions.AddAdditionalCapability("platormName", "Windows");
        appiumOptions.AddAdditionalCapability("app", WpfAppId);
        appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
        DesktopSession = null;
        try
        {
            Console.WriteLine("Trying to Launch App");
            driver = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
        }
        catch (Exception e) { }

        //1. Creating a Desktop session
        var desktopCapabilities = new AppiumOptions();
        desktopCapabilities.AddAdditionalCapability("platormName", "Windows");
        desktopCapabilities.AddAdditionalCapability("app", "Root");
        desktopCapabilities.AddAdditionalCapability("deviceName", "WindowsPC");
        DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities);

        while (IsElementPresent("Finish") == false)
            Task.WaitAll(Task.Delay(1000));
        DesktopSession.FindElement(By.Name("Finish")).Click();
        //Thread.Sleep(20000);
        Task.WaitAll(Task.Delay(20000));
        if (IsElementPresent("No") == true)
            DesktopSession.FindElement(By.Name("No")).Click();
        if (IsElementPresent("OK") == true)
            DesktopSession.FindElement(By.Name("OK")).Click();
        while (!DesktopSession.FindElementByAccessibilityId("1001").Enabled)
            Task.WaitAll(Task.Delay(1000));
       // DesktopSession.SwitchTo().ActiveElement();
        DesktopSession.FindElementByAccessibilityId("1001").SendKeys("Test Company x" );
        Task.WaitAll(Task.Delay(1000));
        DesktopSession.FindElementByAccessibilityId("13105").SendKeys("Test Attn");
        Task.WaitAll(Task.Delay(1000));
        DesktopSession.FindElementByAccessibilityId("13107").SendKeys("Address1");
        Task.WaitAll(Task.Delay(1000));
        DesktopSession.FindElementByAccessibilityId("13112").SendKeys("xxxxx");
     //   Task.WaitAll(Task.Delay(2000));
        //DesktopSession.FindElementByAccessibilityId("13112").SendKeys(Keys.Tab);
        Task.WaitAll(Task.Delay(2000));

       // DesktopSession.FindElementByAccessibilityId("13079").SendKeys(Keys.Backspace);
        DesktopSession.FindElementByAccessibilityId("13079").SendKeys("1");
        Task.WaitAll(Task.Delay(3000));
        DesktopSession.FindElementByAccessibilityId("1001").SendKeys(Keys.F10);
   //     Task.WaitAll(Task.Delay(15000));

        while (DesktopSession.FindElementByAccessibilityId("13117").Enabled)
            Task.WaitAll(Task.Delay(1000));
        Task.WaitAll(Task.Delay(3000));
        DesktopSession.FindElementByAccessibilityId("1001").SendKeys(Keys.F3);
        Task.WaitAll(Task.Delay(2000));
        DesktopSession.FindElementByAccessibilityId("16178").Click();
        Task.WaitAll(Task.Delay(2000));
        DesktopSession.FindElementByAccessibilityId("1001").SendKeys("Test Company x");
        Task.WaitAll(Task.Delay(2000));
        DesktopSession.FindElementByAccessibilityId("13775").SendKeys(Keys.Enter);
        Task.WaitAll(Task.Delay(2000));
        TearDown();
    }

WinAppDriverexe.txt

anunay1 commented 4 years ago

Is there a specific reason that you are using desktop session for finding the element, there could be multiple items with accessibility id 1001, I would suggest that you create a new driver session with the top window of your application, and inspite of using Task.wait use implicit wait.

rupalibyte commented 4 years ago

Is there a specific reason that you are using desktop session for finding the element, there could be multiple items with accessibility id 1001, I would suggest that you create a new driver session with the top window of your application, and inspite of using Task.wait use implicit wait.

I am new to WinAppDriver, do you have any sample code to create new driver session? also example of implicit wait until object is enabled? any reference link is also fine. I searched a lot but not able to find anything different than whatever i developed so far.

anunay1 commented 4 years ago

You can check the below link https://github.com/microsoft/WinAppDriver/wiki/Frequently-Asked-Questions

The driver by default searches for the element for 60 seconds. If you need to wait more then you can use web driver wait

anunay1 commented 4 years ago

Additionaly you can clone this repo and check the samples

rupalibyte commented 4 years ago

I used driver session initially in Visual Studio + C#. But getting warning message 'DesiredCapabilities' is obsolete : 'Use of DesiredCapabilities has been deprecated in favor of browser-specific Options Classes'

Capture

anunay1 commented 4 years ago

With the latest appium you need to use AppiumOptions

anunay1 commented 4 years ago

Not nudget package instead of desired capabilities you need to use AppiumOptions

anunay1 commented 4 years ago

AppiumOptions capabilities = new AppiumOptions()

rupalibyte commented 4 years ago

With the latest appium you need to use AppiumOptions

If you refer my original source code, i have used AppiumOptions only.

anunay1 commented 4 years ago

OK. Then the logic is same to grab the top level window and create a session