appium / dotnet-client

Extension to the official Selenium dotnet webdriver
Apache License 2.0
381 stars 186 forks source link

appium crashes when testing a Windows application on virtual machine #759

Closed valinprogress closed 5 months ago

valinprogress commented 6 months ago

Description

I am trying to test with appium multiple Windows application installed on an Virtual Machine. I expect the window being active when I instantiate the WindowsDriver with IP Address of the virtual machine. When testing the application on my machine the UI Test is executed. On the virtual machine the window opens without focus and when I try to interact with it, appium crashes.

Environment

Code To Reproduce Issue [ Good To Have ]

AppiumOptions capabilities = new AppiumOptions();

Console.WriteLine($"Installation Folder: {applicationFolder}");

capabilities.App = $"{applicationFolder}\My First Application.exe"; capabilities.DeviceName = "WindowsPC"; capabilities.PlatformName = "Windows"; capabilities.AutomationName = "Windows"; capabilities.AddAdditionalAppiumOption("newCommandTimeout", 300); capabilities.AddAdditionalAppiumOption("appWorkingDir", applicationFolder); capabilities.AddAdditionalAppiumOption("waitForAppLaunch", "25"); if (_firstDriver == null) { _firstDriver = StartDriver(_firstDriver, driverURI, "4724", capabilities); Thread.Sleep(10000); ResizeWindow(); }

capabilities = new AppiumOptions(); capabilities.App = $"{applicationFolder}\My Application.exe"; capabilities.DeviceName = "WindowsPC"; capabilities.PlatformName = "Windows"; capabilities.AutomationName = "Windows"; capabilities.AddAdditionalAppiumOption("newCommandTimeout", 300); capabilities.AddAdditionalAppiumOption("waitForAppLaunch", 25);

if (driver == null) { driver = StartDriver(driver, driverURI, "4723", capabilities); }

private static WindowsDriver StartDriver(WindowsDriver? driver, object? driverURI, string port, AppiumOptions capabilities) { string driverUriValue = driverURI?.ToString();

if (!string.IsNullOrEmpty(driverUriValue))
{
    Uri driverUri = new Uri($"http://{driverUriValue}:{port}/wd/hub");

    // Add logging to verify the URI
    Console.WriteLine($"Created URI: {driverUri}");

    // Continue with the driver initialization
    driver = new WindowsDriver(driverUri, capabilities);
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
}
else
{
    Console.WriteLine($"Error connecting to machine {driverURI}");
}
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
return driver;

}

Exception stack traces

OpenQA.Selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.WebDriver.FindElement(String mechanism, String value) at OpenQA.Selenium.Appium.AppiumDriver.FindElement(String by, String value) at OpenQA.Selenium.Appium.MobileBy.FindElement(ISearchContext context) at OpenQA.Selenium.Appium.ByAccessibilityId.FindElement(ISearchContext context) at OpenQA.Selenium.WebDriver.FindElement(By by) at OpenQA.Selenium.Appium.AppiumDriver.FindElement(By by)

Link to Appium logs

log available on request

Dor-bl commented 6 months ago

Where exactly you see appium crash? It says : OpenQA.Selenium.NoSuchElementException Also, which appium dotnet client version you are using?

valinprogress commented 6 months ago

Edit

The additional appium options are only

capabilities.AddAdditionalAppiumOption("newCommandTimeout", 300); capabilities.AddAdditionalAppiumOption("waitForAppLaunch", 25);

Appium crashes when invoking: driver.FindElement(MobileBy.AccessibilityId("MyElement")).Click();

I am using Appium.WebDriver 5.0.0-beta02 and Selenium.WebDriver 4.5.1

valinprogress commented 6 months ago

I have modified my code above, as shown below (I would like the app that opens being the active one)

if (driver == null) { StartDriver(driver, driverURI, "4723", capabilities);

driver.ActivateApp($"{applicationFolder}\\My Application.exe");

}

but I get an Exception:

System.NotImplementedException

Method has not yet been implemented Source=WebDriver StackTrace: at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver.ActivateApp(String appId)

when the method, in the package version 5.0.0-beta02, is implemented:

public void ActivateApp(string appId) => Execute(AppiumDriverCommand.ActivateApp, AppiumCommandExecutionHelper.PrepareArgument("appId", appId));

Dor-bl commented 6 months ago

@valinprogress Why do you need to Activate the App if you already include it in the appium Options? capabilities.App = $"{applicationFolder}\My Application.exe"; I assume you're having this issue since WinAppDriver does not support the ActivateApp method. The reason for that is that Microsoft has no longer maintain that driver.

valinprogress commented 6 months ago

Edit

I forgot to mention, and edited the first post to be more specific, that I have multiple drivers starting. The first one opens a window; the second, when instantiating the driver, opens the window that I want to interact with, but the second window stays inactive. The focus is on the first window and I cannot interact with "My Application", even with

driver.SwitchTo().Window(driver.WindowHandles[0]);

I have found a workaround with

ResizeWindow();

that resizes the first window and makes the window opened by "My Application" visible. When invoking:

driver.FindElement(MobileBy.AccessibilityId("MyElement")).Click();

the application behaves correctly;

Dor-bl commented 5 months ago

@valinprogress If you found a working workaround, can we close this issue? Taking into consideration WinAppDriver is no longer maintained by Microsoft.

valinprogress commented 5 months ago

@Dor-bl yes we can close it, thanks for your help