Skip to content

Commit

Permalink
add application alert handling and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Jul 26, 2023
1 parent c2331c3 commit 38b8968
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
Binary file not shown.
20 changes: 19 additions & 1 deletion xcuitest-sample-proj/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ struct CarouselView: View {
@State var sliderValue: Double = .zero
@State var selectedDate = Date()
@State var selectedPickerValue = "None"
@State private var showingAlert = false

var body: some View {
VStack {
ScrollView {
ScrollView(.horizontal) {
LazyHStack(spacing: 16) {
ForEach(1...10, id: \.self) { index in
Expand Down Expand Up @@ -104,6 +105,23 @@ struct CarouselView: View {
.pickerStyle(.wheel)
.frame(height: 150)
.accessibilityIdentifier("picker")
.border(.black)

Spacer()
.frame(height: 30)

Button("Show Alert") {
showingAlert = true
}
.foregroundColor(.white)
.padding()
.frame(width: 200, height: 50)
.background(Color.blue)
.cornerRadius(12)
.accessibilityIdentifier("alert-button")
.alert(isPresented: $showingAlert) {
Alert(title: Text("This is an alert"), message: Text("Please take care!"), primaryButton: .default(Text("Accept")), secondaryButton: .cancel(Text("Close")))
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions xcuitest-sample-projUITests/Helpers/Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,38 @@ class Elements {
start.press(forDuration: 0.05, thenDragTo: end)
}

/// Set a picker to a specific value
static func setPickerValue(_ element: XCUIElement, _ value: String) {
element.adjust(toPickerWheelValue: value)
}

// 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")
var alertButton: XCUIElement
if button == "" {
alertButton = alert.buttons.firstMatch
XCTAssertTrue(alertButton.exists, "No button was found in the presented alert")
} else {
alertButton = alert.buttons[button]
XCTAssertTrue(alertButton.exists, "No button with identifier: `\(button)` was found in the presented alert")
}
alertButton.tap()
Elements.waitUntilElementDisappears(alertButton, 2)
}

/// Handle application alert targetting specific button
static func handleAppAlert(_ button: String) {
let alert = BaseTest().getApp().alerts.firstMatch
handleAppAlert(alert, button)
}

/// Agnostically handle application alert - just wait for alert and tap the first button on it
static func handleAppAlert() {
handleAppAlert("")
}

// MARK: - Wait functions

/// Wait for element to exist
Expand Down
1 change: 1 addition & 0 deletions xcuitest-sample-projUITests/Pages/FirstPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class FirstPage: BasePage {
var slider: XCUIElement { app.sliders["slider"] }
var picker: XCUIElement { app.pickers["picker"] }
var pickerWheel: XCUIElement { picker.pickerWheels.firstMatch }
var triggerAlertButton: XCUIElement { app.buttons["alert-button"] }
}
20 changes: 20 additions & 0 deletions xcuitest-sample-projUITests/SampleAppUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,24 @@ final class SampleAppUITests: BaseTest {
Elements.setSlider(firstPage.slider, 0.8)
print(firstPage.slider.textFromValue)
}

func testCloseAppAlertWithAlertAndButtonName() {
let firstPage = FirstPage(app: getApp())
firstPage.triggerAlertButton.tap()
Elements.handleAppAlert(getApp().alerts.firstMatch, "Close")
}

func testCloseAppAlertWithButtonName() {
let firstPage = FirstPage(app: getApp())
firstPage.triggerAlertButton.tap()
Elements.handleAppAlert("Close")
firstPage.triggerAlertButton.tap()
Elements.handleAppAlert("Accept")
}

func testCloseAppAlertAgnostic() {
let firstPage = FirstPage(app: getApp())
firstPage.triggerAlertButton.tap()
Elements.handleAppAlert("")
}
}

0 comments on commit 38b8968

Please sign in to comment.