Skip to content

Commit

Permalink
Merge branch 'main' into squid/orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Feb 27, 2024
2 parents 443f3af + 23dacb1 commit 2ca1606
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ Contributions to expand support to unimplemented functionality are always welcom
| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented |
| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | Not implemented |
| GET | `/session/:sessionId/window_handle` | Supported | Not implemented |
| GET | `/session/:sessionId/window_handles` | Supported | Not implemented |
| GET | `/session/:sessionId/window_handle` | Supported | `Session.windowHandle`|
| GET | `/session/:sessionId/window_handles` | Supported | `Session.windowHandles`|
26 changes: 20 additions & 6 deletions Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,7 @@ public enum Requests {
public var pathComponents: [String] { ["session", session, "source"] }
public var method: HTTPMethod {.get}

public typealias Response = ResponseWithValue<ResponseValue>

public struct ResponseValue: Codable {
public var source: String
}

public typealias Response = ResponseWithValue<String>
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status
Expand Down Expand Up @@ -588,4 +583,23 @@ public enum Requests {
public typealias Response = ResponseWithValue<ScreenOrientation>
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindow_handle
public struct SessionWindowHandle: Request {
public var session: String

public var pathComponents: [String] { ["session", session, "window_handle"] }
public var method: HTTPMethod { .get }

public typealias Response = ResponseWithValue<String>
}

public struct SessionWindowHandles: Request {
public var session: String

public var pathComponents: [String] { ["session", session, "window_handles"] }
public var method: HTTPMethod { .get }

public typealias Response = ResponseWithValue<Array<String>>
}
}
25 changes: 21 additions & 4 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,27 @@ public class Session {
try webDriver.send(Requests.SessionOrientation.Post(session: id, orientation: value))
}

/// - Returns: The current page source.
public func source() throws -> String {
let response = try webDriver.send(Requests.SessionSource(session: id))
return response.value.source
/// Get the current page source
public var source: String {
get throws {
try webDriver.send(Requests.SessionSource(session: id)).value
}
}

/// - Returns: Current window handle
public var windowHandle: String {
get throws {
let response = try webDriver.send(Requests.SessionWindowHandle(session: id))
return response.value
}
}

/// - Returns: Array of window handles
public var windowHandles: [String] {
get throws {
let response = try webDriver.send(Requests.SessionWindowHandles(session: id))
return response.value
}
}

/// Deletes the current session.
Expand Down
50 changes: 34 additions & 16 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 Expand Up @@ -214,35 +234,33 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}


func testWindowOrientation() throws {
func testWindowHandle() 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)
mockWebDriver.expect(path: "session/mySession/window_handle", method: .get, type: Requests.SessionWindowHandle.self) {
ResponseWithValue(.init("myWindow"))
}
XCTAssert(try session.orientation == .portrait)

mockWebDriver.expect(path: "session/mySession/orientation", method: .post)
try session.setOrientation(.landscape)
XCTAssert(try session.windowHandle == "myWindow")
}

mockWebDriver.expect(path: "session/mySession/orientation", method: .get, type: Requests.SessionOrientation.Get.self) {
ResponseWithValue(.landscape)
func testWindowHandles() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

mockWebDriver.expect(path: "session/mySession/window_handles", method: .get, type: Requests.SessionWindowHandles.self) {
ResponseWithValue(.init(["myWindow", "myWindow"]))
}
XCTAssert(try session.orientation == .landscape)
XCTAssert(try session.windowHandles == ["myWindow", "myWindow"])
}


func testSessionSource() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) {
ResponseWithValue(.init(source: "currentSource"))
ResponseWithValue("currentSource")
}
XCTAssert(try session.source() == "currentSource")
XCTAssert(try session.source == "currentSource")
}
}

0 comments on commit 2ca1606

Please sign in to comment.