Skip to content

Commit 0e62310

Browse files
authored
feat: Source method (#134)
Implements method: `/session/:sessionId/source`
1 parent b84c7a2 commit 0e62310

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

Docs/SupportedAPIs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Contributions to expand support to unimplemented functionality are always welcom
4646
| GET | `/session/:sessionId/orientation` | Supported | Not implemented |
4747
| POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` |
4848
| GET | `/session/:sessionId/screenshot` | Supported | `Session.screenshot()`|
49-
| GET | `/session/:sessionId/source` | Supported | Not implemented |
49+
| GET | `/session/:sessionId/source` | Supported | `Session.source` |
5050
| POST | `/session/:sessionId/timeouts` | Supported | `Session.setTimeout()`|
5151
| GET | `/session/:sessionId/title` | Supported | `Session.title` |
5252
| POST | `/session/:sessionId/touch/click` | Supported | `Element.touchClick()`|

Sources/WebDriver/Requests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,21 @@ public enum Requests {
541541
}
542542
}
543543

544+
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource
545+
public struct SessionSource: Request {
546+
public var session: String
547+
548+
public var pathComponents: [String] { ["session", session, "source"] }
549+
public var method: HTTPMethod {.get}
550+
551+
public typealias Response = ResponseWithValue<ResponseValue>
552+
553+
public struct ResponseValue: Codable {
554+
public var source: String
555+
}
556+
557+
}
558+
544559
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status
545560
public struct Status: Request {
546561
public var pathComponents: [String] { ["status"] }

Sources/WebDriver/Session.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ public class Session {
321321
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
322322
}
323323

324+
/// - Returns: The current page source.
325+
public func source() throws -> String {
326+
let response = try webDriver.send(Requests.SessionSource(session: id))
327+
return response.value.source
328+
}
329+
324330
/// Deletes the current session.
325331
public func delete() throws {
326332
guard shouldDelete else { return }

Tests/UnitTests/APIToRequestMappingTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,14 @@ class APIToRequestMappingTests: XCTestCase {
213213
}
214214
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
215215
}
216+
217+
func testSessionSource() throws {
218+
let mockWebDriver: MockWebDriver = MockWebDriver()
219+
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
220+
221+
mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) {
222+
ResponseWithValue(.init(source: "currentSource"))
223+
}
224+
XCTAssert(try session.source() == "currentSource")
225+
}
216226
}

0 commit comments

Comments
 (0)