From 4d3408cb61d7856356563020efc771f954c115fb Mon Sep 17 00:00:00 2001 From: Brian Harvey Date: Mon, 26 Feb 2024 09:33:32 -0500 Subject: [PATCH] feat: Return one or many window handles (#131) --- Docs/SupportedAPIs.md | 4 ++-- Sources/WebDriver/Requests.swift | 19 ++++++++++++++++++ Sources/WebDriver/Session.swift | 12 +++++++++++ .../UnitTests/APIToRequestMappingTests.swift | 20 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 5e35d71..ba8ca67 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -69,5 +69,5 @@ Contributions to expand support to unimplemented functionality are always welcom | POST | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented | | GET | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented | | POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | Not implemented | -| GET | `/session/:sessionId/window_handle` | Supported | Not implemented | -| GET | `/session/:sessionId/window_handles` | Supported | Not implemented | +| GET | `/session/:sessionId/window_handle` | Supported | `Session.windowHandle()`| +| GET | `/session/:sessionId/window_handles` | Supported | `Session.windowHandles()`| \ No newline at end of file diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 29d2948..66bf035 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -563,4 +563,23 @@ public enum Requests { public typealias Response = WebDriverStatus } + + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindow_handle + public struct SessionWindowHandle: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "window_handle"] } + public var method: HTTPMethod { .get } + + public typealias Response = ResponseWithValue + } + + public struct SessionWindowHandles: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "window_handles"] } + public var method: HTTPMethod { .get } + + public typealias Response = ResponseWithValue> + } } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index b59f4ab..29675f0 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -326,6 +326,18 @@ public class Session { let response = try webDriver.send(Requests.SessionSource(session: id)) return response.value.source } + + /// - Returns: Current window handle + public func windowHandle() throws -> String { + let response = try webDriver.send(Requests.SessionWindowHandle(session: id)) + return response.value + } + + /// - Returns: Array of window handles + public func windowHandles() throws -> [String] { + let response = try webDriver.send(Requests.SessionWindowHandles(session: id)) + return response.value + } /// Deletes the current session. public func delete() throws { diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index a054ee1..cfe5f11 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -214,6 +214,26 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } + func testWindowHandle() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + + mockWebDriver.expect(path: "session/mySession/window_handle", method: .get, type: Requests.SessionWindowHandle.self) { + ResponseWithValue(.init("myWindow")) + } + XCTAssert(try session.windowHandle() == "myWindow") + } + + func testWindowHandles() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + + mockWebDriver.expect(path: "session/mySession/window_handles", method: .get, type: Requests.SessionWindowHandles.self) { + ResponseWithValue(.init(["myWindow", "myWindow"])) + } + XCTAssert(try session.windowHandles() == ["myWindow", "myWindow"]) + } + func testSessionSource() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession")