diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 7e45a35..20947af 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -43,7 +43,7 @@ Contributions to expand support to unimplemented functionality are always welcom | POST | `/session/:sessionId/keys` | Supported | `Session.sendKeys()`| | GET | `/session/:sessionId/location` | Supported | Not implemented | | POST | `/session/:sessionId/moveto` | Supported | `Session.moveTo()` | -| GET | `/session/:sessionId/orientation` | Supported | Not implemented | +| GET | `/session/:sessionId/orientation` | Supported | `Session.orientation`| | POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` | | GET | `/session/:sessionId/screenshot` | Supported | `Session.screenshot()`| | GET | `/session/:sessionId/source` | Supported | `Session.source` | diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index e656c1f..5413e5b 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -559,6 +559,31 @@ public enum Requests { public typealias Response = WebDriverStatus } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidorientation + public enum SessionOrientation { + public struct Post: Request { + public var session: String + public var orientation: ScreenOrientation + + public var pathComponents: [String] { ["session", session, "orientation"] } + public var method: HTTPMethod { .post } + public var body: Body { .init(orientation: orientation) } + + public struct Body: Codable { + public var orientation: ScreenOrientation + } + } + + public struct Get: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "orientation"] } + public var method: HTTPMethod { .get } + + public typealias Response = ResponseWithValue + } + } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindow_handle public struct SessionWindowHandle: Request { public var session: String diff --git a/Sources/WebDriver/ScreenOrientation.swift b/Sources/WebDriver/ScreenOrientation.swift new file mode 100644 index 0000000..47fd797 --- /dev/null +++ b/Sources/WebDriver/ScreenOrientation.swift @@ -0,0 +1,5 @@ +// Screen orientations +public enum ScreenOrientation: String, Codable { + case portrait = "PORTRAIT" + case landscape = "LANDSCAPE" +} \ No newline at end of file diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 6c6ebd6..6d6fcbe 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -66,6 +66,13 @@ public class Session { } } } + + public var orientation: ScreenOrientation { + get throws { + let response = try webDriver.send(Requests.SessionOrientation.Get(session: id)) + return response.value + } + } /// Sets a a timeout value on this session. public func setTimeout(type: String, duration: TimeInterval) throws { @@ -321,6 +328,11 @@ public class Session { try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) } + /// - Prarmeter: Orientation the window will flip to {LANDSCAPE|PORTRAIT} + public func setOrientation(_ value: ScreenOrientation) throws { + try webDriver.send(Requests.SessionOrientation.Post(session: id, orientation: value)) + } + /// Get the current page source public var source: String { get throws { @@ -345,6 +357,7 @@ public class Session { } /// Deletes the current session. + public func delete() throws { guard shouldDelete else { return } try webDriver.send(Requests.SessionDelete(session: id)) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 48cc899..6d75cf4 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -97,6 +97,26 @@ class APIToRequestMappingTests: XCTestCase { } try session.buttonUp(button: .right) } + + func testSessionOrientation() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + mockWebDriver.expect(path: "session/mySession/orientation", method: .post) + try session.setOrientation(.portrait) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { + ResponseWithValue(.portrait) + } + XCTAssert(try session.orientation == .portrait) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .post) + try session.setOrientation(.landscape) + + mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) { + ResponseWithValue(.landscape) + } + XCTAssert(try session.orientation == .landscape) + } func testSendKeys() throws { let mockWebDriver = MockWebDriver()