Skip to content

Commit

Permalink
feat: Orientation method (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics authored Feb 27, 2024
1 parent 23dacb1 commit eab3069
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
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 {
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"
}
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

0 comments on commit eab3069

Please sign in to comment.