Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it explicit when modifier keys should not be released #112

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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