From aa85731d7606b8f5cdf47a8190e0961c6fa9a34a Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 20 Oct 2023 15:21:12 -0400 Subject: [PATCH 1/3] Add a parameter to make it explicit when modifier keys should not be released. --- Sources/Keys.swift | 4 +++- Sources/Session.swift | 15 +++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Sources/Keys.swift b/Sources/Keys.swift index d0a84a5..58c77c2 100644 --- a/Sources/Keys.swift +++ b/Sources/Keys.swift @@ -45,7 +45,6 @@ public struct Keys: RawRepresentable { 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 +98,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. From 860d56773681dfd7d54eb13dd211cfd8ff03c6c1 Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 20 Oct 2023 15:23:21 -0400 Subject: [PATCH 2/3] Remove stale comment --- Sources/Keys.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Keys.swift b/Sources/Keys.swift index 58c77c2..2107b2e 100644 --- a/Sources/Keys.swift +++ b/Sources/Keys.swift @@ -44,7 +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 cancel = Self(rawValue: "\u{E001}") public static let help = Self(rawValue: "\u{E002}") public static let backspace = Self(rawValue: "\u{E003}") From 2a0e43322ac9366afc98f37b63983dbeed62ec2a Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Fri, 20 Oct 2023 15:28:48 -0400 Subject: [PATCH 3/3] Fix unit test --- Tests/UnitTests/APIToRequestMappingTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)