From 141d9ec40a6e974a553bd46bf02c7e2bc0ac6778 Mon Sep 17 00:00:00 2001 From: Brian Harvey Date: Thu, 7 Mar 2024 09:28:36 -0500 Subject: [PATCH] feat: Location method (#132) --- Docs/SupportedAPIs.md | 3 ++- Sources/WebDriver/Location.swift | 12 ++++++++++ Sources/WebDriver/Requests.swift | 22 +++++++++++++++++++ Sources/WebDriver/Session.swift | 19 +++++++++++++++- .../UnitTests/APIToRequestMappingTests.swift | 15 +++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 Sources/WebDriver/Location.swift diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 4de4e51..ebfd848 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -41,7 +41,8 @@ Contributions to expand support to unimplemented functionality are always welcom | POST | `/session/:sessionId/execute_async` | Not Supported| `Session.execute()` | | POST | `/session/:sessionId/forward` | Supported | `Session.forward()` | | POST | `/session/:sessionId/keys` | Supported | `Session.sendKeys()`| -| GET | `/session/:sessionId/location` | Supported | Not implemented | +| POST | `/session/:sessionId/location` | Supported | `Session.setLocation`| +| GET | `/session/:sessionId/location` | Supported | `Session.location`| | POST | `/session/:sessionId/moveto` | Supported | `Session.moveTo()` | | GET | `/session/:sessionId/orientation` | Supported | `Session.orientation`| | POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` | diff --git a/Sources/WebDriver/Location.swift b/Sources/WebDriver/Location.swift new file mode 100644 index 0000000..98cc21c --- /dev/null +++ b/Sources/WebDriver/Location.swift @@ -0,0 +1,12 @@ +public struct Location: Codable, Equatable { + public var latitude: Double + public var longitude: Double + public var altitude: Double + + public init(latitude: Double, longitude: Double, altitude: Double) { + self.latitude = latitude + self.longitude = longitude + self.altitude = altitude + } + +} diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 2f9f776..bc93a29 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -541,6 +541,27 @@ public enum Requests { } } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidlocation + public enum SessionLocation { + public struct Post: Request { + public var session: String + public var location: Location + + public var pathComponents: [String] { ["session", session, "location"] } + public var method: HTTPMethod { .post } + public var body: Location { location } + } + + public struct Get: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "location"] } + public var method: HTTPMethod {.get} + + public typealias Response = ResponseWithValue + } + } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource public struct SessionSource: Request { public var session: String @@ -548,6 +569,7 @@ public enum Requests { public var pathComponents: [String] { ["session", session, "source"] } public var method: HTTPMethod {.get} + public typealias Response = ResponseWithValue } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 6bea0d5..688ffad 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -67,10 +67,18 @@ public class Session { } } + public var location: Location { + get throws { + let response = try webDriver.send(Requests.SessionLocation.Get(session: id)) + return response.value + } + } + public var orientation: ScreenOrientation { get throws { let response = try webDriver.send(Requests.SessionOrientation.Get(session: id)) - return response.value + + return response.value } } @@ -337,6 +345,15 @@ public class Session { } } + /// Set the current geolocation + public func setLocation(_ location: Location) throws { + try webDriver.send(Requests.SessionLocation.Post(session: id, location: location)) + } + + public func setLocation(latitude: Double, longitude: Double, altitude: Double) throws { + try setLocation(Location(latitude: latitude, longitude: longitude, altitude: altitude)) + } + /// - Returns: Array of window handles public var windowHandles: [String] { get throws { diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index bf3e3bf..3181aef 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -246,6 +246,20 @@ class APIToRequestMappingTests: XCTestCase { XCTAssert(try session.window(handle: "myWindow").size == (width: 500, height: 500)) } + func testLocation() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + let location = Location(latitude: 5, longitude: 20, altitude: 2003) + + mockWebDriver.expect(path: "session/mySession/location", method: .post) + try session.setLocation(location) + + mockWebDriver.expect(path: "session/mySession/location", method: .get, type: Requests.SessionLocation.Get.self) { + ResponseWithValue(.init(latitude: 5, longitude: 20, altitude: 2003)) + } + XCTAssert(try session.location == location) + } + func testMaximizeWindow() throws { let mockWebDriver: MockWebDriver = MockWebDriver() let session: Session = Session(webDriver: mockWebDriver, existingId: "mySession") @@ -264,6 +278,7 @@ class APIToRequestMappingTests: XCTestCase { } func testWindowHandles() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession")