Skip to content

Commit

Permalink
fix: Address the pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics committed Feb 23, 2024
1 parent b45eaa4 commit f458dc8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ Contributions to expand support to unimplemented functionality are always welcom
| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | Not implemented |
| GET | `/session/:sessionId/window_handle` | Supported | `Session.windowHandle()`|
| GET | `/session/:sessionId/window_handles` | Supported | `Session.windowHandle()`|
| GET | `/session/:sessionId/window_handles` | Supported | `Session.windowHandles()`|
19 changes: 12 additions & 7 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,22 @@ 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 many: Bool

public var pathComponents: [String] { ["session", session, many ? "window_handles" : "window_handle"] }
public var pathComponents: [String] { ["session", session, "window_handle"] }
public var method: HTTPMethod { .get }

public typealias Response = ResponseWithValue<ResponseValue>
public struct ResponseValue: Codable {
public var windowHandle: String
public var windowHandles: [String]
}
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
15 changes: 8 additions & 7 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,20 @@ class APIToRequestMappingTests: XCTestCase {
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

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

func testWindowHandles() throws {
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.SessionWindowHandle.self) {
ResponseWithValue(.init(windowHandle: "", windowHandles: ["myWindow", "myWindow"]))
mockWebDriver.expect(path: "session/mySession/window_handles", method: .get, type: Requests.SessionWindowHandles.self) {
ResponseWithValue(.init(["myWindow", "myWindow"]))
}
XCTAssert(try session.windowHandle(many: true) == (windowHandle: "", windowHandles: ["myWindow", "myWindow"]))
XCTAssert(try session.windowHandles() == ["myWindow", "myWindow"])
}

func testSessionSource() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
Expand Down

0 comments on commit f458dc8

Please sign in to comment.