|
24 | 24 | import org.json.JSONException;
|
25 | 25 | import org.json.JSONObject;
|
26 | 26 | import org.junit.Test;
|
| 27 | +import org.openqa.selenium.Capabilities; |
| 28 | +import org.openqa.selenium.WebDriverException; |
27 | 29 | import org.openqa.selenium.remote.CapabilityType;
|
28 | 30 | import org.openqa.selenium.remote.CommandExecutor;
|
29 | 31 | import org.openqa.selenium.remote.ErrorCodes;
|
|
34 | 36 | import java.io.IOException;
|
35 | 37 | import java.net.HttpURLConnection;
|
36 | 38 | import java.net.URL;
|
| 39 | +import java.util.concurrent.atomic.AtomicBoolean; |
37 | 40 |
|
38 | 41 | public class RemoteWebDriverTest extends JUnit4TestBase {
|
| 42 | + private boolean stopClientCalled = false; |
| 43 | + private boolean quitCalled = false; |
39 | 44 |
|
40 | 45 | @Test
|
41 | 46 | public void testCanCheckServerStatusIndependentlyOfSessions() throws IOException, JSONException {
|
@@ -78,10 +83,95 @@ public void testCanCheckServerStatusIndependentlyOfSessions() throws IOException
|
78 | 83 | }
|
79 | 84 | }
|
80 | 85 |
|
| 86 | + @Test |
| 87 | + public void testStopsClientIfStartClientFails() { |
| 88 | + if (!(driver instanceof RemoteWebDriver)) { |
| 89 | + System.out.println("Skipping test: driver is not a remote webdriver"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + RemoteWebDriver remote = (RemoteWebDriver) driver; |
| 94 | + boolean exceptionThrown = false; |
| 95 | + AtomicBoolean stopCalled = new AtomicBoolean(false); |
| 96 | + |
| 97 | + try { |
| 98 | + new BadStartClientRemoteWebDriver(remote.getCommandExecutor(), |
| 99 | + remote.getCapabilities(), |
| 100 | + remote.getCapabilities(), |
| 101 | + stopCalled); |
| 102 | + } catch (WebDriverException e) { |
| 103 | + assertTrue(e.getMessage().contains("Failed to start client.")); |
| 104 | + |
| 105 | + exceptionThrown = true; |
| 106 | + } |
| 107 | + |
| 108 | + assertTrue(exceptionThrown); |
| 109 | + assertTrue(stopClientCalled); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testQuitsIfStartSessionFails() { |
| 114 | + if (!(driver instanceof RemoteWebDriver)) { |
| 115 | + System.out.println("Skipping test: driver is not a remote webdriver"); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + RemoteWebDriver remote = (RemoteWebDriver) driver; |
| 120 | + boolean exceptionThrown = false; |
| 121 | + |
| 122 | + try { |
| 123 | + new BadStartSessionRemoteWebDriver(remote.getCommandExecutor(), |
| 124 | + remote.getCapabilities(), |
| 125 | + remote.getCapabilities()); |
| 126 | + } catch (WebDriverException e) { |
| 127 | + assertTrue(e.getMessage().contains("Failed to start session.")); |
| 128 | + |
| 129 | + exceptionThrown = true; |
| 130 | + } |
| 131 | + |
| 132 | + assertTrue(exceptionThrown); |
| 133 | + assertTrue(quitCalled); |
| 134 | + } |
| 135 | + |
81 | 136 | private static void assertHasKeys(JSONObject object, String... keys) {
|
82 | 137 | for (String key : keys) {
|
83 | 138 | assertTrue("Object does not contain expected key: " + key + " (" + object + ")",
|
84 | 139 | object.has(key));
|
85 | 140 | }
|
86 | 141 | }
|
| 142 | + |
| 143 | + private class BadStartClientRemoteWebDriver extends RemoteWebDriver { |
| 144 | + public BadStartClientRemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities, |
| 145 | + Capabilities requiredCapabilities, AtomicBoolean stopCalled) { |
| 146 | + super(executor, desiredCapabilities, requiredCapabilities); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void startClient() { |
| 151 | + throw new RuntimeException("Stub client that should fail"); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected void stopClient() { |
| 156 | + stopClientCalled = true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private class BadStartSessionRemoteWebDriver extends RemoteWebDriver { |
| 161 | + public BadStartSessionRemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities, |
| 162 | + Capabilities requiredCapabilities) { |
| 163 | + super(executor, desiredCapabilities, requiredCapabilities); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected void startSession(Capabilities desiredCapabilities, |
| 168 | + Capabilities requiredCapabilities) { |
| 169 | + throw new RuntimeException("Stub session that should fail"); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void quit() { |
| 174 | + quitCalled = true; |
| 175 | + } |
| 176 | + } |
87 | 177 | }
|
0 commit comments