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

Implement and test session timeouts request #80

Merged
merged 1 commit into from
Sep 5, 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
15 changes: 15 additions & 0 deletions Sources/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public enum Requests {
public typealias Response = ResponseWithValue<String>
}

public struct SessionTimeouts: Request {
public var session: String
public var type: String
public var ms: Double

public var pathComponents: [String] { ["session", session, "timeouts"] }
public var method: HTTPMethod { .post }
public var body: Body { .init(type: type, ms: ms) }

public struct Body: Codable {
public var type: String
public var ms: Double
}
}

public struct SessionTitle: Request {
public var session: String

Expand Down
7 changes: 7 additions & 0 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class Session {
}
}

/// Sets a a timeout value on this session.
/// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidtimeouts
public func setTimeout(type: String, duration: TimeInterval) throws {
try webDriver.send(
Requests.SessionTimeouts(session: id, type: type, ms: duration * 1000))
}

/// screenshot()
/// Take a screenshot of the current page.
/// - Returns: The screenshot data as a PNG file.
Expand Down
5 changes: 5 additions & 0 deletions Sources/TimeoutType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum TimeoutType {
public static let script = "script"
public static let implicitWait = "implicit"
public static let pageLoad = "page load"
}
55 changes: 55 additions & 0 deletions Tests/WebDriverTests/TimeoutTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@testable import WebDriver
import XCTest

class TimeoutTests: XCTestCase {
private static var _winAppDriver: Result<WinAppDriver, any Error>!
public var winAppDriver: WinAppDriver! { try? Self._winAppDriver.get() }

public override class func setUp() {
_winAppDriver = Result { try WinAppDriver() }
}

public override class func tearDown() {
_winAppDriver = nil
}

public override func setUpWithError() throws {
try XCTSkipIf(winAppDriver == nil)
}

func startApp() throws -> Session {
// Use a simple app in which we can expect queries to execute quickly
let windowsDir = ProcessInfo.processInfo.environment["SystemRoot"]!
let capabilities = WinAppDriver.Capabilities(app: "\(windowsDir)\\System32\\winver.exe")
return try Session(webDriver: winAppDriver, desiredCapabilities: capabilities)
}

static func time(_ callback: () throws -> Void) rethrows -> Double {
let before = DispatchTime.now()
try callback()
let after = DispatchTime.now()
return Double(after.uptimeNanoseconds - before.uptimeNanoseconds) / 1_000_000_000
}

public func testLibraryImplicitWait() throws {
let session = try startApp()

// Test library timeout implementation
session.defaultRetryTimeout = 1
XCTAssert(try Self.time({ _ = try session.findElement(byAccessibilityId: "IdThatDoesNotExist") }) > 0.5)

session.defaultRetryTimeout = 0
XCTAssert(try Self.time({ _ = try session.findElement(byAccessibilityId: "IdThatDoesNotExist") }) < 0.5)
}

public func testWebDriverImplicitWait() throws {
let session = try startApp()
session.defaultRetryTimeout = 0

try session.setTimeout(type: TimeoutType.implicitWait, duration: 1)
XCTAssert(try Self.time({ _ = try session.findElement(byAccessibilityId: "IdThatDoesNotExist") }) > 0.5)

try session.setTimeout(type: TimeoutType.implicitWait, duration: 0)
XCTAssert(try Self.time({ _ = try session.findElement(byAccessibilityId: "IdThatDoesNotExist") }) < 0.5)
}
}