Skip to content

Commit

Permalink
Shorten type names, removing WebDriver prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Aug 31, 2023
1 parent 5eed01d commit 87bd1a7
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The `setUp()` method of the `XCTestCase` class instantiates the WinAppDriver usi

Implementations of the bindings are in the `Sources` folder. `WinAppDriver.swift`, `Session.swift`, `Element.swift` implement the corresponding object structs or classes. Files with the same names followed by `+requests` implement the actual bindings as extensions of these structs or classes.

Each binding consist of a request struct conforming to the `WebDriverRequest` protocol and specializing the request, and of a method of the appropriate object instantiating that struct and passing it to `WebDriver.send<Request>(:)`. This encapsulates the specifics of the underlying http requests to that function.
Each binding consist of a request struct conforming to the `Request` protocol and specializing the request, and of a method of the appropriate object instantiating that struct and passing it to `WebDriver.send<Request>(:)`. This encapsulates the specifics of the underlying http requests to that function.

## Building and running the project

Expand Down
16 changes: 8 additions & 8 deletions Sources/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public struct Element {
/// click() - simulate clicking this Element
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidclick
public func click(retryTimeout: TimeInterval? = nil) throws {
let request = WebDriverRequests.ElementClick(session: session.id, element: id)
let request = Requests.ElementClick(session: session.id, element: id)
try retryUntil(retryTimeout ?? session.defaultRetryTimeout) {
do {
try webDriver.send(request)
return true
} catch let error as WebDriverError where error.status == .winAppDriver_elementNotInteractable {
} catch let error as ErrorResponse where error.status == .winAppDriver_elementNotInteractable {
return false
}
}
Expand All @@ -30,7 +30,7 @@ public struct Element {
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidtext
public var text: String {
get throws {
try webDriver.send(WebDriverRequests.ElementText(
try webDriver.send(Requests.ElementText(
session: session.id, element: id)).value
}
}
Expand Down Expand Up @@ -92,15 +92,15 @@ public struct Element {
/// - calls fatalError for any other error
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidattributename
public func getAttribute(name: String) throws -> String {
try webDriver.send(WebDriverRequests.ElementAttribute(
try webDriver.send(Requests.ElementAttribute(
session: session.id, element: id, attribute: name)).value
}

/// location - return x, y location of the element relative to the screen top left corner
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidlocation
public var location: (x: Int, y: Int) {
get throws {
let responseValue = try webDriver.send(WebDriverRequests.ElementLocation(
let responseValue = try webDriver.send(Requests.ElementLocation(
session: session.id, element: id)).value
return (responseValue.x, responseValue.y)
}
Expand All @@ -110,7 +110,7 @@ public struct Element {
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidsize
public var size: (width: Int, height: Int) {
get throws {
let responseValue = try webDriver.send(WebDriverRequests.ElementSize(
let responseValue = try webDriver.send(Requests.ElementSize(
session: session.id, element: id)).value
return (responseValue.width, responseValue.height)
}
Expand All @@ -120,15 +120,15 @@ public struct Element {
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementiddisplayed
public var displayed: Bool {
get throws {
try webDriver.send(WebDriverRequests.ElementDisplayed(
try webDriver.send(Requests.ElementDisplayed(
session: session.id, element: id)).value
}
}

/// Send keys to an element
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelementidvalue
public func sendKeys(value: [String]) throws {
try webDriver.send(WebDriverRequests.ElementValue(
try webDriver.send(Requests.ElementValue(
session: session.id, element: id, value: value))
}

Expand Down
11 changes: 5 additions & 6 deletions Sources/WebDriverError.swift → Sources/ErrorResponse.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
public struct WebDriverError: Codable, Error {
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#response-status-codes
public struct ErrorResponse: Codable, Error {
public var status: Status
public var value: Value

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#response-status-codes
public struct Status: Codable, Hashable, RawRepresentable {
public var rawValue: Int

Expand Down Expand Up @@ -30,7 +32,7 @@ public struct WebDriverError: Codable, Error {
public static let sessionNotCreatedException = Self(rawValue: 33)
public static let moveTargetOutOfBounds = Self(rawValue: 34)

public init?(rawValue: Int) {
public init(rawValue: Int) {
self.rawValue = rawValue
}
}
Expand All @@ -40,7 +42,4 @@ public struct WebDriverError: Codable, Error {
public var message: String
public var stacktrace: String?
}

public var status: Status
public var value: Value
}
14 changes: 7 additions & 7 deletions Sources/HTTPWebDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ public struct HTTPWebDriver: WebDriver {
rootURL = endpoint
}

// Send a WebDriverRequest to the web driver local service
// Send a Request to the web driver local service
// TODO: consider making this function async/awaitable
@discardableResult
public func send<Request: WebDriverRequest>(_ request: Request) throws -> Request.Response {
public func send<Req: Request>(_ request: Req) throws -> Req.Response {
let urlRequest = try buildURLRequest(request)

// Send the request and decode result or error
let (status, responseData) = try urlRequest.send()
guard status == 200 else {
throw try JSONDecoder().decode(WebDriverError.self, from: responseData)
throw try JSONDecoder().decode(ErrorResponse.self, from: responseData)
}
return try JSONDecoder().decode(Request.Response.self, from: responseData)
return try JSONDecoder().decode(Req.Response.self, from: responseData)
}

private func buildURLRequest<Request: WebDriverRequest>(_ request: Request) throws -> URLRequest {
private func buildURLRequest<Req: Request>(_ request: Req) throws -> URLRequest {
var url = rootURL
for (index, pathComponent) in request.pathComponents.enumerated() {
let last = index == request.pathComponents.count - 1
Expand All @@ -36,8 +36,8 @@ public struct HTTPWebDriver: WebDriver {
// TODO(#40): Setting timeoutInterval causes a crash when sending the request on the CI machines.
// urlRequest.timeoutInterval = Self.defaultTimeout

// Add the body if the WebDriverRequest type defines one
if Request.Body.self != CodableNone.self {
// Add the body if the Request type defines one
if Req.Body.self != CodableNone.self {
urlRequest.addValue("application/json;charset=UTF-8", forHTTPHeaderField: "content-type")
urlRequest.httpBody = try JSONEncoder().encode(request.body)
}
Expand Down
11 changes: 4 additions & 7 deletions Sources/WebDriverRequest.swift → Sources/Request.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import Foundation
import FoundationNetworking

public struct CodableNone: Codable {}

public protocol WebDriverRequest {
public protocol Request {
associatedtype Body: Codable = CodableNone
associatedtype Response: Codable = CodableNone

Expand All @@ -12,10 +7,12 @@ public protocol WebDriverRequest {
var body: Body { get }
}

extension WebDriverRequest where Body == CodableNone {
extension Request where Body == CodableNone {
public var body: Body { .init() }
}

public struct CodableNone: Codable {}

public enum HTTPMethod: String {
case get = "GET"
case delete = "DELETE"
Expand Down
36 changes: 18 additions & 18 deletions Sources/WebDriverRequests.swift → Sources/Requests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum WebDriverRequests {
public enum Requests {
public struct ResponseWithValue<Value>: Codable where Value: Codable {
public var value: Value

Expand All @@ -19,7 +19,7 @@ public enum WebDriverRequests {
}
}

public struct ElementAttribute: WebDriverRequest {
public struct ElementAttribute: Request {
public var session: String
public var element: String
public var attribute: String
Expand All @@ -30,15 +30,15 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<String>
}

public struct ElementClick: WebDriverRequest {
public struct ElementClick: Request {
public var session: String
public var element: String

public var pathComponents: [String] { ["session", session, "element", element, "click"] }
public var method: HTTPMethod { .post }
}

public struct ElementDisplayed: WebDriverRequest {
public struct ElementDisplayed: Request {
public var session: String
public var element: String

Expand All @@ -52,7 +52,7 @@ public enum WebDriverRequests {
}
}

public struct ElementValue: WebDriverRequest {
public struct ElementValue: Request {
public var session: String
public var element: String
public var value: [String]
Expand All @@ -67,7 +67,7 @@ public enum WebDriverRequests {
}
}

public struct ElementLocation: WebDriverRequest {
public struct ElementLocation: Request {
public var session: String
public var element: String

Expand All @@ -81,7 +81,7 @@ public enum WebDriverRequests {
}
}

public struct ElementSize: WebDriverRequest {
public struct ElementSize: Request {
public var session: String
public var element: String

Expand All @@ -95,7 +95,7 @@ public enum WebDriverRequests {
}
}

public struct ElementText: WebDriverRequest {
public struct ElementText: Request {
public var session: String
public var element: String

Expand All @@ -105,7 +105,7 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<String>
}

public struct Session<Caps: Capabilities>: WebDriverRequest {
public struct Session<Caps: Capabilities>: Request {
public var requiredCapabilities: Caps?
public var desiredCapabilities: Caps?

Expand All @@ -129,7 +129,7 @@ public enum WebDriverRequests {
}
}

public struct SessionActiveElement: WebDriverRequest {
public struct SessionActiveElement: Request {
public var session: String

public var pathComponents: [String] { ["session", session, "element", "active"] }
Expand All @@ -138,7 +138,7 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<ElementResponseValue>
}

public struct SessionButton: WebDriverRequest {
public struct SessionButton: Request {
public var session: String
public var action: Action
public var button: MouseButton
Expand All @@ -158,14 +158,14 @@ public enum WebDriverRequests {
}
}

public struct SessionDelete: WebDriverRequest {
public struct SessionDelete: Request {
public var sessionId: String

public var pathComponents: [String] { ["session", sessionId] }
public var method: HTTPMethod { .delete }
}

public struct SessionElement: WebDriverRequest {
public struct SessionElement: Request {
public var session: String
public var element: String? = nil
public var using: String
Expand All @@ -190,7 +190,7 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<ElementResponseValue>
}

public struct SessionKeys: WebDriverRequest {
public struct SessionKeys: Request {
public var session: String
public var value: [String]

Expand All @@ -203,7 +203,7 @@ public enum WebDriverRequests {
}
}

public struct SessionMoveTo: WebDriverRequest {
public struct SessionMoveTo: Request {
public var session: String
public var element: String?
public var xOffset: Int
Expand All @@ -226,7 +226,7 @@ public enum WebDriverRequests {
}
}

public struct SessionScreenshot: WebDriverRequest {
public struct SessionScreenshot: Request {
public var session: String

public var pathComponents: [String] { ["session", session, "screenshot"] }
Expand All @@ -235,7 +235,7 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<String>
}

public struct SessionTitle: WebDriverRequest {
public struct SessionTitle: Request {
public var session: String

public var pathComponents: [String] { ["session", session, "title"] }
Expand All @@ -244,7 +244,7 @@ public enum WebDriverRequests {
public typealias Response = ResponseWithValue<String>
}

public struct Status: WebDriverRequest {
public struct Status: Request {
public var pathComponents: [String] { ["status"] }
public var method: HTTPMethod { .get }

Expand Down
Loading

0 comments on commit 87bd1a7

Please sign in to comment.