From 42920a81da2cd5fd3e9d055a719b34acf60348db Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 21:32:56 -0800 Subject: [PATCH 1/7] feat: Source method --- Docs/SupportedAPIs.md | 2 +- Sources/WebDriver/Requests.swift | 15 +++++++++++++++ Sources/WebDriver/Session.swift | 6 ++++++ Tests/UnitTests/APIToRequestMappingTests.swift | 10 ++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Docs/SupportedAPIs.md b/Docs/SupportedAPIs.md index 1e6c987..7467dcf 100644 --- a/Docs/SupportedAPIs.md +++ b/Docs/SupportedAPIs.md @@ -44,7 +44,7 @@ Contributions to expand support to unimplemented functionality are always welcom | GET | `/session/:sessionId/orientation` | Supported | Not implemented | | POST | `/session/:sessionId/refresh` | Not supported| `Session.refresh()` | | GET | `/session/:sessionId/screenshot` | Supported | `Session.screenshot()`| -| GET | `/session/:sessionId/source` | Supported | Not implemented | +| GET | `/session/:sessionId/source` | Supported | `Session.source` | | POST | `/session/:sessionId/timeouts` | Supported | `Session.setTimeout()`| | GET | `/session/:sessionId/title` | Supported | `Session.title` | | POST | `/session/:sessionId/touch/click` | Supported | `Element.touchClick()`| diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 0351bc0..8758e3c 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -524,6 +524,21 @@ public enum Requests { } } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidsource + public struct SessionSource: Request { + public var session: String + + public var pathComponents: [String] { ["session", session, "source"] } + public var method: HTTPMethod {.get} + + public typealias Response = ResponseWithValue + + public struct ResponseWithValue: Codable { + public var source: String + } + + } + // https://www.selenium.dev/documentation/legacy/json_wire_protocol/#status public struct Status: Request { public var pathComponents: [String] { ["status"] } diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 22bd368..82faa85 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -317,6 +317,12 @@ public class Session { try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height)) } + /// - Returns: The current page source. + public func source() throws -> String { + let response = try webDriver.send(Request.SessionSource(session: id)) + return response.value.source + } + /// Deletes the current session. public func delete() throws { guard shouldDelete else { return } diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index a674dd4..78af201 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -199,4 +199,14 @@ class APIToRequestMappingTests: XCTestCase { } XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500)) } + + func testSessionSource() throws { + let mockWebDriver: MockWebDriver = MockWebDriver() + let session = Session(webDriver: mockWebDriver, existingId: "mySession") + + mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionWindowSize.Get.self) { + ResponseWithValue(.init(source: "currentSource")) + } + XCTAssert(try session.source() == "currentSource") + } } From d8eed4bdf1efdc9de914943458419917f4f36fa4 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 22:30:34 -0800 Subject: [PATCH 2/7] fix: Failing tests --- Sources/WebDriver/Requests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WebDriver/Requests.swift b/Sources/WebDriver/Requests.swift index 8758e3c..389984f 100644 --- a/Sources/WebDriver/Requests.swift +++ b/Sources/WebDriver/Requests.swift @@ -533,7 +533,7 @@ public enum Requests { public typealias Response = ResponseWithValue - public struct ResponseWithValue: Codable { + public struct ResponseValue: Codable { public var source: String } From 550046c7197c7c520bcc12cb53e088578792d2bc Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 22:37:07 -0800 Subject: [PATCH 3/7] fix: Failing tests --- Sources/WebDriver/Session.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 82faa85..97f71e2 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -319,7 +319,7 @@ public class Session { /// - Returns: The current page source. public func source() throws -> String { - let response = try webDriver.send(Request.SessionSource(session: id)) + let response = try webDriver.send(Requests.SessionSource(session: id)) return response.value.source } From d574ff6f00f01b40c72090524bc2de838c16316d Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 22:42:24 -0800 Subject: [PATCH 4/7] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 78af201..9060bd2 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -205,7 +205,7 @@ class APIToRequestMappingTests: XCTestCase { let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionWindowSize.Get.self) { - ResponseWithValue(.init(source: "currentSource")) + ResponseWithValue(.init("currentSource")) } XCTAssert(try session.source() == "currentSource") } From 18f79c9a8ec681ebfd5868ea21c42799610b2061 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 22:50:19 -0800 Subject: [PATCH 5/7] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 9060bd2..cd85989 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -204,7 +204,7 @@ class APIToRequestMappingTests: XCTestCase { let mockWebDriver: MockWebDriver = MockWebDriver() let session = Session(webDriver: mockWebDriver, existingId: "mySession") - mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionWindowSize.Get.self) { + mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) { ResponseWithValue(.init("currentSource")) } XCTAssert(try session.source() == "currentSource") From a706e58221c1cd345ed242d3cc976b5940200e10 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 22:54:48 -0800 Subject: [PATCH 6/7] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index cd85989..dc91ccc 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -205,7 +205,7 @@ class APIToRequestMappingTests: XCTestCase { let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) { - ResponseWithValue(.init("currentSource")) + ResponseWithValue(.init(sources: "currentSource")) } XCTAssert(try session.source() == "currentSource") } From 73739fd4e044587094185fa4b2b8873150145bc7 Mon Sep 17 00:00:00 2001 From: Squidonomics Date: Sun, 3 Dec 2023 23:05:34 -0800 Subject: [PATCH 7/7] fix: Failing tests --- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index dc91ccc..3d0fe8f 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -205,7 +205,7 @@ class APIToRequestMappingTests: XCTestCase { let session = Session(webDriver: mockWebDriver, existingId: "mySession") mockWebDriver.expect(path: "session/mySession/source", method: .get, type: Requests.SessionSource.self) { - ResponseWithValue(.init(sources: "currentSource")) + ResponseWithValue(.init(source: "currentSource")) } XCTAssert(try session.source() == "currentSource") }