From 9232d45cf9c19329386c4c075a115c41fc3f2eed Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 20 Oct 2023 15:35:31 -0400 Subject: [PATCH] Make it explicit when modifier keys should not be released (#112) --- Sources/Keys.swift | 5 +++-- Sources/Session.swift | 15 +++++++++------ Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Sources/Keys.swift b/Sources/Keys.swift index d0a84a5..2107b2e 100644 --- a/Sources/Keys.swift +++ b/Sources/Keys.swift @@ -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}") @@ -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}") diff --git a/Sources/Session.swift b/Sources/Session.swift index 796b45a..ea42b05 100644 --- a/Sources/Session.swift +++ b/Sources/Session.swift @@ -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. diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index fe99063..498ecb4 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -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)