Skip to content
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

feat: Orientation method #133

Merged
merged 5 commits into from
Feb 27, 2024
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
2 changes: 1 addition & 1 deletion Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
25 changes: 25 additions & 0 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,31 @@ public enum Requests {
public typealias Response = WebDriverStatus
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidorientation
public enum SessionOrientation {
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
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<ScreenOrientation>
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindow_handle
public struct SessionWindowHandle: Request {
public var session: String
Expand Down
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"
}
tristanlabelle marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 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 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 {
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))
}

/// - 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 {
Expand All @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading