Skip to content

Commit

Permalink
Fix code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
piotruela committed Sep 20, 2024
1 parent 2a786cf commit c8732c2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ class AutomatorServer(private val automation: Automator) : NativeAutomatorServer
uiSelector = request.selector.toUiSelector(),
bySelector = request.selector.toBySelector(),
index = request.selector.instance?.toInt() ?: 0,
maxScrolls = request.maxScrolls,
maxScrolls = request.maxScrolls
)
} else if (request.androidSelector != null) {
automation.scrollTo(
uiSelector = request.androidSelector.toUiSelector(),
bySelector = request.androidSelector.toBySelector(),
index = request.androidSelector.instance?.toInt() ?: 0,
maxScrolls = request.maxScrolls,
maxScrolls = request.maxScrolls
)
} else {
throw PatrolException("scrollTo(): neither selector nor androidSelector are set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,45 +326,51 @@
}
}

func scrollTo(on selector: Selector, inApp bundleId: String, atIndex index: Int, maxScrolls scrolls: Int) throws {
var view = createLogMessage(element: "view", from: selector)
view += " in app \(bundleId)"

try runAction("scrolling to \(view)") {
let app = try self.getApp(withBundleId: bundleId)

let query = app.descendants(matching: .any).matching(selector.toNSPredicate())

Logger.shared.i("waiting for existence of \(view)")
guard
let element = self.waitFor(
func scrollTo(
on selector: Selector, inApp bundleId: String, atIndex index: Int, maxScrolls scrolls: Int
) throws {
var view = createLogMessage(element: "view", from: selector)
view += " in app \(bundleId)"

try runAction("scrolling to \(view)") {
let app = try self.getApp(withBundleId: bundleId)

let query = app.descendants(matching: .any).matching(selector.toNSPredicate())

Logger.shared.i("waiting for existence of \(view)")
guard
let element = self.waitFor(
query: query, index: selector.instance ?? 0, timeout: self.timeout)
else {
throw PatrolError.viewNotExists(view)
}
else {
throw PatrolError.viewNotExists(view)
}

try self.scrollToElement(element: element, bundleId: bundleId, index: index, scrolls: scrolls)
try self.scrollToElement(
element: element, bundleId: bundleId, index: index, scrolls: scrolls)
}
}

func scrollTo(on selector: IOSSelector, inApp bundleId: String, atIndex index: Int, maxScrolls scrolls: Int) throws {
var view = createLogMessage(element: "view", from: selector)
view += " in app \(bundleId)"

try runAction("scrolling to \(view)") {
let app = try self.getApp(withBundleId: bundleId)
func scrollTo(
on selector: IOSSelector, inApp bundleId: String, atIndex index: Int, maxScrolls scrolls: Int
) throws {
var view = createLogMessage(element: "view", from: selector)
view += " in app \(bundleId)"

try runAction("scrolling to \(view)") {
let app = try self.getApp(withBundleId: bundleId)

let query = app.descendants(matching: .any).matching(selector.toNSPredicate())
let query = app.descendants(matching: .any).matching(selector.toNSPredicate())

Logger.shared.i("waiting for existence of \(view)")
guard
let element = self.waitFor(
Logger.shared.i("waiting for existence of \(view)")
guard
let element = self.waitFor(
query: query, index: selector.instance ?? 0, timeout: self.timeout)
else {
throw PatrolError.viewNotExists(view)
}
else {
throw PatrolError.viewNotExists(view)
}

try self.scrollToElement(element: element, bundleId: bundleId, index: index, scrolls: scrolls)
try self.scrollToElement(
element: element, bundleId: bundleId, index: index, scrolls: scrolls)
}
}

Expand Down Expand Up @@ -931,31 +937,33 @@

element.typeText(delete + data)
}

private func scrollToElement(element: XCUIElement, bundleId: String, index: Int, scrolls: Int) throws {
var attempts = 0 // Track the number of scrolls
while try isVisible(element: element, bundleId: bundleId, index: index) == false
{
if attempts >= scrolls {
throw PatrolError.viewNotExists("Element not found after \(scrolls) scrolls.")
}
let app = try self.getApp(withBundleId: bundleId)

let startCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.9))
let endCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.1))
startCoordinate.press(forDuration: 0.1, thenDragTo: endCoordinate)

attempts += 1 // Increment the scroll count after each scroll
}
}

private func isVisible(element: XCUIElement, bundleId: String, index: Int) throws-> Bool

private func scrollToElement(element: XCUIElement, bundleId: String, index: Int, scrolls: Int)
throws
{
var attempts = 0 // Track the number of scrolls
while try isVisible(element: element, bundleId: bundleId, index: index) == false {
if attempts >= scrolls {
throw PatrolError.viewNotExists("Element not found after \(scrolls) scrolls.")
}
let app = try self.getApp(withBundleId: bundleId)

guard element.exists && element.isHittable && !CGRectIsEmpty(element.frame) else { return false }

return CGRectContainsRect(app.windows.element(boundBy: index).frame, element.frame)

let startCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.9))
let endCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.1))
startCoordinate.press(forDuration: 0.1, thenDragTo: endCoordinate)

attempts += 1 // Increment the scroll count after each scroll
}
}

private func isVisible(element: XCUIElement, bundleId: String, index: Int) throws -> Bool {
let app = try self.getApp(withBundleId: bundleId)

guard element.exists && element.isHittable && !CGRectIsEmpty(element.frame) else {
return false
}

return CGRectContainsRect(app.windows.element(boundBy: index).frame, element.frame)
}

private func isSimulator() -> Bool {
Expand Down

0 comments on commit c8732c2

Please sign in to comment.