Skip to content

Commit

Permalink
rework the queries examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Aug 11, 2023
1 parent 81fdd02 commit 3c286d1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
64 changes: 42 additions & 22 deletions xcuitest-sample-proj/Views/QueriesScreenView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,58 @@
import SwiftUI

struct QueriesScreenView: View {
@State private var updateableTextValue: String = "FirstStackText1"
@State private var isButtonVisible = true

var body: some View {
ScrollView {
AccessibilityGroup(identifier: "first-group") {
Text("Descendant 1")
.accessibilityIdentifier("first-stack-text")
Text("Descendant 2")
.accessibilityIdentifier("first-stack-text")
AccessibilityGroup(identifier: "first-group-child") {
Text("Descendant 3")
.accessibilityIdentifier("first-stack-text")
Text("Descendant 4")
.accessibilityIdentifier("first-stack-text")
AccessibilityGroup(identifier: "first-group-child-child") {
Text("Descendant 5")
.accessibilityIdentifier("first-stack-text")
LazyVStack {
Text("FirstStackText")
.accessibilityIdentifier("FirstStackText")
Text(updateableTextValue)
.accessibilityIdentifier("FirstStackText")
.onTapGesture {
// Change the text value when tapped
updateableTextValue = "Tapped"
}
LazyVStack {
Text("FirstStackText3")
.accessibilityIdentifier("FirstStackText")
Text("FirstStackText4")
.accessibilityIdentifier("FirstStackText")
Text("FirstStackText5")
.accessibilityIdentifier("FirstStackText")
LazyVStack {
Text("FirstStackText6")
.accessibilityIdentifier("FirstStackText")
}
.border(.green)
.padding()
}
.border(.blue)
.accessibilityIdentifier("FirstStackFirstChildStack")
.border(.green)
.padding()
}
.border(.black)
.accessibilityIdentifier("FirstStack")
.padding()

Spacer()

AccessibilityGroup(identifier: "second-group") {
Text("SECOND STACK")
LazyVStack {
if isButtonVisible {
Button(action: {
isButtonVisible = false
}) {
LazyVStack{
Image(systemName: "heart.fill")
.accessibilityIdentifier("TestImage")
}
}
.accessibilityIdentifier("SecondStackButton")
}
}
.border(.black)
.accessibilityIdentifier("SecondStack")

}
.accessibilityIdentifier("queries-view")
.accessibilityIdentifier("QueriesScrollView")

}
}

Expand Down
55 changes: 54 additions & 1 deletion xcuitest-sample-projUITests/SampleAppUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,65 @@ final class SampleAppUITests: BaseTest {
}

func testWaitForElementBySuppliedPredicate() {
// Passes
Element.waitForElementPredicate(firstScreen.carousel, 2, NSPredicate(format: "exists == true"))
// Fails
Element.waitForElementPredicate(firstScreen.carousel, 2, NSPredicate(format: "exists == false"))
}

func testQueriesExamples() {
tabBar.openFourthPage()
printPageSource()
// Get descendant element from a parent element by accessibility identifier - variant 1
// Simple element locator for accessibilityIdentifier or label
// From parent XCUIElement get the first matching element of XCUIElement.ElementType
let firstStack = getApp().otherElements["FirstStack"]
XCTAssert(firstStack.exists)
let secondStack = getApp().otherElements["SecondStack"]
XCTAssert(secondStack.exists)
let firstStackChildStack = firstStack.otherElements["FirstStackFirstChildStack"]
XCTAssert(firstStackChildStack.exists)

// Get first matching child/descendant element of XCUIElement.ElementType from parent XCUIElement
// `firstStack.buttons` returns XCUIElementQuery from which we get `firstMatch`
let firstStackFirstText = firstStack.staticTexts.firstMatch
XCTAssert(firstStackFirstText.exists)
XCTAssertEqual(firstStackFirstText.label, "FirstStackText")

// Get last matching child/descendant element of XCUIElement.ElementType from parent XCUIElement
// `firstStack.buttons` returns XCUIElementQuery from which we get `lastMatch`
let firstStackLastText = firstStack.staticTexts.lastMatch
XCTAssert(firstStackLastText.exists)
XCTAssertEqual(firstStackLastText.label, "FirstStackText6")

// Get all elements matching a specific identifier, returns XCUIElementQuery
let firstStackTexts = firstStack.staticTexts.matching(identifier: "FirstStackText")
XCTAssertTrue(firstStackTexts.count == 6)

// Get specific element from XCUIElementQuery by index
// In the example get the second XCUIElement from the XCUIElementQuery
let firstStackSecondText = firstStackTexts.element(boundBy: 1)
XCTAssertEqual(firstStackSecondText.label, "FirstStackText1")
firstStackSecondText.tap()
XCTAssertEqual(firstStackSecondText.label, "Tapped")

// Get direct children of parent element based on their XCUIElement.ElementType
// In the example get all static texts direct children of `firstStack`
let firstStackDirectChildTexts = firstStack.children(matching: .staticText)
XCTAssertEqual(firstStackDirectChildTexts.count, 2)

// Get all descendants of parent element based on their XCUIElement.ElementType
// In the example get all descendant static texts of `firstStack`
let firstStackDescendantTexts = firstStack.descendants(matching: .staticText)
XCTAssertEqual(firstStackDescendantTexts.count, 6)

// Locate direct child of parent element based on its XCUIElement.ElementType and accessibilityIdentifier
let secondStackDescendantButtonById = secondStack.descendants(matching: .button)["SecondStackButton"]
XCTAssertTrue(secondStackDescendantButtonById.exists)

// Filter elements from XCUIElementQuery based on their descendants of XCUIElement.ElementType and specific accessibilityIdentifier
let secondStackButtonsByDescendantTypeAndId = secondStack.buttons.containing(.image, identifier: "TestImage")
XCTAssertTrue(secondStackButtonsByDescendantTypeAndId.count > 0)
secondStackButtonsByDescendantTypeAndId.firstMatch.tap()
XCTAssertFalse(secondStackButtonsByDescendantTypeAndId.firstMatch.exists)
}
}

0 comments on commit 3c286d1

Please sign in to comment.