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

Store element's found method #149

Closed
wants to merge 2 commits into from
Closed
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
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
12 changes: 6 additions & 6 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Session {
}
}
}

public var location: Location {
get throws {
let response = try webDriver.send(Requests.SessionLocation.Get(session: id))
Expand Down 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) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original query might also have had a startingAt value, so immediately rerunning the query using foundUsing and foundUsingValue would not return the same result.

}

/// 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 Expand Up @@ -344,7 +344,7 @@ public class Session {
try webDriver.send(Requests.SessionSource(session: id)).value
}
}

/// - Returns: Current window handle
public var windowHandle: String {
get throws {
Expand All @@ -358,8 +358,8 @@ public class Session {
try webDriver.send(Requests.SessionLocation.Post(session: id, location: location))
}

public func setLocation(latitude: Double, longitude: Double, altitude: Double) throws {
try setLocation(Location(latitude: latitude, longitude: longitude, altitude: altitude))
public func setLocation(latitude: Double, longitude: Double, altitude: Double) throws {
try setLocation(Location(latitude: latitude, longitude: longitude, altitude: altitude))
}

/// - Returns: Array of window handles
Expand Down
40 changes: 34 additions & 6 deletions 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 Expand Up @@ -97,7 +126,7 @@ class APIToRequestMappingTests: XCTestCase {
}
try session.buttonUp(button: .right)
}

func testSessionOrientation() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
Expand Down Expand Up @@ -250,10 +279,10 @@ class APIToRequestMappingTests: XCTestCase {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
let location = Location(latitude: 5, longitude: 20, altitude: 2003)

mockWebDriver.expect(path: "session/mySession/location", method: .post)
try session.setLocation(location)

mockWebDriver.expect(path: "session/mySession/location", method: .get, type: Requests.SessionLocation.Get.self) {
ResponseWithValue(.init(latitude: 5, longitude: 20, altitude: 2003))
}
Expand Down Expand Up @@ -281,14 +310,13 @@ class APIToRequestMappingTests: XCTestCase {

let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")

mockWebDriver.expect(path: "session/mySession/window_handles", method: .get, type: Requests.SessionWindowHandles.self) {
ResponseWithValue(.init(["myWindow", "myWindow"]))
}
XCTAssert(try session.windowHandles == ["myWindow", "myWindow"])
}


func testElementDoubleClick() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
Expand Down
Loading