Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Return one or many window handles #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
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
Loading