Skip to content

Commit ff484ed

Browse files
author
quangnt
committed
Find child processes in Quit,Close to fix issue Process Terminated.
1 parent 027b77a commit ff484ed

File tree

6 files changed

+61
-35
lines changed

6 files changed

+61
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
## v1.6.0
1010

11-
- Allow switching process name to `realProcessName` for an apps starting
12-
with a launcher. That can fix exception Process Not Found in close/quit function
11+
- Fix exception Process Not Found in close/quit function
1312
- Fix throw exceptions in getting some gui element's attributes
1413

1514

src/Winium.Desktop.Driver/CommandExecutors/CloseExecutor.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
namespace Winium.Desktop.Driver.CommandExecutors
22
{
3+
#region using
4+
5+
using Winium.Desktop.Driver.CommonHelpers;
6+
7+
#endregion
8+
39
internal class CloseExecutor : CommandExecutorBase
410
{
511
#region Methods
612

713
protected override string DoImpl()
814
{
9-
if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp)
10-
{
11-
if (!this.Automator.Application.Close())
12-
{
13-
this.Automator.Application.Kill();
14-
}
15-
16-
this.Automator.ElementsRegistry.Clear();
17-
}
18-
19-
return this.JsonResponse();
15+
return CommonHelpers.TerminateExcecutor(this.Automator, this.JsonResponse());
2016
}
2117

2218
#endregion

src/Winium.Desktop.Driver/CommandExecutors/NewSessionExecutor.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ protected override string DoImpl()
3232
// Gives sometime to load visuals (needed only in case of slow emulation)
3333
Thread.Sleep(this.Automator.ActualCapabilities.LaunchDelay);
3434

35-
// Update running application process pointing to the real process instead of the launcher in such cases
36-
if (this.Automator.Application.HasExited())
37-
{
38-
// Add parse process name pass from request
39-
var realProcessName = this.ExecutedCommand.Parameters["desiredCapabilities"]["realProcessName"];
40-
// Update launched process by process name if it's exited
41-
if (realProcessName != null)
42-
{
43-
this.Automator.Application.UpdateProcessByName(realProcessName.ToString());
44-
}
45-
}
4635
return this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities);
4736
}
4837

src/Winium.Desktop.Driver/CommandExecutors/QuitExecutor.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
namespace Winium.Desktop.Driver.CommandExecutors
22
{
3+
#region using
4+
5+
using Winium.Desktop.Driver.CommonHelpers;
6+
7+
#endregion
8+
39
internal class QuitExecutor : CommandExecutorBase
410
{
511
#region Methods
612

713
protected override string DoImpl()
814
{
9-
if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp)
10-
{
11-
if (!this.Automator.Application.Close())
12-
{
13-
this.Automator.Application.Kill();
14-
}
15-
16-
this.Automator.ElementsRegistry.Clear();
17-
}
18-
19-
return this.JsonResponse();
15+
return CommonHelpers.TerminateExcecutor(this.Automator, this.JsonResponse());
2016
}
2117

2218
#endregion
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Winium.Desktop.Driver.CommonHelpers
2+
{
3+
#region using
4+
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
8+
using Winium.Desktop.Driver.Automator;
9+
10+
#endregion
11+
12+
public static class CommonHelpers
13+
{
14+
public static string TerminateExcecutor(object automatorObject, string jsonResponse)
15+
{
16+
Automator automator = (Automator)automatorObject;
17+
if (!automator.ActualCapabilities.DebugConnectToRunningApp)
18+
{
19+
// If application had exited, find and terminate all children processes
20+
if (automator.Application.HasExited())
21+
{
22+
List<Process> children = new List<Process>();
23+
children = automator.Application.GetChildPrecesses(automator.Application.GetProcessId());
24+
foreach (var child in children)
25+
{
26+
if (!child.HasExited && !automator.Application.Close(child))
27+
{
28+
automator.Application.Kill(child);
29+
}
30+
}
31+
}
32+
33+
// If application is still running, terminate it as normal case
34+
else if (!automator.Application.Close())
35+
{
36+
automator.Application.Kill();
37+
}
38+
39+
automator.ElementsRegistry.Clear();
40+
}
41+
42+
return jsonResponse;
43+
}
44+
}
45+
}

src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<Compile Include="CommandExecutors\NotImplementedExecutor.cs" />
117117
<Compile Include="CommandExecutors\SwitchToWindowExecutor.cs" />
118118
<Compile Include="CommandLineOptions.cs" />
119+
<Compile Include="CommonHelpers\CommonHelpers.cs" />
119120
<Compile Include="ElementsRegistry.cs" />
120121
<Compile Include="Extensions\AutomationPropertyHelper.cs" />
121122
<Compile Include="Extensions\ByHelper.cs" />

0 commit comments

Comments
 (0)