diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index cb258d1..20947af 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 67531b9..5413e5b 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -548,12 +548,7 @@ public enum Requests { public var pathComponents: [String] { ["session", session, "source"] } public var method: HTTPMethod {.get} - public typealias Response = ResponseWithValue - - public struct ResponseValue: Codable { - public var source: String - } - + public typealias Response = ResponseWithValue } // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status @@ -588,4 +583,23 @@ public enum Requests { public typealias Response = ResponseWithValue } } + + // 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 b014c32..6d6fcbe 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -333,10 +333,27 @@ public class Session { try webDriver.send(Requests.SessionOrientation.Post(session: id, orientation: value)) } - /// - Returns: The current page source. - public func source() throws -> String { - let response = try webDriver.send(Requests.SessionSource(session: id)) - return response.value.source + /// Get the current page source + public var source: String { + get throws { + try webDriver.send(Requests.SessionSource(session: id)).value + } + } + + /// - Returns: Current window handle + public var windowHandle: String { + get throws { + let response = try webDriver.send(Requests.SessionWindowHandle(session: id)) + return response.value + } + } + + /// - Returns: Array of window handles + public var windowHandles: [String] { + get throws { + let response = try webDriver.send(Requests.SessionWindowHandles(session: id)) + return response.value + } } /// Deletes the current session. diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index c7fc7e3..6d75cf4 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -97,6 +97,26 @@ class APIToRequestMappingTests: XCTestCase { } try session.buttonUp(button: .right) } + + func testSessionOrientation() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + mockWebDriver.expect(path: "session/mySession/orientation", method: .post) + try session.setOrientation(.portrait) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { + ResponseWithValue(.portrait) + } + XCTAssert(try session.orientation == .portrait) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .post) + try session.setOrientation(.landscape) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { + ResponseWithValue(.landscape) + } + XCTAssert(try session.orientation == .landscape) + } func testSendKeys() throws { let mockWebDriver = MockWebDriver() @@ -214,35 +234,33 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } - - func testWindowOrientation() throws { + func testWindowHandle() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") - mockWebDriver.expect(path: "session/mySession/orientation", method: .post) - try session.setOrientation(.portrait) - mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { - ResponseWithValue(.portrait) + mockWebDriver.expect(path: "session/mySession/window_handle", method: .get, type: Requests.SessionWindowHandle.self) { + ResponseWithValue(.init("myWindow")) } - XCTAssert(try session.orientation == .portrait) - - mockWebDriver.expect(path: "session/mySession/orientation", method: .post) - try session.setOrientation(.landscape) + XCTAssert(try session.windowHandle == "myWindow") + } - mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { - ResponseWithValue(.landscape) + 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.orientation == .landscape) + XCTAssert(try session.windowHandles == ["myWindow", "myWindow"]) } - func testSessionSource() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) { - ResponseWithValue(.init(source: "currentSource")) + ResponseWithValue("currentSource") } - XCTAssert(try session.source() == "currentSource") + XCTAssert(try session.source == "currentSource") } }