Skip to content

Commit

Permalink
Take sessionId out of the generic response struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdav committed Aug 3, 2023
1 parent 3c7ed8a commit 70dbf26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sources/Element+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Element {
}

struct ClickRequest: WebDriverRequest {
typealias ResponseValue = WebDriverNoResponseValue
typealias ResponseValue = WebDriverResponseNoValue

private let element: Element

Expand Down
17 changes: 11 additions & 6 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ public class Session {
let webDriver: any WebDriver
let id: String

private var valid: Bool

init(in webDriver: some WebDriver, id: String) {
self.webDriver = webDriver
self.id = id
valid = true
}

/// retryTimeout
Expand All @@ -17,23 +20,25 @@ public class Session {
/// delete
/// Attempts to delete the session.
func delete() throws {
guard valid else {
return
}

let deleteSessionRequest = DeleteSessionRequest(sessionId: id)
try webDriver.send(deleteSessionRequest)
valid = false
}

deinit {
// TODO: Get rid of this deinit and make callers use Session.delete
// and handle/propegate exceptions. For now this is challenging to
// untangle, and unlikely to actually throw.
do { try delete() } catch let error as WebDriverError {
fatalError("Error in Session.delete: \(error)")
assertionFailure("Error in Session.delete: \(error)")
} catch {
fatalError("Unknown error in Session.delete.")
assertionFailure("Unknown error in Session.delete.")
}
}

struct DeleteSessionRequest: WebDriverRequest {
typealias ResponseValue = WebDriverNoResponseValue
typealias ResponseValue = WebDriverResponseNoValue

let sessionId: String
var pathComponents: [String] { ["session", sessionId] }
Expand Down
16 changes: 6 additions & 10 deletions Sources/WebDriverRequest.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import FoundationNetworking

public struct CodableNone: Codable {}

public protocol WebDriverRequest {
associatedtype Body: Codable = CodableNone
associatedtype ResponseValue: Codable = CodableNone
Expand All @@ -19,27 +21,21 @@ extension WebDriverRequest {
public var query: [String: String] { [:] }
}

public struct CodableNone: Codable {}

public enum HTTPMethod: String {
case get = "GET"
case delete = "DELETE"
case post = "POST"
}

// Response to a WebDriver request
// All fields are optional because the response returned by WebDriver might be missing them, e.g.,
// - deleteSession request returns no sessionId and no value
// - element click request returns a sessionId but no value
// - etc.
// Response to a WebDriver request with a response value.
public struct WebDriverResponse<Value>: Codable where Value: Codable {
public var sessionId: String?
public var status: Int?
public var value: Value?
}

// For WebDriver requests with no expected response.
public struct WebDriverNoResponse: Codable {}

public struct WebDriverNoResponseValue: Codable {
// For WebDriver requests whose response lacks a value field.
public struct WebDriverResponseNoValue: Codable {
public init(from decoder: Decoder) throws {}
}
15 changes: 10 additions & 5 deletions Sources/WinAppDriver+Requests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Foundation
import WinSDK

public struct SessionResponse: Codable {
public var sessionId: String
}

extension WinAppDriver {
/// newSession(app:) - Creates a new WinAppDriver session
/// - app: location of the exe for the app to test
Expand All @@ -11,11 +15,11 @@ extension WinAppDriver {
public func newSession(app: String, appArguments: [String]? = nil, appWorkingDir: String? = nil, waitForAppLaunch: Int? = nil) throws -> Session {
let args = appArguments?.joined(separator: " ")
let newSessionRequest = NewSessionRequest(app: app, appArguments: args, appWorkingDir: appWorkingDir, waitForAppLaunch: waitForAppLaunch)
return Session(in: self, id: try send(newSessionRequest).sessionId!)
return Session(in: self, id: try send(newSessionRequest).sessionId)
}

struct NewSessionRequest: WebDriverRequest {
typealias ResponseValue = WebDriverNoResponseValue
typealias Response = SessionResponse

init(app: String, appArguments: String?, appWorkingDir: String?, waitForAppLaunch: Int?) {
body.desiredCapabilities = .init(app: app, appArguments: appArguments, appWorkingDir: appWorkingDir, waitForAppLaunch: waitForAppLaunch)
Expand All @@ -33,6 +37,7 @@ extension WinAppDriver {
var appWorkingDir: String?
var waitForAppLaunch: Int?
let experimentalWebDriver = true

enum CodingKeys: String, CodingKey {
case app
case appArguments
Expand All @@ -52,13 +57,13 @@ extension WinAppDriver {
/// Creates a new session attached to an existing app top level window
/// - Parameter appTopLevelWindowHandle: the window handle
/// - Returns: new Session instance
public func newSession(appTopLevelWindowHandle: UInt) -> Session {
public func newSession(appTopLevelWindowHandle: UInt) throws -> Session {
let newSessionRequest = NewSessionAttachRequest(appTopLevelWindowHandle: appTopLevelWindowHandle)
return Session(in: self, id: try! send(newSessionRequest).sessionId!)
return Session(in: self, id: try send(newSessionRequest).sessionId)
}

struct NewSessionAttachRequest: WebDriverRequest {
typealias ResponseValue = WebDriverNoResponseValue
typealias Response = SessionResponse

init(appTopLevelWindowHandle: UInt) {
let appTopLevelWindowHexHandle = String(appTopLevelWindowHandle, radix: 16)
Expand Down

0 comments on commit 70dbf26

Please sign in to comment.