Skip to content

Commit

Permalink
Store element's found method, to allow element's non-existence to be …
Browse files Browse the repository at this point in the history
…checked
  • Loading branch information
vinocher-bc committed May 1, 2024
1 parent 4555637 commit fb27464
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Sources/WebDriver/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ public struct Element {
var webDriver: WebDriver { session.webDriver }
public let session: Session
public let id: String
public let foundUsing: String?
public let foundUsingValue: String?

public init(session: Session, id: String) {
public init(session: Session, id: String, foundUsing: String? = nil, foundUsingValue: String? = nil) {
self.session = session
self.id = id
self.foundUsing = foundUsing
self.foundUsingValue = foundUsingValue
}

/// The element's textual contents.
Expand Down
4 changes: 2 additions & 2 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public class Session {
return PollResult(value: elementId, success: elementId != nil)
}.value

return elementId.map { Element(session: self, id: $0) }
return elementId.map { Element(session: self, id: $0, foundUsing: using, foundUsingValue: value) }
}

/// Finds elements by id, starting from the root.
Expand Down Expand Up @@ -225,7 +225,7 @@ public class Session {
return try poll(timeout: retryTimeout ?? defaultRetryTimeout) {
do {
// Allow errors to bubble up unless they are specifically saying that the element was not found.
return PollResult.success(try webDriver.send(request).value.map { Element(session: self, id: $0.element) })
return PollResult.success(try webDriver.send(request).value.map { Element(session: self, id: $0.element, foundUsing: using, foundUsingValue: value) })
} catch let error as ErrorResponse where error.status == .noSuchElement {
// Follow the WebDriver spec and keep polling if no elements are found
return PollResult.failure([])
Expand Down
31 changes: 30 additions & 1 deletion Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,36 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssertEqual($0.value, "myElement.name")
return ResponseWithValue(.init(element: "myElement"))
}
XCTAssertNotNil(try session.findElement(byName: "myElement.name"))
let element = try session.findElement(byName: "myElement.name")
XCTAssertNotNil(element)
if let element {
XCTAssertEqual(element.foundUsing, "name")
XCTAssertEqual(element.foundUsingValue, "myElement.name")
}

mockWebDriver.expect(path: "session/mySession/element", method: .post, type: Requests.SessionElement.self) {
XCTAssertEqual($0.using, "accessibility id")
XCTAssertEqual($0.value, "myElement2.accessibilityId")
return ResponseWithValue(.init(element: "myElement2"))
}
let element2 = try session.findElement(byAccessibilityId: "myElement2.accessibilityId")
XCTAssertNotNil(element2)
if let element2 {
XCTAssertEqual(element2.foundUsing, "accessibility id")
XCTAssertEqual(element2.foundUsingValue, "myElement2.accessibilityId")
}

mockWebDriver.expect(path: "session/mySession/element", method: .post, type: Requests.SessionElement.self) {
XCTAssertEqual($0.using, "id")
XCTAssertEqual($0.value, "myElement3.Id")
return ResponseWithValue(.init(element: "myElement3"))
}
let element3 = try session.findElement(byId: "myElement3.Id")
XCTAssertNotNil(element3)
if let element3 {
XCTAssertEqual(element3.foundUsing, "id")
XCTAssertEqual(element3.foundUsingValue, "myElement3.Id")
}

mockWebDriver.expect(path: "session/mySession/element/active", method: .post, type: Requests.SessionActiveElement.self) {
ResponseWithValue(.init(element: "myElement"))
Expand Down

0 comments on commit fb27464

Please sign in to comment.