Skip to content

Commit

Permalink
Make it explicit when modifier keys should not be released (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Oct 20, 2023
1 parent 3a11024 commit 9232d45
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Sources/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public struct Keys: RawRepresentable {
public static let digit9 = Self(rawValue: "9")
public static let digit0 = Self(rawValue: "0")

/// Resets the state of modifier keys
public static let null = Self(rawValue: "\u{E000}")
public static let cancel = Self(rawValue: "\u{E001}")
public static let help = Self(rawValue: "\u{E002}")
public static let backspace = Self(rawValue: "\u{E003}")
Expand Down Expand Up @@ -99,6 +97,9 @@ public struct Keys: RawRepresentable {

/// Modifier keys are interpreted as toggles instead of key presses.
public enum Modifiers {
/// A special Keys value that causes all modifiers to be released.
public static let releaseAll = Keys(rawValue: "\u{E000}")

public static let shift = Keys(rawValue: "\u{E008}")
public static let control = Keys(rawValue: "\u{E009}")
public static let alt = Keys(rawValue: "\u{E00A}")
Expand Down
15 changes: 9 additions & 6 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,19 @@ public class Session {

/// Sends key presses to this session.
/// - Parameter keys: An array of key sequences according to the WebDriver spec.
public func sendKeys(_ keys: [Keys]) throws {
try webDriver.send(Requests.SessionKeys(
session: id, value: keys.map { $0.rawValue }))
/// - Parameter releaseModifiers: A boolean indicating whether to release modifier keys at the end of the sequence.
public func sendKeys(_ keys: [Keys], releaseModifiers: Bool = true) throws {
var value = keys.map { $0.rawValue }
if releaseModifiers { value.append(Keys.Modifiers.releaseAll.rawValue) }
try webDriver.send(Requests.SessionKeys(session: id, value: value))
}

/// Sends key presses to this session.
/// - Parameter keys: A key sequence according to the WebDriver spec.
public func sendKeys(_ keys: Keys) throws {
try webDriver.send(Requests.SessionKeys(
session: id, value: [keys.rawValue]))
/// - Parameter releaseModifiers: A boolean indicating whether to release modifier keys at the end of the sequence.
public func sendKeys(_ keys: Keys, releaseModifiers: Bool = true) throws {
let value = releaseModifiers ? [keys.rawValue, Keys.Modifiers.releaseAll.rawValue] : [keys.rawValue]
try webDriver.send(Requests.SessionKeys(session: id, value: value))
}

/// Deletes the current session.
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssertEqual($0.value, keys.map { $0.rawValue })
return CodableNone()
}
try session.sendKeys(keys)
try session.sendKeys(keys, releaseModifiers: false)

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

0 comments on commit 9232d45

Please sign in to comment.