Skip to content

Commit 0151d5d

Browse files
committed
Provide a free-function to delete an existing session
Issue tebeka#148
1 parent 2a4ff98 commit 0151d5d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

remote.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"io/ioutil"
1313
"mime"
1414
"net/http"
15+
"net/url"
16+
"path"
1517
"strings"
1618
"time"
1719

@@ -122,6 +124,10 @@ func (e *Error) Error() string {
122124
// encoded by the remote end in a JSON structure. If no error is present, the
123125
// entire, raw request payload is returned.
124126
func (wd *remoteWD) execute(method, url string, data []byte) (json.RawMessage, error) {
127+
return executeCommand(method, url, data)
128+
}
129+
130+
func executeCommand(method, url string, data []byte) (json.RawMessage, error) {
125131
debugLog("-> %s %s\n%s", method, filteredURL(url), data)
126132
request, err := newRequest(method, url, data)
127133
if err != nil {
@@ -213,7 +219,7 @@ const DefaultURLPrefix = "http://127.0.0.1:4444/wd/hub"
213219
// Providing an empty string for urlPrefix causes the DefaultURLPrefix to be
214220
// used.
215221
func NewRemote(capabilities Capabilities, urlPrefix string) (WebDriver, error) {
216-
if len(urlPrefix) == 0 {
222+
if urlPrefix == "" {
217223
urlPrefix = DefaultURLPrefix
218224
}
219225

@@ -230,6 +236,17 @@ func NewRemote(capabilities Capabilities, urlPrefix string) (WebDriver, error) {
230236
return wd, nil
231237
}
232238

239+
// DeleteSession deletes an existing session at the WebDriver instance
240+
// specified by the urlPrefix and the session ID.
241+
func DeleteSession(urlPrefix, id string) error {
242+
u, err := url.Parse(urlPrefix)
243+
if err != nil {
244+
return err
245+
}
246+
u.Path = path.Join(u.Path, "session", id)
247+
return voidCommand("DELETE", u.String(), nil)
248+
}
249+
233250
func (wd *remoteWD) stringCommand(urlTemplate string) (string, error) {
234251
url := wd.requestURL(urlTemplate, wd.id)
235252
response, err := wd.execute("GET", url, nil)
@@ -249,18 +266,22 @@ func (wd *remoteWD) stringCommand(urlTemplate string) (string, error) {
249266
return *reply.Value, nil
250267
}
251268

252-
func (wd *remoteWD) voidCommand(urlTemplate string, params interface{}) error {
269+
func voidCommand(method, url string, params interface{}) error {
253270
if params == nil {
254271
params = make(map[string]interface{})
255272
}
256273
data, err := json.Marshal(params)
257274
if err != nil {
258275
return err
259276
}
260-
_, err = wd.execute("POST", wd.requestURL(urlTemplate, wd.id), data)
277+
_, err = executeCommand(method, url, data)
261278
return err
262279
}
263280

281+
func (wd *remoteWD) voidCommand(urlTemplate string, params interface{}) error {
282+
return voidCommand("POST", wd.requestURL(urlTemplate, wd.id), params)
283+
}
284+
264285
func (wd remoteWD) stringsCommand(urlTemplate string) ([]string, error) {
265286
url := wd.requestURL(urlTemplate, wd.id)
266287
response, err := wd.execute("GET", url, nil)

remote_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ func runTest(f func(*testing.T, config), c config) func(*testing.T) {
535535
func runTests(t *testing.T, c config) {
536536
t.Run("Status", runTest(testStatus, c))
537537
t.Run("NewSession", runTest(testNewSession, c))
538+
t.Run("DeleteSession", runTest(testDeleteSession, c))
538539
t.Run("Error", runTest(testError, c))
539540
t.Run("ExtendedErrorMessage", runTest(testExtendedErrorMessage, c))
540541
t.Run("Capabilities", runTest(testCapabilities, c))
@@ -627,6 +628,15 @@ func testNewSession(t *testing.T, c config) {
627628
}
628629
}
629630

631+
func testDeleteSession(t *testing.T, c config) {
632+
wd := newRemote(t, c)
633+
defer quitRemote(t, wd)
634+
635+
if err := DeleteSession(c.addr, wd.SessionID()); err != nil {
636+
t.Fatalf("DeleteSession(%s, %s) returned error: %v", c.addr, wd.SessionID(), err)
637+
}
638+
}
639+
630640
func testError(t *testing.T, c config) {
631641
wd := newRemote(t, c)
632642
defer quitRemote(t, wd)

0 commit comments

Comments
 (0)