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 5 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 @@ -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 | Not implemented |
| 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
}

}
25 changes: 25 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,30 @@ 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 latitude: Double
public var longitude: Double
public var altitude: Double
public var location: Location

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

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
Expand All @@ -554,6 +578,7 @@ public enum Requests {
public var source: String
}


}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status
Expand Down
12 changes: 12 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public class Session {
}
}
}

public var location: Location {
get throws {
let response = try webDriver.send(Requests.SessionLocation.Get(session: id))
return response.value
}
}

/// Sets a a timeout value on this session.
public func setTimeout(type: String, duration: TimeInterval) throws {
Expand Down Expand Up @@ -321,6 +328,11 @@ public class Session {
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
}

/// Set the current geolocation
public func setLocation(latitude: Double, longitude: Double, altitude: Double, location: Location) throws {
try webDriver.send(Requests.SessionLocation.Post(session: id, latitude: latitude, longitude: longitude, altitude: altitude, location: location))
}

/// - Returns: The current page source.
public func source() throws -> String {
let response = try webDriver.send(Requests.SessionSource(session: id))
Expand Down
15 changes: 15 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssert(try session.size(window: "myWindow") == (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(latitude: 5, longitude: 20, altitude: 2003, location: 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 testSessionSource() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
Expand All @@ -222,5 +236,6 @@ class APIToRequestMappingTests: XCTestCase {
ResponseWithValue(.init(source: "currentSource"))
}
XCTAssert(try session.source() == "currentSource")

}
}