From 0d52495b42f904b08a3a21eb4603ba9e12f1bfb1 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Tue, 20 Feb 2024 19:53:07 -0500 Subject: [PATCH] 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")) }