|
38 | 38 | package com.groupon.seleniumgridextras.tasks;
|
39 | 39 |
|
40 | 40 |
|
| 41 | +import com.google.common.base.Throwables; |
41 | 42 | import com.google.gson.JsonObject;
|
42 |
| - |
| 43 | +import com.groupon.seleniumgridextras.ExecuteCommand; |
| 44 | +import com.groupon.seleniumgridextras.config.RuntimeConfig; |
43 | 45 | import com.groupon.seleniumgridextras.tasks.config.TaskDescriptions;
|
| 46 | +import com.groupon.seleniumgridextras.utilities.json.JsonCodec; |
| 47 | +import com.groupon.seleniumgridextras.utilities.json.JsonParserWrapper; |
| 48 | +import org.apache.log4j.Logger; |
| 49 | + |
| 50 | +import java.util.HashMap; |
| 51 | +import java.util.Map; |
44 | 52 |
|
45 | 53 | public class KillAllIE extends KillAllByName {
|
46 | 54 |
|
47 |
| - public KillAllIE() { |
48 |
| - setEndpoint(TaskDescriptions.Endpoints.KILL_IE); |
49 |
| - setDescription(TaskDescriptions.Description.KILL_IE); |
50 |
| - JsonObject params = new JsonObject(); |
51 |
| - setAcceptedParams(params); |
52 |
| - setRequestType("GET"); |
53 |
| - setResponseType("json"); |
54 |
| - setClassname(this.getClass().getCanonicalName().toString()); |
55 |
| - setCssClass(TaskDescriptions.UI.BTN_DANGER); |
56 |
| - setButtonText(TaskDescriptions.UI.ButtonText.KILL_IE); |
57 |
| - setEnabledInGui(true); |
58 |
| - } |
59 |
| - |
60 |
| - |
61 |
| - @Override |
62 |
| - public String getWindowsCommand() { |
63 |
| - return super.getWindowsCommand("iexplore.exe"); |
64 |
| - } |
65 |
| - |
66 |
| - @Override |
67 |
| - public String getLinuxCommand() { |
68 |
| - return super.getLinuxCommand("iexplore.exe"); |
69 |
| - } |
70 |
| - |
71 |
| - @Override |
72 |
| - public String getMacCommand() { |
73 |
| - return super.getMacCommand("iexplore.exe"); |
74 |
| - } |
| 55 | + public static final String CLEAR_HISTORY_RESPONSE = "clear_history_response"; |
| 56 | + public static final String KILL_IE_RESPONSE = "kill_ie_response"; |
| 57 | + public static final String KILL_DRIVER_RESPONSE = "kill_driver_response"; |
| 58 | + |
| 59 | + private static Logger logger = Logger.getLogger(KillAllIE.class); |
| 60 | + |
| 61 | + public KillAllIE() { |
| 62 | + setEndpoint(TaskDescriptions.Endpoints.KILL_IE); |
| 63 | + setDescription(TaskDescriptions.Description.KILL_IE); |
| 64 | + JsonObject params = new JsonObject(); |
| 65 | + setAcceptedParams(params); |
| 66 | + setRequestType("GET"); |
| 67 | + setResponseType("json"); |
| 68 | + setClassname(this.getClass().getCanonicalName().toString()); |
| 69 | + setCssClass(TaskDescriptions.UI.BTN_DANGER); |
| 70 | + setButtonText(TaskDescriptions.UI.ButtonText.KILL_IE); |
| 71 | + setEnabledInGui(true); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @Override |
| 76 | + public JsonObject execute(String parameter) { |
| 77 | + return execute(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public JsonObject execute(Map<String, String> parameter) { |
| 82 | + return execute(); |
| 83 | + } |
| 84 | + |
| 85 | + public JsonObject execute() { |
| 86 | + |
| 87 | + if (!RuntimeConfig.getOS().isWindows()) { |
| 88 | + getJsonResponse().addKeyValues(JsonCodec.ERROR, "This command can only be executed on Windows"); |
| 89 | + return getJsonResponse().getJson(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + Map<String, String> responsesList = new HashMap<String, String>(); |
| 94 | + try { |
| 95 | + logger.info(String.format("Killing all IE Driver instances with command %s", getKillDriverCommand())); |
| 96 | + JsonObject killDriverResponse = ExecuteCommand.execRuntime(getKillDriverCommand(), true); |
| 97 | + logger.debug(killDriverResponse); |
| 98 | + responsesList.put(KILL_DRIVER_RESPONSE, JsonParserWrapper.prettyPrintString(killDriverResponse)); |
| 99 | + |
| 100 | + logger.info(String.format("Killing all IE instances with command %s", getKillIECommand())); |
| 101 | + JsonObject killIEResponse = ExecuteCommand.execRuntime(getKillIECommand(), true); |
| 102 | + logger.debug(killIEResponse); |
| 103 | + responsesList.put(KILL_IE_RESPONSE, JsonParserWrapper.prettyPrintString(killIEResponse)); |
| 104 | + |
| 105 | + logger.info(String.format("Clearing all browser data with command %s", getClearHistoryCommand())); |
| 106 | + JsonObject clearHistoryResponse = ExecuteCommand.execRuntime(getClearHistoryCommand(), true); |
| 107 | + logger.debug(clearHistoryResponse); |
| 108 | + responsesList.put(CLEAR_HISTORY_RESPONSE, JsonParserWrapper.prettyPrintString(clearHistoryResponse)); |
| 109 | + } catch (Exception e) { |
| 110 | + getJsonResponse().addKeyValues(JsonCodec.ERROR, Throwables.getStackTraceAsString(e)); |
| 111 | + } |
| 112 | + |
| 113 | + getJsonResponse().addKeyValues(JsonCodec.OUT, responsesList); |
| 114 | + return getJsonResponse().getJson(); |
| 115 | + } |
| 116 | + |
| 117 | + public String getKillDriverCommand() { |
| 118 | + return "taskkill -F -T -IM iedriver*"; |
| 119 | + } |
| 120 | + |
| 121 | + public String getKillIECommand() { |
| 122 | + return "taskkill -F -T -IM iexplore*"; |
| 123 | + } |
| 124 | + |
| 125 | + public String getClearHistoryCommand() { |
| 126 | + return "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351"; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean initialize() { |
| 131 | + try { |
| 132 | + if (RuntimeConfig.getOS().isWindows()) { |
| 133 | + execute(); |
| 134 | + } |
| 135 | + } catch (Exception error) { |
| 136 | + printInitilizedFailure(); |
| 137 | + logger.error(error); |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + printInitilizedSuccessAndRegisterWithAPI(); |
| 142 | + return true; |
| 143 | + |
| 144 | + } |
75 | 145 | }
|
0 commit comments