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 2 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 @@ -67,5 +67,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.windowHandle()`|
14 changes: 14 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,18 @@ public enum Requests {

public typealias Response = WebDriverStatus
}

public struct SessionWindowHandle: Request {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public var session: String
public var many: Bool

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

public typealias Response = ResponseWithValue<ResponseValue>
public struct ResponseValue: Codable {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public var windowHandle: String
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public var windowHandles: [String]
}
}
}
8 changes: 8 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ public class Session {
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
}

///
/// - Parameter many: Bool value that will return multiple window handles
/// - Returns: either current window handle or many based on bool flag
public func windowHandle(many: Bool) throws -> (windowHandle: String, windowHandles: [String]) {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
let response = try webDriver.send(Requests.SessionWindowHandle(session: id, many: many))
return (windowHandle: response.value.windowHandle, windowHandles: response.value.windowHandles)
}

/// Deletes the current session.
public func delete() throws {
guard shouldDelete else { return }
Expand Down
20 changes: 20 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,24 @@ 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(windowHandle: "myWindow", windowHandles: []))
}
XCTAssert(try session.windowHandle(many: false) == (windowHandle: "myWindow", windowHandles: []))
}

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"]))
}
XCTAssert(try session.windowHandle(many: true) == (windowHandle: "", windowHandles: ["myWindow", "myWindow"]))
}
}