Skip to content

feat: Location method #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Contributions to expand support to unimplemented functionality are always welcom
| POST | `/session/:sessionId/element/:id/value` | Supported | `Element.sendKeys()`|
| 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.getLocation`|
| POST | `/session/:sessionId/moveto` | Supported | `Session.moveTo()` |
| GET | `/session/:sessionId/orientation` | Supported | Not implemented |
| POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` |
Expand Down
35 changes: 35 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,41 @@ public enum Requests {
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidlocation
public enum SessionGeoLocation {
public struct Post: Request {
public var session: String
public var latitude: Int
public var longitude: Int
public var altitude: Int

public var pathComponents: [String] {["session", session, "location"]}
public var method: HTTPMethod { .post }
public var body: Body {.init(latitude: latitude, longitude: longitude, altitude: altitude)}

public struct Body: Codable {
public var latitude: Int
public var longitude: Int
public var altitude: Int
}
}

public struct Get: Request {
public var session: String

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

public typealias Response = ResponseWithValue<ResponseValue>

public struct ResponseValue: Codable {
public var latitude: Int
public var longitude: Int
public var altitude: Int
}
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status
public struct Status: Request {
public var pathComponents: [String] { ["status"] }
Expand Down
13 changes: 13 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ public class Session {
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
}

/// - Parameters:
/// - latitude: Number coordinate of current geo location
/// - longitude: Number coordinate of current geo location
/// - altitude: Number coordinate of current geo location
public func setLocation(latitude: Int, longitude: Int, altitude: Int) throws {
try webDriver.send(Requests.SessionGeoLocation.Post(session: id, latitude: latitude, longitude: longitude, altitude: altitude))
}

public func getLocation() throws -> (latitude: Int, longitude: Int, altitude: Int) {
let response = try webDriver.send(Requests.SessionGeoLocation.Get(session: id))
return (latitude: response.value.latitude, longitude: response.value.longitude, altitude: response.value.altitude)
}

/// Deletes the current session.
public func delete() throws {
guard shouldDelete else { return }
Expand Down
13 changes: 13 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,17 @@ class APIToRequestMappingTests: XCTestCase {
}
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}

func testGeoLocation() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

mockWebDriver.expect(path: "session/mySession/location", method: .post)
try session.setLocation(latitude: 5, longitude: 20, altitude: 2003)

mockWebDriver.expect(path: "session/mySession/location", method: .get, type: Requests.SessionGeoLocation.Get.self) {
ResponseWithValue(.init(latitude: 5, longitude: 20, altitude: 2003))
}
XCTAssert(try session.getLocation() == (latitude: 5, longitude: 20, altitude: 2003))
}
}