Skip to content

Commit

Permalink
add fully visible check on XCUIElement
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Jul 29, 2023
1 parent 0f88e98 commit 9abfadb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
Binary file not shown.
22 changes: 11 additions & 11 deletions xcuitest-sample-proj/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ struct CarouselView: View {

var body: some View {
ScrollView {
let pickerValues = ["None", "Little", "Medium", "Normal", "More", "Many"]
Picker("", selection: $selectedPickerValue) {
ForEach(pickerValues, id: \.self) {
Text($0)
}
}
.pickerStyle(.wheel)
.frame(height: 150)
.accessibilityIdentifier("picker")
.border(.black)

ScrollView(.horizontal) {
LazyHStack(spacing: 16) {
ForEach(1...10, id: \.self) { index in
Expand Down Expand Up @@ -96,17 +107,6 @@ struct CarouselView: View {
.frame(width: 300, height: 50)
.accessibilityIdentifier("slider")

let pickerValues = ["None", "Little", "Medium", "Normal", "More", "Many"]
Picker("", selection: $selectedPickerValue) {
ForEach(pickerValues, id: \.self) {
Text($0)
}
}
.pickerStyle(.wheel)
.frame(height: 150)
.accessibilityIdentifier("picker")
.border(.black)

Spacer()
.frame(height: 30)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,33 @@ extension XCUIElement {
// This can lead to problems and test flakiness as the test will evaluate a query before e.g. view transition has been completed.
return exists && isHittable
}

func isFullyVisible(_ kbPresent: Bool = false) -> Bool {
let appFrame = BaseTest().getApp().frame
let navBar = BaseTest().getApp().navigationBars.firstMatch
let tabBar = BaseTest().getApp().tabBars.firstMatch

var visFrameMinY: CGFloat
if navBar.exists {
visFrameMinY = navBar.frame.maxY
} else {
visFrameMinY = appFrame.minY
}

var visFrameMaxY: CGFloat
if kbPresent {
if BaseTest().getApp().keyboards.firstMatch.exists {
visFrameMaxY = BaseTest().getApp().keyboards.firstMatch.frame.minY
} else {
visFrameMaxY = tabBar.frame.minY
}
} else {
visFrameMaxY = tabBar.frame.minY
}

return self.frame.minX >= appFrame.minX &&
self.frame.maxX <= appFrame.maxX &&
self.frame.minY >= visFrameMinY &&
self.frame.maxY <= visFrameMaxY
}
}
2 changes: 1 addition & 1 deletion xcuitest-sample-projUITests/Helpers/Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Elements {
// MARK: - App alerts handling
/// Handle application alert by element and targetting specific button
static func handleAppAlert(_ alert: XCUIElement, _ button: String) {
XCTAssertTrue(waitForElement(alert, TestConstants.Timeout.medium), "The provided alert element was not found")
XCTAssertTrue(waitForElement(alert, TestConstants.Timeout.medium), "Alert element was not found")
var alertButton: XCUIElement
if button == "" {
alertButton = alert.buttons.firstMatch
Expand Down
7 changes: 7 additions & 0 deletions xcuitest-sample-projUITests/SampleAppUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ final class SampleAppUITests: BaseTest {
firstPage.triggerAlertButton.tap()
Elements.handleAppAlert("")
}

func testElementFullyVisible() {
let firstPage = FirstPage(app: getApp())
print(firstPage.triggerAlertButton.isFullyVisible())
getApp().swipeUp()
print(firstPage.triggerAlertButton.isFullyVisible())
}
}

0 comments on commit 9abfadb

Please sign in to comment.