Skip to content

Commit

Permalink
Cleanup response handling and improve error tests. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdav authored Aug 9, 2023
1 parent 8ab40cf commit 4f70372
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Sources/Element+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ extension Element {
}

struct KeysRequest: WebDriverRequest {
typealias ResponseValue = CodableNone
typealias Response = WebDriverResponseNoValue

private let element: Element

Expand Down
2 changes: 1 addition & 1 deletion Sources/Session+Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ extension Session {
}

struct KeysRequest: WebDriverRequest {
typealias ResponseValue = CodableNone
typealias Response = WebDriverResponseNoValue

let session: Session
init(_ session: Session, value: [String]) {
Expand Down
4 changes: 1 addition & 3 deletions Sources/WebDriverRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ public struct WebDriverResponse<Value>: Codable where Value: Codable {
}

// For WebDriver requests whose response lacks a value field.
public struct WebDriverResponseNoValue: Codable {
public init(from decoder: Decoder) throws {}
}
public struct WebDriverResponseNoValue: Codable {}
14 changes: 9 additions & 5 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ class APIToRequestMappingTests: XCTestCase {
.expect(path: "session/mySession/element/myElement/size", method: .get, type: Element.SizeRequest.self) { WebDriverResponse(value: .init(width: 100, height: 200)) }
XCTAssert(try element.size == (width: 100, height: 200))

let keys = ["a", "b", "c"]
mockWebDriver.expect(path: "session/mySession/element/myElement/value", method: .post, type: Element.KeysRequest.self) {
XCTAssertEqual($0.value, ["a", "b", "c"])
return WebDriverResponse(value: CodableNone())
XCTAssertEqual($0.value, keys)
return WebDriverResponseNoValue()
}
try element.sendKeys(value: ["a", "b", "c"])
try element.sendKeys(value: keys)

mockWebDriver.expect(path: "session/mySession/keys", method: .post)
try session.sendKeys(value: ["d", "e", "f"])
mockWebDriver.expect(path: "session/mySession/keys", method: .post, type: Session.KeysRequest.self) {
XCTAssertEqual($0.value, keys)
return WebDriverResponseNoValue()
}
try session.sendKeys(value: keys)

// Account for session deinitializer
mockWebDriver.expect(path: "session/mySession", method: .delete)
Expand Down
18 changes: 7 additions & 11 deletions Tests/WebDriverTests/NotepadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ class Notepad {
}

func dismissNewFileDialog() throws {
guard let dismissButton = try? session.findElement(byName: "No") else {
return XCTFail("Dismiss New File dialog: Button \"no\" was not found")
}
let dismissButton = try XCTUnwrap(session.findElement(byName: "No"), "Dismiss New File dialog: Button \"no\" was not found")
try dismissButton.click()
}

func moveToCenterOf(byName name: String) throws {
guard let element = try? session.findElement(byName: name) else {
XCTFail("Can't find element \(name)")
return
}

let element = try XCTUnwrap(session.findElement(byName: name), "Can't find element named '\(name)'")
let size = try element.size
XCTAssert(size.width > 0)
XCTAssert(size.height > 0)
Expand All @@ -50,12 +44,14 @@ class Notepad {
editor = try session.findElement(byClassName: "Edit")
}
}
XCTAssertNotNil(editor)
try editor!.sendKeys(value: keys)

let editor = try XCTUnwrap(editor, "Failed to find element named 'Text Editor' or of class 'Edit'")
try editor.sendKeys(value: keys)
}

func close() throws {
try session.findElement(byName: "close")?.click()
let closeButton = try XCTUnwrap(session.findElement(byName: "Close"), "Failed to find element named 'Close'")
try closeButton.click()
}
}

Expand Down
20 changes: 16 additions & 4 deletions Tests/WebDriverTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ class SessionTests: XCTestCase {
}

public func testMaximizeAndRestore() throws {
guard let element = try Self.session.findElement(byName: "Maximize") else {
XCTAssert(false, "Maximize button not found")
return
}
let element = try XCTUnwrap(Self.session.findElement(byName: "Maximize"), "Maximize button not found")
try element.click()
try element.click()
}

public func testKeysAndAttributes() throws {
continueAfterFailure = false

let findWhatEditBoxAutomationId = "204"
let element = try XCTUnwrap(Self.session.findElement(byAccessibilityId: findWhatEditBoxAutomationId))
try element.click()

try XCTAssertEqual(element.getAttribute(name: "HasKeyboardFocus").lowercased(), "true", "Element does not have keyboard focus")
try Self.session.sendKeys(value: ["B", "I", "O", "S", KeyCode.returnKey.rawValue])

// It takes some time for focus to move.
Thread.sleep(forTimeInterval: 1)
try XCTAssertEqual(element.getAttribute(name: "HasKeyboardFocus").lowercased(), "false", "Element still has keyboard focus")
}
}
10 changes: 5 additions & 5 deletions Tests/WebDriverTests/StatusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class StatusTest: XCTestCase {

// test that status returns reasonable answers
func testStatus() throws {
let status = try Self.winAppDriver.status
let status = try XCTUnwrap(Self.winAppDriver.status)

// Check that we got a version number
XCTAssert(status?.build?.version != nil)
XCTAssert(status?.build?.version != String())
XCTAssertNotNil(status.build?.version)
XCTAssertNotEqual(status.build?.version, String())

// Check the returned date format
if let dateString = status?.build?.time {
if let dateString = status.build?.time {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss yyyy"
Expand All @@ -41,6 +41,6 @@ class StatusTest: XCTestCase {
}

// and that the OS is windows
XCTAssert(status?.os?.name == "windows")
XCTAssert(status.os?.name == "windows")
}
}

0 comments on commit 4f70372

Please sign in to comment.