Skip to content

Commit

Permalink
feat: Location method (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics authored Mar 7, 2024
1 parent 8626746 commit 141d9ec
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()` |
Expand Down
12 changes: 12 additions & 0 deletions Sources/WebDriver/Location.swift
Original file line number Diff line number Diff line change
@@ -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
}

}
22 changes: 22 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,35 @@ 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<Location>
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource
public struct SessionSource: Request {
public var session: String

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


public typealias Response = ResponseWithValue<String>
}

Expand Down
19 changes: 18 additions & 1 deletion Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -264,6 +278,7 @@ class APIToRequestMappingTests: XCTestCase {
}

func testWindowHandles() throws {

let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

Expand Down

0 comments on commit 141d9ec

Please sign in to comment.