From 7b5590169e2a2a9e258e43a27be1b918f028bc7a Mon Sep 17 00:00:00 2001 From: Daniel Bengtsson Date: Wed, 23 Jan 2013 13:50:45 +0100 Subject: [PATCH 1/2] WindowsUtils.kill() fix On Windows 8 the kill() function did not correctly locate the process in the list. There were two issues: * The executable name in the regexp was duplicated. This patch changed the iteration over the arguments to not include the executable again. * If the process was started without using .exe in the name kill() could not find it as the regexp did not have the .exe there. This patch adds .exe if not already present as an optional match. --- .../src/org/openqa/selenium/os/WindowsUtils.java | 10 +++++++--- .../openqa/selenium/os/WindowsUtilsUnitTest.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/java/client/src/org/openqa/selenium/os/WindowsUtils.java b/java/client/src/org/openqa/selenium/os/WindowsUtils.java index a2a28cd6ab546..14861b49f840f 100644 --- a/java/client/src/org/openqa/selenium/os/WindowsUtils.java +++ b/java/client/src/org/openqa/selenium/os/WindowsUtils.java @@ -110,9 +110,13 @@ public static void kill(String[] cmdarray) throws Exception { */ // TODO We should be careful, in case Windows has ~1-ified the executable name as well pattern.append("\"?.*?\\\\"); - pattern.append(executable.getName()); + String execName = executable.getName(); + pattern.append(execName); + if (!execName.endsWith(".exe")) { + pattern.append("(\\.exe)?"); + } pattern.append("\"?"); - for (String arg : cmdarray) { + for (int i = 1; i < cmdarray.length; i++) { /* * There may be a space, but maybe not (\\s?), may be a quote or maybe not (\"?), but then * turn on block quotation (as if *everything* had a regex backslash in front of it) with \Q. @@ -120,7 +124,7 @@ public static void kill(String[] cmdarray) throws Exception { * quotation. Now ignore a final quote if any (\"?) */ pattern.append("\\s?\"?\\Q"); - pattern.append(arg); + pattern.append(cmdarray[i]); pattern.append("\\E\"?"); } pattern.append("\\s*"); diff --git a/java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java b/java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java index 6f37006eb8b58..4245111950fea 100644 --- a/java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java +++ b/java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.Test; +import java.util.Arrays; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -73,6 +74,19 @@ public void testTaskKill() { assertFalse("taskkill should be found", "taskkill".equals(WindowsUtils.findTaskKill())); } + private void tryKill(String[] cmd) throws Exception { + CommandLine cl = new CommandLine(cmd); + cl.executeAsync(); + WindowsUtils.kill(cmd); + assertFalse("Should be able to kill " + Arrays.toString(cmd), cl.isRunning()); + } + + @Test + public void testKill() throws Exception { + tryKill(new String[]{"sleep.exe", "10"}); + tryKill(new String[]{"sleep", "10"}); + } + @Test public void testRegistry() { if (!WindowsUtils.thisIsWindows()) return; From 4e0f81d94ef6eed8fdd1cb5a7b15463be3cba5e4 Mon Sep 17 00:00:00 2001 From: Daniel Bengtsson Date: Tue, 12 Feb 2013 15:26:39 +0100 Subject: [PATCH 2/2] Find processes without the path prefix In some cases the process list will not have a binary prefixed with the full path, thus the regexp fails as it alwasy expect a \. This makes the \ optional. --- java/client/src/org/openqa/selenium/os/WindowsUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/client/src/org/openqa/selenium/os/WindowsUtils.java b/java/client/src/org/openqa/selenium/os/WindowsUtils.java index 14861b49f840f..a531fd611c5eb 100644 --- a/java/client/src/org/openqa/selenium/os/WindowsUtils.java +++ b/java/client/src/org/openqa/selenium/os/WindowsUtils.java @@ -109,7 +109,7 @@ public static void kill(String[] cmdarray) throws Exception { * quote (\"?) */ // TODO We should be careful, in case Windows has ~1-ified the executable name as well - pattern.append("\"?.*?\\\\"); + pattern.append("(\"?.*?\\\\)?"); String execName = executable.getName(); pattern.append(execName); if (!execName.endsWith(".exe")) {