Skip to content

Commit 371a394

Browse files
authored
Add new-window API to expose WebDriver's New Window endpoint (clj-commons#680)
* Add workflow_dispatch to test.yml * Add new-window API to expose WebDriver's New Window endpoint * Revert "Add workflow_dispatch to test.yml" This reverts commit dd59ae9. * Add credit in CHANGELOG
1 parent 5e68f69 commit 371a394

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ A release with an intentional breaking changes is marked with:
1919
// (adjust these in publish.clj as you see fit)
2020
== Unreleased
2121

22+
* Changes
23+
** {issue}679[#679]: Add `new-window` function that exposes WebDriver's New Window endpoint. ({person}dgr[@dgr])
24+
2225
== v1.1.42 - 2024-09-27 [[v1.1.42]]
2326

2427
* Changes

src/etaoin/api.clj

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
- [[get-window-size]] [[set-window-size]]
9393
- [[maximize]]
9494
- [[switch-window]] [[switch-window-next]]
95-
- [[close-window]]
95+
- [[new-window]] [[close-window]]
9696
9797
**Frames**
9898
- [[switch-frame]] [[switch-frame-first]] [[switch-frame-parent]] [[switch-frame-top]] [[with-frame]]
@@ -372,6 +372,27 @@
372372
(recur hs)))]
373373
(switch-window driver next-handle)))
374374

375+
(defn new-window
376+
"Have `driver` create a new window. The `window-type-hint` parameter
377+
must be either `:tab` or `:window` and specifies the type of window
378+
that is desired, if supported by the browser. If successful, return
379+
a map with keys `:handle` indicating the new window handle and
380+
`:type` indicating the type of window that was actually
381+
created (either `:tab` or `:window`).
382+
383+
https://www.w3.org/TR/webdriver2/#dfn-new-window"
384+
[driver window-type-hint]
385+
(if (#{:tab :window} window-type-hint)
386+
(-> (execute {:driver driver
387+
:method :post
388+
:path [:session (:session driver) :window :new]
389+
:data {:type window-type-hint}})
390+
:value
391+
(update :type keyword))
392+
(throw+ {:type :etaoin/argument
393+
:message "Argument `window-type-hint` must be either `:tab` or `:window`."
394+
:window-type-hint window-type-hint})))
395+
375396
(defn close-window
376397
"Have `driver` close current browser window.
377398
On last window close, closes the session.

test/etaoin/api_test.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,16 @@
637637
(is (not= width width'))
638638
(is (not= height height'))))))
639639

640+
(deftest test-new-window
641+
(is (= 1 (count (e/get-window-handles *driver*))))
642+
(let [initial-window (e/get-window-handle *driver*)
643+
new-windows (for [_ (range 3)]
644+
(-> (e/new-window *driver* :tab)
645+
:handle))
646+
windows (into #{initial-window} new-windows)]
647+
(is (= 4 (count (e/get-window-handles *driver*))))
648+
(is (= windows (set (e/get-window-handles *driver*))))))
649+
640650
(deftest test-switch-window
641651
(let [init-handle (e/get-window-handle *driver*)
642652
init-url (e/get-url *driver*)]

0 commit comments

Comments
 (0)