diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ea37c3..94523fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,7 @@ ## v1.6.0 -- Allow switching process name to `realProcessName` for an apps starting -with a launcher. That can fix exception Process Not Found in close/quit function +- Fix exception Process Not Found in close/quit function - Fix throw exceptions in getting some gui element's attributes diff --git a/src/Winium.Desktop.Driver/CommandExecutors/CloseExecutor.cs b/src/Winium.Desktop.Driver/CommandExecutors/CloseExecutor.cs index 4cbbacd..41d90b5 100644 --- a/src/Winium.Desktop.Driver/CommandExecutors/CloseExecutor.cs +++ b/src/Winium.Desktop.Driver/CommandExecutors/CloseExecutor.cs @@ -1,22 +1,18 @@ namespace Winium.Desktop.Driver.CommandExecutors { + #region using + + using Winium.Desktop.Driver.CommandHelpers; + + #endregion + internal class CloseExecutor : CommandExecutorBase { #region Methods protected override string DoImpl() { - if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp) - { - if (!this.Automator.Application.Close()) - { - this.Automator.Application.Kill(); - } - - this.Automator.ElementsRegistry.Clear(); - } - - return this.JsonResponse(); + return TerminateApp.TerminateExcecutor(this.Automator, this.JsonResponse()); } #endregion diff --git a/src/Winium.Desktop.Driver/CommandExecutors/NewSessionExecutor.cs b/src/Winium.Desktop.Driver/CommandExecutors/NewSessionExecutor.cs index 2c50786..982c73a 100644 --- a/src/Winium.Desktop.Driver/CommandExecutors/NewSessionExecutor.cs +++ b/src/Winium.Desktop.Driver/CommandExecutors/NewSessionExecutor.cs @@ -32,17 +32,6 @@ protected override string DoImpl() // Gives sometime to load visuals (needed only in case of slow emulation) Thread.Sleep(this.Automator.ActualCapabilities.LaunchDelay); - // Update running application process pointing to the real process instead of the launcher in such cases - if (this.Automator.Application.HasExited()) - { - // Add parse process name pass from request - var realProcessName = this.ExecutedCommand.Parameters["desiredCapabilities"]["realProcessName"]; - // Update launched process by process name if it's exited - if (realProcessName != null) - { - this.Automator.Application.UpdateProcessByName(realProcessName.ToString()); - } - } return this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities); } diff --git a/src/Winium.Desktop.Driver/CommandExecutors/QuitExecutor.cs b/src/Winium.Desktop.Driver/CommandExecutors/QuitExecutor.cs index 59b6fb6..fa507be 100644 --- a/src/Winium.Desktop.Driver/CommandExecutors/QuitExecutor.cs +++ b/src/Winium.Desktop.Driver/CommandExecutors/QuitExecutor.cs @@ -1,22 +1,18 @@ namespace Winium.Desktop.Driver.CommandExecutors { + #region using + + using Winium.Desktop.Driver.CommandHelpers; + + #endregion + internal class QuitExecutor : CommandExecutorBase { #region Methods protected override string DoImpl() { - if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp) - { - if (!this.Automator.Application.Close()) - { - this.Automator.Application.Kill(); - } - - this.Automator.ElementsRegistry.Clear(); - } - - return this.JsonResponse(); + return TerminateApp.TerminateExcecutor(this.Automator, this.JsonResponse()); } #endregion diff --git a/src/Winium.Desktop.Driver/CommandHelpers/TerminateApp.cs b/src/Winium.Desktop.Driver/CommandHelpers/TerminateApp.cs new file mode 100644 index 0000000..3855a28 --- /dev/null +++ b/src/Winium.Desktop.Driver/CommandHelpers/TerminateApp.cs @@ -0,0 +1,45 @@ +namespace Winium.Desktop.Driver.CommandHelpers +{ + #region using + + using System.Collections.Generic; + using System.Diagnostics; + + using Winium.Desktop.Driver.Automator; + + #endregion + + public static class TerminateApp + { + public static string TerminateExcecutor(object automatorObject, string jsonResponse) + { + Automator automator = (Automator)automatorObject; + if (!automator.ActualCapabilities.DebugConnectToRunningApp) + { + // If application had exited, find and terminate all children processes + if (automator.Application.HasExited()) + { + List children = new List(); + children = automator.Application.GetChildPrecesses(automator.Application.GetProcessId()); + foreach (var child in children) + { + if (!child.HasExited && !automator.Application.Close(child)) + { + automator.Application.Kill(child); + } + } + } + + // If application is still running, terminate it as normal case + else if (!automator.Application.Close()) + { + automator.Application.Kill(); + } + + automator.ElementsRegistry.Clear(); + } + + return jsonResponse; + } + } +} diff --git a/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj b/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj index a6bcf99..10f5fe0 100644 --- a/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj +++ b/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj @@ -116,6 +116,7 @@ +