Skip to content
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
2 changes: 1 addition & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 | Not implemented |
Expand Down
28 changes: 28 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,32 @@ 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<ResponseValue>
public struct ResponseValue: Codable {
public var screenOrientation: String
}
}
}
}
5 changes: 5 additions & 0 deletions Sources/WebDriver/ScreenOrientation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Screen orientations
public enum ScreenOrientation: String, Codable {
case portrait = "PORTRAIT"
case landscape = "LANDSCAPE"
}
17 changes: 16 additions & 1 deletion Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,22 @@ public class Session {
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
}

/// Deletes the current session.
/// - Parameter orientation: Orientation the window will flip to {LANDSCAPE|PORTRAIT}
public func portraitOrientation(orientation: ScreenOrientation = .portrait) throws {
try webDriver.send(Requests.SessionOrientation.Post(session: id, orientation: orientation))
}

/// - Parameter orientation: Orientation the window will flip to {LANDSCAPE|PORTRAIT}
public func landscapeOrientation(orientation: ScreenOrientation = .landscape) throws {
try webDriver.send(Requests.SessionOrientation.Post(session: id, orientation: orientation))
}

/// - Parameter orientation: Orientation the window is oriented to {LANDSCAPE|PORTRAIT}
public func orientation() throws -> String {
let response = try webDriver.send(Requests.SessionOrientation.Get(session: id))
return response.value.screenOrientation
}

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

func testWindowOrientation() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
mockWebDriver.expect(path: "session/mySession/orientation", method: .post)
try session.portraitOrientation()

mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) {
ResponseWithValue(.init(screenOrientation: "PORTRAIT"))
}
XCTAssert(try session.orientation() == "PORTRAIT")

mockWebDriver.expect(path: "session/mySession/orientation", method: .post)
try session.landscapeOrientation()

mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) {
ResponseWithValue(.init(screenOrientation: "LANDSCAPE"))
}
XCTAssert(try session.orientation() == "LANDSCAPE")
}

}