Skip to content

Commit

Permalink
feat: Return one or many window handles (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics authored Feb 26, 2024
1 parent 0e62310 commit 4d3408c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`|
19 changes: 19 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,23 @@ public enum Requests {

public typealias Response = WebDriverStatus
}

// 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<String>
}

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<Array<String>>
}
}
12 changes: 12 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ public class Session {
let response = try webDriver.send(Requests.SessionSource(session: id))
return response.value.source
}

/// - Returns: Current window handle
public func windowHandle() throws -> String {
let response = try webDriver.send(Requests.SessionWindowHandle(session: id))
return response.value
}

/// - Returns: Array of window handles
public func windowHandles() throws -> [String] {
let response = try webDriver.send(Requests.SessionWindowHandles(session: id))
return response.value
}

/// Deletes the current session.
public func delete() throws {
Expand Down
20 changes: 20 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}

func testWindowHandle() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

mockWebDriver.expect(path: "session/mySession/window_handle", method: .get, type: Requests.SessionWindowHandle.self) {
ResponseWithValue(.init("myWindow"))
}
XCTAssert(try session.windowHandle() == "myWindow")
}

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.windowHandles() == ["myWindow", "myWindow"])
}

func testSessionSource() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
Expand Down

0 comments on commit 4d3408c

Please sign in to comment.