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: Current window position #130

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 3 additions & 3 deletions Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ Contributions to expand support to unimplemented functionality are always welcom
| GET | `/session/:sessionId/window/size` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/size` | Supported | `Session.resize()` |
| GET | `/session/:sessionId/window/:windowHandle/size` | Supported | `Session.size()` |
| 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 |
| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.reposition()`|
Squidonomics marked this conversation as resolved.
Show resolved Hide resolved
| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.position()`|
| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | `Session.maximize()`|
| GET | `/session/:sessionId/window_handle` | Supported | Not implemented |
| GET | `/session/:sessionId/window_handles` | Supported | Not implemented |
42 changes: 42 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,46 @@ public enum Requests {

public typealias Response = WebDriverStatus
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindowwindowhandleposition
public enum SessionPosition {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public struct Post: Request {
public var session: String
public var windowHandle: String
public var x: Int
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public var y: Int

public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] }
public var method: HTTPMethod { .post }
public var body: Body { .init(x: x, y: y) }

public struct Body: Codable {
public var x: Int
public var y: Int
}
}

public struct Get: Request {
public var session: String
public var windowHandle: String
compnerd marked this conversation as resolved.
Show resolved Hide resolved

public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] }
public var method: HTTPMethod { .get }

public typealias Response = ResponseWithValue<ResponseValue>
public struct ResponseValue: Codable {
public var x: Int
public var y: Int
}
}
}

public struct SessionMaximize: Request {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
public var session: String
public var windowHandle: String

public var pathComponents: [String] { ["session", session, "window", windowHandle, "maximize"] }
public var method: HTTPMethod { .post }

}
}
20 changes: 20 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ public class Session {
try webDriver.send(Requests.SessionDelete(session: id))
shouldDelete = false
}

/// - Parameter windowHandle: Name of current window
/// - Returns: Current window position in form of {x,y} where x and y are the upper left corner of the screen
public func position(windowHandle: String) throws -> (x: Int, y: Int) {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
let response = try webDriver.send(Requests.SessionPosition.Get(session: id, windowHandle: windowHandle))
return (x: response.value.x, y: response.value.y)
}

/// - Parameters:
/// - windowHandle: Name of current window
/// - x: Position in the top left corner of the x coordinate
/// - y: Position in the top left corner of the y coordinate
public func reposition(windowHandle: String, x: Int, y: Int) throws {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
try webDriver.send(Requests.SessionPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y))
}

/// Maximize specific window if :windowHandle is "current" the current window will be maximized
public func maximize(windowHandle: String) throws {
try webDriver.send(Requests.SessionMaximize(session: id, windowHandle: windowHandle))
}

deinit {
do { try delete() }
Expand Down
19 changes: 19 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssert(try element.enabled == true)
}

func testWindowHandlePosition() throws {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post)
try session.reposition(windowHandle: "myWindow", x: 9, y: 16)

mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .get, type: Requests.SessionPosition.Get.self) {
ResponseWithValue(.init(x: 9, y: 16))
}
XCTAssert(try session.position(windowHandle: "myWindow") == (x: 9, y: 16))
}

func testSessionTouchScroll() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
Expand Down Expand Up @@ -199,4 +211,11 @@ class APIToRequestMappingTests: XCTestCase {
}
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}

func testMaximizeSelectedWindow() throws {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
let mockWebDriver: MockWebDriver = MockWebDriver()
let session: Session = Session(webDriver: mockWebDriver, existingId: "mySession")
mockWebDriver.expect(path: "session/mySession/window/myWindow/maximize", method: .post)
try session.maximize(windowHandle: "myWindow")
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
}
}