From f4da3708cc1ebf1c82fffeb99a8a3436959d790e Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 12:12:01 -0800 Subject: [PATCH 01/17] feat: Current window position --- Docs/SupportedAPIs.md | 4 +-- Sources/WebDriver/Requests.swift | 33 +++++++++++++++++++ Sources/WebDriver/Session.swift | 15 +++++++++ .../UnitTests/APIToRequestMappingTests.swift | 12 +++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index da49494..ff10edd 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -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 | Not implemented | | GET | `/session/:sessionId/window/:windowHandle/size` | Supported | Not implemented | -| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented | -| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented | +| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.reposition`| +| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.position` | | 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 | diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 04bf41f..1a08bd6 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -469,4 +469,37 @@ public enum Requests { public typealias Response = WebDriverStatus } + + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindowwindowhandleposition + public enum SessionPosition { + public struct Post: Request { + public var session: String + public var windowHandle: String + public var x: Int + 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 + + public var pathComponents: [String] = { ["session" + session, "window", windowHandle, "position"] } + public var method: HTTPMethod = { .get } + + public typealias Response = ResponseWithValue + public struct ResponseValue: Codable { + public var x: Int + public var y: Int + } + } + } } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 90aa5d6..ac6c8ab 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -298,6 +298,21 @@ 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) { + 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) { + try webDriver.send(Requests.SessionPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) + } deinit { do { try delete() } diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 3b4c2cb..50a3f83 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -169,4 +169,16 @@ class APIToRequestMappingTests: XCTestCase { } XCTAssert(try element.enabled == true) } + + func testWindowHandleSize() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) + try session.reposition(window: "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(window: "myWindow") == (x: 9, y: 16)) + } } From 4d1d6e0a3e6c4b4054fe9e9f2963c78887e01ea3 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 15:13:51 -0800 Subject: [PATCH 02/17] feat: Maximize selected window --- Docs/SupportedAPIs.md | 2 +- Sources/WebDriver/Requests.swift | 9 +++++++++ Sources/WebDriver/Session.swift | 5 +++++ Tests/UnitTests/APIToRequestMappingTests.swift | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 09f0a43..ffb8360 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -66,6 +66,6 @@ Contributions to expand support to unimplemented functionality are always welcom | GET | `/session/:sessionId/window/:windowHandle/size` | Supported | `Session.size()` | | POST | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.reposition()`| | GET | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.position()`| -| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | Not implemented | +| 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 | \ No newline at end of file diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index e9e8f25..fd77401 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -564,4 +564,13 @@ public enum Requests { } } } + + public struct SessionMaximize: Request { + public var session: String + public var windowHandle: String + + public var pathComponents: [String] = { ["session", session, "window", windowHandle, "maximize"] } + public var method: HTTPMethod = { .post } + + } } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 0b97236..8bafa64 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -339,6 +339,11 @@ public class Session { 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() } catch let error as ErrorResponse { diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 19fce71..18faae7 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -211,4 +211,11 @@ class APIToRequestMappingTests: XCTestCase { } XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } + + func testMaximizeSelectedWindow() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session: Session = Session(webDriver: WebDriver, existingId: "mySession") + mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) + try session.maximize(window: "myWindow") + } } From 23de5b9025eaf3526b42f9628e39a43daa5f36bc Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 15:22:14 -0800 Subject: [PATCH 03/17] fix: Initialize without equals sign --- Sources/WebDriver/Requests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index fd77401..099a842 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -540,8 +540,8 @@ public enum Requests { public var x: Int public var y: Int - public var pathComponents: [String] = { ["session" + session, "window", windowHandle, "position"] } - public var method: HTTPMethod = { .post } + 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 { @@ -554,8 +554,8 @@ public enum Requests { public var session: String public var windowHandle: String - public var pathComponents: [String] = { ["session" + session, "window", windowHandle, "position"] } - public var method: HTTPMethod = { .get } + public var pathComponents: [String] { ["session" + session, "window", windowHandle, "position"] } + public var method: HTTPMethod { .get } public typealias Response = ResponseWithValue public struct ResponseValue: Codable { @@ -569,8 +569,8 @@ public enum Requests { public var session: String public var windowHandle: String - public var pathComponents: [String] = { ["session", session, "window", windowHandle, "maximize"] } - public var method: HTTPMethod = { .post } + public var pathComponents: [String] { ["session", session, "window", windowHandle, "maximize"] } + public var method: HTTPMethod { .post } } } From 91f3a665df9d1af376dc3f999094db9918813a6c Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 15:53:41 -0800 Subject: [PATCH 04/17] fix: Handle errors --- Sources/WebDriver/Session.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 8bafa64..3d6596e 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -335,7 +335,7 @@ public class Session { /// - 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) { + public func reposition(windowHandle: String, x: Int, y: Int) throws { try webDriver.send(Requests.SessionPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) } From e8346af5e64ba92d31ba728f955144f62d9c54f2 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 16:07:42 -0800 Subject: [PATCH 05/17] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 18faae7..1c25573 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -170,16 +170,16 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try element.enabled == true) } - func testWindowHandleSize() throws { + func testWindowHandlePosition() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) - try session.reposition(window: "myWindow", x: 9, y: 16) + 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(window: "myWindow") == (x: 9, y: 16)) + XCTAssert(try session.position(windowHandle: "myWindow") == (x: 9, y: 16)) } func testSessionTouchScroll() throws { @@ -214,8 +214,8 @@ class APIToRequestMappingTests: XCTestCase { func testMaximizeSelectedWindow() throws { let mockWebDriver: MockWebDriver = MockWebDriver() - let session: Session = Session(webDriver: WebDriver, existingId: "mySession") + let session: Session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) - try session.maximize(window: "myWindow") + try session.maximize(windowHandle: "myWindow") } } From 21e7b26246e56dc4c237dcc9c9d219327913cd9d Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 16:22:43 -0800 Subject: [PATCH 06/17] fix: Failing tests --- Sources/WebDriver/Requests.swift | 4 ++-- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 099a842..9b95e89 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -540,7 +540,7 @@ public enum Requests { public var x: Int public var y: Int - public var pathComponents: [String] { ["session" + session, "window", windowHandle, "position"] } + public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] } public var method: HTTPMethod { .post } public var body: Body { .init(x: x, y: y) } @@ -554,7 +554,7 @@ public enum Requests { public var session: String public var windowHandle: String - public var pathComponents: [String] { ["session" + session, "window", windowHandle, "position"] } + public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] } public var method: HTTPMethod { .get } public typealias Response = ResponseWithValue diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 1c25573..9a52155 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -215,7 +215,7 @@ class APIToRequestMappingTests: XCTestCase { func testMaximizeSelectedWindow() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session: Session = Session(webDriver: mockWebDriver, existingId: "mySession") - mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) + mockWebDriver.expect(path: "session/mySession/window/myWindow/maximize", method: .post) try session.maximize(windowHandle: "myWindow") } } From f702b9c2684aa6fdc4eec8d4cc3ad20081167bb2 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Wed, 7 Feb 2024 19:35:54 -0500 Subject: [PATCH 07/17] fix: Int to float and proper test names --- Sources/WebDriver/Requests.swift | 14 +++++++------- Tests/UnitTests/APIToRequestMappingTests.swift | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index a27b05e..da78f4e 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -550,20 +550,20 @@ public enum Requests { } // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindowwindowhandleposition - public enum SessionPosition { + public enum WindowPosition { public struct Post: Request { public var session: String public var windowHandle: String - public var x: Int - public var y: Int + public var x: Double + public var y: Double 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 var x: Double + public var y: Double } } @@ -576,8 +576,8 @@ public enum Requests { public typealias Response = ResponseWithValue public struct ResponseValue: Codable { - public var x: Int - public var y: Int + public var x: Double + public var y: Double } } } diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index c5dc141..dccc6c9 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -170,7 +170,7 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try element.enabled == true) } - func testWindowHandlePosition() throws { + func testWindowPosition() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .post) @@ -226,7 +226,7 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } - func testMaximizeSelectedWindow() throws { + func testMaximizeWindow() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session: Session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/window/myWindow/maximize", method: .post) From 599c6bf09675ac1a8d83485290f6ddc3e4ad36e9 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Thu, 8 Feb 2024 21:48:08 -0500 Subject: [PATCH 08/17] feat: Init window struct --- Sources/WebDriver/Window.swift | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Sources/WebDriver/Window.swift diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift new file mode 100644 index 0000000..318571f --- /dev/null +++ b/Sources/WebDriver/Window.swift @@ -0,0 +1,47 @@ +public struct Window { + + var webDriver: WebDriver { session.webDriver } + public let session: Session + public let handle: String + + public init(session: Session, handle: String) { + self.session = session + self.handle = handle + } + + public var position: (x: Double, y: Double) { + get throws { + let responseValue = try webDriver.send(Requests.WindowPosition.Get( + session: session.id, windowHandle: handle)).value + return (responseValue.x, responseValue.y) + } + } + + public var size: (width: Double, height: Double) { + get throws { + let responseValue = try webDriver.send(Requests.SessionWindowSize.Get( + session: session.id, windowHandle: handle)).value + return (responseValue.width, responseValue.height) + } + } + + /// - 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) { + 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 setPosition(windowHandle: String, x: Double, y: Double) throws { + 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)) + } +} \ No newline at end of file From ee68f0d5e59f891a7d5cf9e654396ea26c34129c Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Fri, 9 Feb 2024 18:54:27 -0500 Subject: [PATCH 09/17] feat: New Window struct --- Sources/WebDriver/Requests.swift | 14 +++++++------- Sources/WebDriver/Window.swift | 14 +++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index da78f4e..c652afd 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -509,20 +509,20 @@ public enum Requests { } // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindowwindowhandlesize - public enum SessionWindowSize { + public enum WindowSize { public struct Post: Request { public var session: String public var windowHandle: String - public var width: Int - public var height: Int + public var width: Double + public var height: Double public var pathComponents: [String] { ["session", session, "window", windowHandle, "size"] } public var method: HTTPMethod { .post } public var body: Body { .init(width: width, height: height) } public struct Body: Codable { - public var width: Int - public var height: Int + public var width: Double + public var height: Double } } @@ -535,8 +535,8 @@ public enum Requests { public typealias Response = ResponseWithValue public struct ResponseValue: Codable { - public var width: Int - public var height: Int + public var width: Double + public var height: Double } } } diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift index 318571f..e388714 100644 --- a/Sources/WebDriver/Window.swift +++ b/Sources/WebDriver/Window.swift @@ -19,7 +19,7 @@ public struct Window { public var size: (width: Double, height: Double) { get throws { - let responseValue = try webDriver.send(Requests.SessionWindowSize.Get( + let responseValue = try webDriver.send(Requests.WindowSize.Get( session: session.id, windowHandle: handle)).value return (responseValue.width, responseValue.height) } @@ -28,16 +28,24 @@ public struct Window { /// - 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) { - let response = try webDriver.send(Requests.SessionPosition.Get(session: id, windowHandle: windowHandle)) + let response = try webDriver.send(Requests.WindowPosition.Get(session: id, windowHandle: windowHandle)) return (x: response.value.x, y: response.value.y) } + /// - Parameters: + /// - windowHandle: Name of te current window + /// - width: The new window width + /// - height: The new window height + public func setSize(windowHandle: String, width: Double, height: Double) throws { + try webDriver.send(Requests.WindowSize.Post(session: id, windowHandle: windowHandle, width: width, height: height)) + } + /// - 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 setPosition(windowHandle: String, x: Double, y: Double) throws { - try webDriver.send(Requests.SessionPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) + try webDriver.send(Requests.WindowPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) } /// Maximize specific window if :windowHandle is "current" the current window will be maximized From f8836f42b2c12e26a121175f1e2d391ac269340b Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Fri, 9 Feb 2024 19:06:00 -0500 Subject: [PATCH 10/17] fix: Failing tests and missing variables --- Sources/WebDriver/Session.swift | 22 ++++++++-------------- Sources/WebDriver/Window.swift | 11 +++-------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 0762da1..17c93b4 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -308,8 +308,8 @@ public class Session { try webDriver.send(Requests.SessionWindow.Delete(session: id, name: name)) } - public func size(window handle: String) throws -> (width: Int, height: Int) { - let response = try webDriver.send(Requests.SessionWindowSize.Get(session: id, windowHandle: handle)) + public func size(window handle: String) throws -> (width: Double, height: Double) { + let response = try webDriver.send(Requests.WindowSize.Get(session: id, windowHandle: handle)) return (width: response.value.width, height: response.value.height) } @@ -317,14 +317,8 @@ public class Session { /// - Parameter name: URL parameter is "current", the currently active window will be resized. /// - Parameter width: The new window width. /// - Parameter height: The new window height - public func resize(window handle: String, width: Int, height: Int) throws { - try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) - } - - /// - Returns: The current page source. - public func source() throws -> String { - let response = try webDriver.send(Requests.SessionSource(session: id)) - return response.value.source + public func resize(window handle: String, width: Double, height: Double) throws { + try webDriver.send(Requests.WindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) } /// Deletes the current session. @@ -336,8 +330,8 @@ public class Session { /// - 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) { - let response = try webDriver.send(Requests.SessionPosition.Get(session: id, windowHandle: windowHandle)) + public func position(windowHandle: String) throws -> (x: Double, y: Double) { + let response = try webDriver.send(Requests.WindowPosition.Get(session: id, windowHandle: windowHandle)) return (x: response.value.x, y: response.value.y) } @@ -345,8 +339,8 @@ public class Session { /// - 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 { - try webDriver.send(Requests.SessionPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) + public func reposition(windowHandle: String, x: Double, y: Double) throws { + try webDriver.send(Requests.WindowPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) } /// Maximize specific window if :windowHandle is "current" the current window will be maximized diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift index e388714..352dc1a 100644 --- a/Sources/WebDriver/Window.swift +++ b/Sources/WebDriver/Window.swift @@ -3,10 +3,12 @@ public struct Window { var webDriver: WebDriver { session.webDriver } public let session: Session public let handle: String + public let id: String - public init(session: Session, handle: String) { + public init(session: Session, handle: String, id: String) { self.session = session self.handle = handle + self.id = id } public var position: (x: Double, y: Double) { @@ -25,13 +27,6 @@ public struct Window { } } - /// - 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) { - let response = try webDriver.send(Requests.WindowPosition.Get(session: id, windowHandle: windowHandle)) - return (x: response.value.x, y: response.value.y) - } - /// - Parameters: /// - windowHandle: Name of te current window /// - width: The new window width From 26480dd74c00b41d6cfa585d5285cbb0b2207e75 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Fri, 9 Feb 2024 19:11:19 -0500 Subject: [PATCH 11/17] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index ec47719..543afcf 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -176,7 +176,7 @@ class APIToRequestMappingTests: XCTestCase { 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) { + mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .get, type: Requests.WindowPosition.Get.self) { ResponseWithValue(.init(x: 9, y: 16)) } XCTAssert(try session.position(windowHandle: "myWindow") == (x: 9, y: 16)) @@ -220,7 +220,7 @@ class APIToRequestMappingTests: XCTestCase { mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .post) try session.resize(window: "myWindow", width: 500, height: 500) - mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .get, type: Requests.SessionWindowSize.Get.self) { + mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .get, type: Requests.WindowSize.Get.self) { ResponseWithValue(.init(width: 500, height: 500)) } XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) From 12b8fe8b4b18db887d1a3c9aa7b7f7b33b8e04b5 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Fri, 9 Feb 2024 19:20:36 -0500 Subject: [PATCH 12/17] fix: Failing tests --- Sources/WebDriver/Session.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 17c93b4..cfd5770 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -313,6 +313,12 @@ public class Session { return (width: response.value.width, height: response.value.height) } + /// - Returns: The current page source. + public func source() throws -> String { + let response = try webDriver.send(Requests.SessionSource(session: id)) + return response.value.source + } + /// Change the size of the specified window /// - Parameter name: URL parameter is "current", the currently active window will be resized. /// - Parameter width: The new window width. From 0d52495b42f904b08a3a21eb4603ba9e12f1bfb1 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Tue, 20 Feb 2024 19:53:07 -0500 Subject: [PATCH 13/17] feat: Window session init --- Sources/WebDriver/Requests.swift | 2 +- Sources/WebDriver/Session.swift | 35 ++----------------- Sources/WebDriver/Window.swift | 20 +++++------ .../UnitTests/APIToRequestMappingTests.swift | 11 +++--- 4 files changed, 16 insertions(+), 52 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 5936fc5..766420a 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -597,7 +597,7 @@ public enum Requests { } } - public struct SessionMaximize: Request { + public struct WindowMaximize: Request { public var session: String public var windowHandle: String diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index cfd5770..5156f98 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -308,24 +308,13 @@ public class Session { try webDriver.send(Requests.SessionWindow.Delete(session: id, name: name)) } - public func size(window handle: String) throws -> (width: Double, height: Double) { - let response = try webDriver.send(Requests.WindowSize.Get(session: id, windowHandle: handle)) - return (width: response.value.width, height: response.value.height) - } - + public func window(handle: String) throws -> Window { .init(session: self, handle: handle) } + /// - Returns: The current page source. public func source() throws -> String { let response = try webDriver.send(Requests.SessionSource(session: id)) return response.value.source } - - /// Change the size of the specified window - /// - Parameter name: URL parameter is "current", the currently active window will be resized. - /// - Parameter width: The new window width. - /// - Parameter height: The new window height - public func resize(window handle: String, width: Double, height: Double) throws { - try webDriver.send(Requests.WindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) - } /// Deletes the current session. public func delete() throws { @@ -333,26 +322,6 @@ 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: Double, y: Double) { - let response = try webDriver.send(Requests.WindowPosition.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: Double, y: Double) throws { - try webDriver.send(Requests.WindowPosition.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() } diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift index 352dc1a..4af6216 100644 --- a/Sources/WebDriver/Window.swift +++ b/Sources/WebDriver/Window.swift @@ -1,14 +1,12 @@ +/// Exposes window-specific webdriver operations public struct Window { - var webDriver: WebDriver { session.webDriver } public let session: Session public let handle: String - public let id: String - public init(session: Session, handle: String, id: String) { + public init(session: Session, handle: String) { self.session = session self.handle = handle - self.id = id } public var position: (x: Double, y: Double) { @@ -28,23 +26,21 @@ public struct Window { } /// - Parameters: - /// - windowHandle: Name of te current window /// - width: The new window width /// - height: The new window height - public func setSize(windowHandle: String, width: Double, height: Double) throws { - try webDriver.send(Requests.WindowSize.Post(session: id, windowHandle: windowHandle, width: width, height: height)) + public func setSize(width: Double, height: Double) throws { + try webDriver.send(Requests.WindowSize.Post(session: session.id, windowHandle: handle, width: width, height: height)) } /// - 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 setPosition(windowHandle: String, x: Double, y: Double) throws { - try webDriver.send(Requests.WindowPosition.Post(session: id, windowHandle: windowHandle, x: x, y: y)) + public func setPosition(x: Double, y: Double) throws { + try webDriver.send(Requests.WindowPosition.Post(session: session.id, windowHandle: handle, 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)) + public func maximize() throws { + try webDriver.send(Requests.SessionMaximize(session: session.id, windowHandle: handle)) } } \ No newline at end of file diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 543afcf..712e37b 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -174,12 +174,12 @@ class APIToRequestMappingTests: XCTestCase { 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) + try session.window(handle: "myWindow").setPosition(x: 9, y: 16) mockWebDriver.expect(path: "session/mySession/window/myWindow/position", method: .get, type: Requests.WindowPosition.Get.self) { ResponseWithValue(.init(x: 9, y: 16)) } - XCTAssert(try session.position(windowHandle: "myWindow") == (x: 9, y: 16)) + XCTAssert(try session.window(handle: "myWindow").position == (x: 9, y: 16)) } func testSessionScript() throws { @@ -218,25 +218,24 @@ class APIToRequestMappingTests: XCTestCase { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .post) - try session.resize(window: "myWindow", width: 500, height: 500) + try session.window(handle: "myWindow").setSize(width: 500, height: 500) mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .get, type: Requests.WindowSize.Get.self) { ResponseWithValue(.init(width: 500, height: 500)) } - XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) + XCTAssert(try session.window(handle: "myWindow").size == (width: 500, height: 500)) } func testMaximizeWindow() throws { 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") + try session.window(handle: "myWindow").maximize() } func testSessionSource() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") - mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) { ResponseWithValue(.init(source: "currentSource")) } From 67fd6c2f6353247f18de202f2f9a4328237de493 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Tue, 20 Feb 2024 20:39:47 -0500 Subject: [PATCH 14/17] fix: Session to window --- Sources/WebDriver/Window.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift index 4af6216..15ff57a 100644 --- a/Sources/WebDriver/Window.swift +++ b/Sources/WebDriver/Window.swift @@ -41,6 +41,6 @@ public struct Window { /// Maximize specific window if :windowHandle is "current" the current window will be maximized public func maximize() throws { - try webDriver.send(Requests.SessionMaximize(session: session.id, windowHandle: handle)) + try webDriver.send(Requests.WindowMaximize(session: session.id, windowHandle: handle)) } } \ No newline at end of file From 133dfdca8458f9b23054b3551ac85fe99b1f0116 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Wed, 21 Feb 2024 11:29:29 -0500 Subject: [PATCH 15/17] docs: Update window usage --- Docs/SupportedAPIs.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 2824da6..4823a8e 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -62,12 +62,10 @@ Contributions to expand support to unimplemented functionality are always welcom | DELETE | `/session/:sessionId/window` | Supported | `Session.close()` | | POST | `/session/:sessionId/window` | Supported | `Session.focus()` | | POST | `/session/:sessionId/window/maximize` | Supported | Not implemented | -| POST | `/session/:sessionId/window/size` | Supported | Not implemented | -| 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 | `Session.reposition()`| -| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | `Session.position()`| -| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | `Session.maximize()`| +| POST | `/session/:sessionId/window/:windowHandle/size` | Supported | `Window.setSize()` | +| GET | `/session/:sessionId/window/:windowHandle/size` | Supported | `Window.size` | +| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | `Window.setPosition()`| +| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | `Window.position`| +| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | `Window.maximize()`| | GET | `/session/:sessionId/window_handle` | Supported | Not implemented | | GET | `/session/:sessionId/window_handles` | Supported | Not implemented | From 4a336fe296b4e2fd00aeaaa239df25bc98858849 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Mar 2024 07:10:38 -0800 Subject: [PATCH 16/17] Update Sources/WebDriver/Requests.swift --- Sources/WebDriver/Requests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 7dd6433..c1855b3 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -605,7 +605,6 @@ public enum Requests { public struct Get: Request { public var session: String - public var windowHandle: String public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] } public var method: HTTPMethod { .get } From 563fd7c5a4edd6d1446a4b848f4276daef6772a1 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Mon, 4 Mar 2024 17:03:47 -0500 Subject: [PATCH 17/17] fix: Incorrect window handle removed --- Sources/WebDriver/Requests.swift | 2 +- Sources/WebDriver/Window.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index c1855b3..2f9f776 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -576,7 +576,6 @@ public enum Requests { public struct Get: Request { public var session: String - public var windowHandle: String public var pathComponents: [String] { ["session", session, "orientation"] } public var method: HTTPMethod { .get } @@ -605,6 +604,7 @@ public enum Requests { public struct Get: Request { public var session: String + public var windowHandle: String public var pathComponents: [String] { ["session", session, "window", windowHandle, "position"] } public var method: HTTPMethod { .get } diff --git a/Sources/WebDriver/Window.swift b/Sources/WebDriver/Window.swift index 15ff57a..f598c35 100644 --- a/Sources/WebDriver/Window.swift +++ b/Sources/WebDriver/Window.swift @@ -43,4 +43,4 @@ public struct Window { public func maximize() throws { try webDriver.send(Requests.WindowMaximize(session: session.id, windowHandle: handle)) } -} \ No newline at end of file +}