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 all 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 | `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