diff --git a/Sources/WebDriver/Element.swift b/Sources/WebDriver/Element.swift index 22f2b94..05f1af4 100644 --- a/Sources/WebDriver/Element.swift +++ b/Sources/WebDriver/Element.swift @@ -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. diff --git a/Sources/WebDriver/Session.swift b/Sources/WebDriver/Session.swift index 69b6a0b..f7efb4d 100644 --- a/Sources/WebDriver/Session.swift +++ b/Sources/WebDriver/Session.swift @@ -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. @@ -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([]) diff --git a/Tests/UnitTests/APIToRequestMappingTests.swift b/Tests/UnitTests/APIToRequestMappingTests.swift index 783f7f8..90ec2d9 100644 --- a/Tests/UnitTests/APIToRequestMappingTests.swift +++ b/Tests/UnitTests/APIToRequestMappingTests.swift @@ -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"))