Skip to content

Commit

Permalink
feat: Window session init
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics committed Feb 21, 2024
1 parent 12b8fe8 commit 0d52495
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public enum Requests {
}
}

public struct SessionMaximize: Request {
public struct WindowMaximize: Request {
public var session: String
public var windowHandle: String

Expand Down
35 changes: 2 additions & 33 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,51 +308,20 @@ 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 {
guard shouldDelete else { return }
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() }
Expand Down
20 changes: 8 additions & 12 deletions Sources/WebDriver/Window.swift
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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))
}
}
11 changes: 5 additions & 6 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"))
}
Expand Down

0 comments on commit 0d52495

Please sign in to comment.