From 0e6231013925b43feaf04340424399102f6e2357 Mon Sep 17 00:00:00 2001 From: Brian Harvey Date: Wed, 7 Feb 2024 09:23:16 -0500 Subject: [PATCH] feat: Source method (#134) Implements method: `/session/:sessionId/source` --- Docs/SupportedAPIs.md | 2 +- Sources/WebDriver/Requests.swift | 15 +++++++++++++++ Sources/WebDriver/Session.swift | 6 ++++++ Tests/UnitTests/APIToRequestMappingTests.swift | 10 ++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 918928b..5e35d71 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -46,7 +46,7 @@ Contributions to expand support to unimplemented functionality are always welcom | GET | `/session/:sessionId/orientation` | Supported | Not implemented | | POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` | | GET | `/session/:sessionId/screenshot` | Supported | `Session.screenshot()`| -| GET | `/session/:sessionId/source` | Supported | Not implemented | +| GET | `/session/:sessionId/source` | Supported | `Session.source` | | POST | `/session/:sessionId/timeouts` | Supported | `Session.setTimeout()`| | GET | `/session/:sessionId/title` | Supported | `Session.title` | | POST | `/session/:sessionId/touch/click` | Supported | `Element.touchClick()`| diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index ebf226e..29d2948 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -541,6 +541,21 @@ public enum Requests { } } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource + public struct SessionSource: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "source"] } + public var method: HTTPMethod {.get} + + public typealias Response = ResponseWithValue + + public struct ResponseValue: Codable { + public var source: String + } + + } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status public struct Status: Request { public var pathComponents: [String] { ["status"] } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index e78b6c7..b59f4ab 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -321,6 +321,12 @@ public class Session { try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) } + /// - Returns: The current page source. + public func source() throws -> String { + let response = try webDriver.send(Requests.SessionSource(session: id)) + return response.value.source + } + /// Deletes the current session. public func delete() throws { guard shouldDelete else { return } diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 79f4769..a054ee1 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -213,4 +213,14 @@ class APIToRequestMappingTests: XCTestCase { } XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } + + 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")) + } + XCTAssert(try session.source() == "currentSource") + } }