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") + } }