From b68ee248296495f4acd24c83fbae1546496b2b1a Mon Sep 17 00:00:00 2001 From: Dave Roberts Date: Wed, 2 Oct 2024 10:19:13 -0500 Subject: [PATCH] Add new-window API to expose WebDriver's New Window endpoint --- CHANGELOG.adoc | 3 +++ src/etaoin/api.clj | 23 ++++++++++++++++++++++- test/etaoin/api_test.clj | 10 ++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 890d95f..2684a9a 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -19,6 +19,9 @@ A release with an intentional breaking changes is marked with: // (adjust these in publish.clj as you see fit) == Unreleased +* Changes +** {issue}679[#679]: Add `new-window` function that exposes WebDriver's New Window endpoint. + == v1.1.42 - 2024-09-27 [[v1.1.42]] * Changes diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index 8daa0bc..add03c4 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -92,7 +92,7 @@ - [[get-window-size]] [[set-window-size]] - [[maximize]] - [[switch-window]] [[switch-window-next]] - - [[close-window]] + - [[new-window]] [[close-window]] **Frames** - [[switch-frame]] [[switch-frame-first]] [[switch-frame-parent]] [[switch-frame-top]] [[with-frame]] @@ -372,6 +372,27 @@ (recur hs)))] (switch-window driver next-handle))) +(defn new-window + "Have `driver` create a new window. The `window-type-hint` parameter + must be either `:tab` or `:window` and specifies the type of window + that is desired, if supported by the browser. If successful, return + a map with keys `:handle` indicating the new window handle and + `:type` indicating the type of window that was actually + created (either `:tab` or `:window`). + + https://www.w3.org/TR/webdriver2/#dfn-new-window" + [driver window-type-hint] + (if (#{:tab :window} window-type-hint) + (-> (execute {:driver driver + :method :post + :path [:session (:session driver) :window :new] + :data {:type window-type-hint}}) + :value + (update :type keyword)) + (throw+ {:type :etaoin/argument + :message "Argument `window-type-hint` must be either `:tab` or `:window`." + :window-type-hint window-type-hint}))) + (defn close-window "Have `driver` close current browser window. On last window close, closes the session. diff --git a/test/etaoin/api_test.clj b/test/etaoin/api_test.clj index 99c7709..753670f 100644 --- a/test/etaoin/api_test.clj +++ b/test/etaoin/api_test.clj @@ -637,6 +637,16 @@ (is (not= width width')) (is (not= height height')))))) +(deftest test-new-window + (is (= 1 (count (e/get-window-handles *driver*)))) + (let [initial-window (e/get-window-handle *driver*) + new-windows (for [_ (range 3)] + (-> (e/new-window *driver* :tab) + :handle)) + windows (into #{initial-window} new-windows)] + (is (= 4 (count (e/get-window-handles *driver*)))) + (is (= windows (set (e/get-window-handles *driver*)))))) + (deftest test-switch-window (let [init-handle (e/get-window-handle *driver*) init-url (e/get-url *driver*)]