Skip to content

Commit

Permalink
feat: Source method (#134)
Browse files Browse the repository at this point in the history
Implements method:
`/session/:sessionId/source`
  • Loading branch information
Squidonomics authored Feb 7, 2024
1 parent b84c7a2 commit 0e62310
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`|
Expand Down
15 changes: 15 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseValue>

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"] }
Expand Down
6 changes: 6 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 0e62310

Please sign in to comment.