Skip to content

Commit b28f253

Browse files
authored
Use TimeoutType cases over static vars (#171)
1 parent 1780b8c commit b28f253

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

Sources/WebDriver/Requests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,15 @@ public enum Requests {
350350
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidtimeouts
351351
public struct SessionTimeouts: Request {
352352
public var session: String
353-
public var type: String
353+
public var type: TimeoutType
354354
public var ms: Double
355355

356356
public var pathComponents: [String] { ["session", session, "timeouts"] }
357357
public var method: HTTPMethod { .post }
358358
public var body: Body { .init(type: type, ms: ms) }
359359

360360
public struct Body: Codable {
361-
public var type: String
361+
public var type: TimeoutType
362362
public var ms: Double
363363
}
364364
}

Sources/WebDriver/Session.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Session {
3434
if newValue == _implicitWaitTimeout { return }
3535
if !emulateImplicitWait {
3636
do {
37-
try setTimeout(type: TimeoutType.implicitWait, duration: newValue)
37+
try setTimeout(type: .implicitWait, duration: newValue)
3838
} catch {
3939
emulateImplicitWait = true
4040
}
@@ -101,11 +101,11 @@ public class Session {
101101
}
102102

103103
/// Sets a a timeout value on this session.
104-
public func setTimeout(type: String, duration: TimeInterval) throws {
104+
public func setTimeout(type: TimeoutType, duration: TimeInterval) throws {
105105
try webDriver.send(
106106
Requests.SessionTimeouts(session: id, type: type, ms: duration * 1000))
107107
// Keep track of the implicit wait to know when we need to override it.
108-
if type == TimeoutType.implicitWait { _implicitWaitTimeout = duration }
108+
if type == .implicitWait { _implicitWaitTimeout = duration }
109109
}
110110

111111
public func execute(script: String, args: [String] = [], async: Bool = false) throws {

Sources/WebDriver/TimeoutType.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
public enum TimeoutType {
2-
public static let script = "script"
3-
public static let implicitWait = "implicit"
4-
public static let pageLoad = "page load"
5-
}
1+
public enum TimeoutType: String, Codable {
2+
case script
3+
case implicitWait = "implicit"
4+
case pageLoad = "page load"
5+
}

Tests/UnitTests/APIToRequestMappingTests.swift

+8
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,12 @@ class APIToRequestMappingTests: XCTestCase {
330330
}
331331
XCTAssert(try session.source == "currentSource")
332332
}
333+
334+
func testSessionTimeouts() throws {
335+
let mockWebDriver: MockWebDriver = MockWebDriver()
336+
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
337+
mockWebDriver.expect(path: "session/mySession/timeouts", method: .post)
338+
try session.setTimeout(type: .implicitWait, duration: 5)
339+
XCTAssert(session.implicitWaitTimeout == 5.0)
340+
}
333341
}

0 commit comments

Comments
 (0)