Skip to content

Commit 2b80410

Browse files
Fixed All SwiftLint Linting Warnings & Errors (#60)
* fix: fixed some linting errors * fix: fixed some linting errors * fix: fixed some linting errors * docs(Element): added documentation for all exposed public symbols * docs(Element,ElementRect): added documentation * docs(APIClient): added docs for public symbols * docs(ElementTypes): added documents for public symbols * docs(PostElementDoubleClickRequest): added documentation for public symbols * docs(DevToolTypes): added documentation for namespace * docs(AnyEncodable): documentation added * refactor(PostExecuteRequest): refactored to only one struct, added documentation * docs(ChromeDriverElementDragAndDropper): added documentation, fixed linting errors * docs(Element): documented find element protocol * refactor(PostElementDragAndDropRequest): refactored body property getter method * docs(PostElementDragAndDropRequest): added documentation * docs(SeleniumError): added documentation * docs(WebDriver): added documentation * docs(ActionsPayload): added documentation * fix(ActionsPayload): fixed property names too short * docs(GetElementRectRequest): added documentation, fixed no implicit access control levels set * fix: fixed all linting errors * feat(package.json): added test script to start up all required integration test services, build package and run tests Also added `npm run compose:up:integration-services` script to startup and wait for docker-compose services required for running integration tests to be healthy.
1 parent 6013367 commit 2b80410

25 files changed

+885
-267
lines changed

Example/Sources/SeleniumSwiftExample/main.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
// main.swift
1+
// Main.swift
22
// Copyright (c) 2025 GetAutomaApp
33
// All source code and related assets are the property of GetAutomaApp.
44
// All rights reserved.
5-
//
6-
// This package is freely distributable under the MIT license.
7-
// This Package is a modified fork of https://github.com/ashi-psn/SwiftWebDriver.
85

96
import SwiftWebDriver
107

118
internal enum Main {
9+
/// Executable main procedure for this example package
1210
public static func main() async throws {
1311
let chromeOption = ChromeOptions(
1412
args: [

Sources/SwiftWebDriver/API/APIClient.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Copyright (c) 2025 GetAutomaApp
33
// All source code and related assets are the property of GetAutomaApp.
44
// All rights reserved.
5-
//
6-
// This package is freely distributable under the MIT license.
7-
// This Package is a modified fork of https://github.com/ashi-psn/SwiftWebDriver.
85

96
import AsyncHTTPClient
107
import Foundation
@@ -25,14 +22,33 @@ internal enum APIError: Error {
2522
internal struct APIClient {
2623
private let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
2724

25+
/// The shared singleton instance of `APIClient`.
26+
///
27+
/// Use this when you need a shared HTTP client for sending requests to
28+
/// the WebDriver server or other API endpoints without creating a new
29+
/// instance each time.
2830
public static let shared = Self()
2931

30-
/// Request send To API and Parse Codable Models
31-
/// - Parameter request: RequestType
32-
/// - Returns: EventLoopFuture<RequestType.Response>
32+
/// Sends a request to the API and decodes the response into a `Codable` model.
33+
///
34+
/// This method executes the given `RequestType` using the underlying
35+
/// `AsyncHTTPClient` and decodes the result into the associated `Response`
36+
/// type declared by the request. If the server responds with a non-OK
37+
/// status code, the response is checked for a `SeleniumError` and thrown
38+
/// if present; otherwise, an `APIError.responseStatsFailed` is thrown.
39+
///
40+
/// - Parameter request: The request to send. Must conform to `RequestType`
41+
/// and have a `Codable` response type.
42+
/// - Returns: An `EventLoopFuture` that will succeed with the decoded
43+
/// response object or fail with an error.
44+
///
45+
/// - Throws:
46+
/// - `SeleniumError` if the server returns a known WebDriver error.
47+
/// - `APIError.responseStatsFailed` if the status code indicates failure.
48+
/// - `APIError.responseBodyIsNil` if no response body is returned.
49+
/// - Any `DecodingError` if the response cannot be decoded.
3350
public func request<R>(_ request: R) -> EventLoopFuture<R.Response> where R: RequestType {
3451
httpClient.execute(request: request).flatMapResult { response -> Result<R.Response, Error> in
35-
3652
guard response.status == .ok else {
3753
if
3854
let buffer = response.body,
@@ -58,9 +74,20 @@ internal struct APIClient {
5874
}
5975
}
6076

61-
/// Request send To API and Perse Codable Models
62-
/// - Parameter request: RequestType
63-
/// - Returns: EventLoopFuture<RequestType.Response>
77+
/// Sends a request to the API and decodes the response into a `Codable` model, using Swift concurrency.
78+
///
79+
/// This is the async/await variant of `request(_:)`, returning the decoded
80+
/// response directly rather than wrapping it in an `EventLoopFuture`.
81+
///
82+
/// - Parameter request: The request to send. Must conform to `RequestType`
83+
/// and have a `Codable` response type.
84+
/// - Returns: The decoded response object.
85+
///
86+
/// - Throws:
87+
/// - `SeleniumError` if the server returns a known WebDriver error.
88+
/// - `APIError.responseStatsFailed` if the status code indicates failure.
89+
/// - `APIError.responseBodyIsNil` if no response body is returned.
90+
/// - Any `DecodingError` if the response cannot be decoded.
6491
@discardableResult
6592
public func request<R>(_ request: R) async throws -> R.Response where R: RequestType {
6693
try await self.request(request).get()

Sources/SwiftWebDriver/API/Request/ActionsPayload.swift

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,118 @@
33
// All source code and related assets are the property of GetAutomaApp.
44
// All rights reserved.
55

6-
struct WebDriverElementOrigin: Encodable {
7-
let element: String
6+
/// Represents a complete actions payload to be sent to the WebDriver Actions API.
7+
///
8+
/// Contains one or more input sources (pointer, touch, etc.) with their associated actions.
9+
internal struct ActionsPayload: Encodable {
10+
/// Array of input sources, each with their pointer actions.
11+
public let actions: [PointerSource]
12+
}
813

9-
enum CodingKeys: String, CodingKey {
10-
case element = "element-6066-11e4-a52e-4f735466cecf"
14+
/// Represents a pointer input source (e.g., mouse) for the WebDriver Actions API.
15+
internal struct PointerSource: Encodable {
16+
/// Type of input source, usually `"pointer"`.
17+
public let type: String
18+
19+
/// Identifier for the input source, e.g., `"mouse"`.
20+
public let id: String
21+
22+
/// Parameters describing the input source.
23+
public let parameters: Parameters
24+
25+
/// Sequence of pointer actions for this input source.
26+
public let actions: [PointerAction]
27+
28+
/// Parameters describing the pointer input source.
29+
internal struct Parameters: Encodable {
30+
/// Pointer type, e.g., `"mouse"`, `"pen"`, `"touch"`.
31+
public let pointerType: String
1132
}
1233
}
1334

14-
struct PointerAction: Encodable {
15-
let type: String
16-
let origin: WebDriverElementOrigin?
17-
let x: Int?
18-
let y: Int?
19-
let button: Int?
20-
let duration: Int?
35+
/// Represents a single pointer action for use in the WebDriver Actions API.
36+
///
37+
/// Examples of actions include pointer move, pointer down/up, and pauses.
38+
internal struct PointerAction: Encodable {
39+
/// The type of the action, e.g., `"pointerMove"`, `"pointerDown"`, `"pointerUp"`, `"pause"`.
40+
public let type: String
41+
42+
/// Optional origin element for the action.
43+
public let origin: WebDriverElementOrigin?
44+
45+
/// Optional x-coordinate for the pointer action.
46+
public let xCoordinate: Int?
47+
48+
/// Optional y-coordinate for the pointer action.
49+
public let yCoordinate: Int?
50+
51+
/// Optional button for pointer actions (0 = left, 1 = middle, 2 = right).
52+
public let button: Int?
53+
54+
/// Optional duration in milliseconds for pause actions.
55+
public let duration: Int?
2156

22-
init(
57+
/// Coding keys for encoding/decoding `PointerAction` to/from JSON.
58+
///
59+
/// Maps the Swift property names to the corresponding keys expected by the WebDriver Actions API.
60+
public enum CodingKeys: String, CodingKey {
61+
/// Action type (e.g., "pointerMove", "pointerDown", "pointerUp").
62+
case type
63+
64+
/// Origin element for the pointer action.
65+
case origin
66+
67+
/// X-coordinate of the pointer action.
68+
case xCoordinate = "x"
69+
70+
/// Y-coordinate of the pointer action.
71+
case yCoordinate = "y"
72+
73+
/// Mouse button involved in the action (0 = left, 1 = middle, 2 = right).
74+
case button
75+
76+
/// Duration of the action in milliseconds.
77+
case duration
78+
}
79+
80+
/// Initializes a new `PointerAction`.
81+
///
82+
/// - Parameters:
83+
/// - type: The type of pointer action.
84+
/// - origin: The element origin for the action (optional).
85+
/// - xCoordinate: X-coordinate (optional).
86+
/// - yCoordinate: Y-coordinate (optional).
87+
/// - button: Button index for mouse actions (optional).
88+
/// - duration: Duration in milliseconds for pause actions (optional).
89+
public init(
2390
type: String,
2491
origin: WebDriverElementOrigin? = nil,
25-
x: Int? = nil,
26-
y: Int? = nil,
92+
xCoordinate: Int? = nil,
93+
yCoordinate: Int? = nil,
2794
button: Int? = nil,
2895
duration: Int? = nil
2996
) {
3097
self.type = type
3198
self.origin = origin
32-
self.x = x
33-
self.y = y
99+
self.xCoordinate = xCoordinate
100+
self.yCoordinate = yCoordinate
34101
self.button = button
35102
self.duration = duration
36103
}
37104
}
38105

39-
struct PointerSource: Encodable {
40-
let type: String
41-
let id: String
42-
let parameters: Parameters
43-
let actions: [PointerAction]
106+
/// Represents a reference to a WebDriver element in the Actions API.
107+
///
108+
/// This is used as the origin for pointer or touch actions.
109+
internal struct WebDriverElementOrigin: Encodable {
110+
/// The element ID in WebDriver protocol format.
111+
public let element: String
44112

45-
struct Parameters: Encodable {
46-
let pointerType: String
113+
/// Coding keys for encoding/decoding `WebDriverElementOrigin` to/from JSON.
114+
///
115+
/// Maps the Swift property `element` to the WebDriver protocol’s expected key.
116+
public enum CodingKeys: String, CodingKey {
117+
/// The WebDriver element identifier key used in actions payloads.
118+
case element = "element-6066-11e4-a52e-4f735466cecf"
47119
}
48120
}
49-
50-
struct ActionsPayload: Encodable {
51-
let actions: [PointerSource]
52-
}

Sources/SwiftWebDriver/API/Request/DevTools/AnyEncodable.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,33 @@
33
// All source code and related assets are the property of GetAutomaApp.
44
// All rights reserved.
55

6+
/// A type-erased wrapper for any `Encodable` value.
7+
///
8+
/// `AnyEncodable` allows you to store or pass around values of different `Encodable`
9+
/// types without knowing their concrete type at compile time. This is useful when
10+
/// building heterogeneous collections of `Encodable` objects or creating dynamic
11+
/// JSON payloads.
612
public struct AnyEncodable: Encodable {
13+
// MARK: - Private Properties
14+
15+
/// The internal closure used to encode the wrapped value.
716
private let encodeFunc: (Encoder) throws -> Void
817

9-
init(_ value: some Encodable) {
18+
// MARK: - Initializers
19+
20+
/// Creates a type-erased wrapper around the given `Encodable` value.
21+
///
22+
/// - Parameter value: Any value that conforms to `Encodable`.
23+
public init(_ value: some Encodable) {
1024
encodeFunc = value.encode
1125
}
1226

27+
// MARK: - Encoding
28+
29+
/// Encodes the wrapped value using the given encoder.
30+
///
31+
/// - Parameter encoder: The encoder to write data to.
32+
/// - Throws: Any encoding error thrown by the wrapped value.
1333
public func encode(to encoder: Encoder) throws {
1434
try encodeFunc(encoder)
1535
}

Sources/SwiftWebDriver/API/Request/DevTools/DevToolTypes.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22
// Copyright (c) 2025 GetAutomaApp
33
// All source code and related assets are the property of GetAutomaApp.
44
// All rights reserved.
5-
//
6-
// This package is freely distributable under the MIT license.
7-
// This Package is a modified fork of https://github.com/ashi-psn/SwiftWebDriver.
85

6+
/// A namespace containing types related to browser developer tool operations.
7+
///
8+
/// `DevToolTypes` groups together constants and enumerations that define
9+
/// configuration or execution modes for actions performed via browser
10+
/// developer tools.
911
public enum DevToolTypes {
12+
/// Represents the available modes for executing JavaScript through
13+
/// browser developer tools.
1014
public enum JavascriptExecutionTypes {
11-
case async, sync
15+
/// Executes JavaScript **asynchronously**.
16+
///
17+
/// The execution returns a result only after an asynchronous operation
18+
/// completes, allowing for `Promise` handling or delayed callbacks.
19+
case async
20+
21+
/// Executes JavaScript **synchronously**.
22+
///
23+
/// The execution blocks until a result is immediately available,
24+
/// without waiting for asynchronous operations.
25+
case sync
1226
}
1327
}

Sources/SwiftWebDriver/API/Request/DevTools/PostExecuteAsyncRequest.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)