Skip to content

Commit

Permalink
add request for sending keys to active element (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff <[email protected]>
  • Loading branch information
stevenbrix and jeffdav authored Jul 25, 2023
1 parent 2c01cc8 commit 2c6653c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Sources/Session+Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,37 @@ extension Session {
var button: MouseButton
}
}

/// sendKeys(:) - send key strokes to the session
/// - Parameter value: key strokes to send
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidkeys
public func sendKeys(value: [String]) {
let keysRequest = KeysRequest(self, value: value)
try! webDriver.send(keysRequest)
}

/// Send keys to the session
/// This overload takes a single string for simplicity
public func sendKeys(value: String) {
let keysRequest = KeysRequest(self, value: [value])
try! webDriver.send(keysRequest)
}

struct KeysRequest: WebDriverRequest {
typealias ResponseValue = CodableNone

let session: Session
init(_ session: Session, value: [String]) {
self.session = session
body = .init(value: value)
}

var pathComponents: [String] { ["session", session.id, "keys"] }
var method: HTTPMethod { .post }
var body: Body

struct Body: Codable {
var value: [String]
}
}
}
3 changes: 3 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class APIToRequestMappingTests: XCTestCase {
}
element.sendKeys(value: ["a", "b", "c"])

mockWebDriver.expect(path: "session/mySession/keys", method: .post)
session.sendKeys(value: ["d", "e", "f"])

// Account for session deinitializer
mockWebDriver.expect(path: "session/mySession", method: .delete)
}
Expand Down

0 comments on commit 2c6653c

Please sign in to comment.