Skip to content

Commit

Permalink
feat: Address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics committed Feb 23, 2024
1 parent cc306c2 commit 55a9e21
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
16 changes: 16 additions & 0 deletions Sources/WebDriver/Location.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public struct Location: Codable {
public var latitude: Double
public var longitude: Double
public var altitude: Float

init(latitude: Double, longitude: Double, altitude: Float) {
self.latitude = latitude
self.longitude = longitude
self.altitude = altitude
}

}

extension Location: Equatable {

}
29 changes: 9 additions & 20 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -542,38 +542,27 @@ public enum Requests {
}

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

public var pathComponents: [String] {["session", session, "location"]}
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 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 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
}
public typealias Response = ResponseWithValue<Location>
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource
public struct SessionSource: Request {
Expand Down
15 changes: 9 additions & 6 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 Location(latitude: response.value.latitude, longitude: response.value.longitude, altitude: response.value.altitude)
}
}

/// Sets a a timeout value on this session.
public func setTimeout(type: String, duration: TimeInterval) throws {
Expand Down Expand Up @@ -325,14 +332,10 @@ public class Session {
/// - 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 setLocation(latitude: Double, longitude: Double, altitude: Float) throws {
try webDriver.send(Requests.SessionLocation.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)

/// - Returns: The current page source.
public func source() throws -> String {
let response = try webDriver.send(Requests.SessionSource(session: id))
Expand Down
10 changes: 6 additions & 4 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,19 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}

func testGeoLocation() throws {
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)

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

func testSessionSource() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
Expand Down

0 comments on commit 55a9e21

Please sign in to comment.