การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest
ทำให้การแตะและการตรวจสอบบนหน้าจอเป็นอัตโนมัติ
การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What UI Tests Do
UI tests drive your app like a real person: they tap, type, and swipe, then check the screen reacted correctly. 📱
A Separate Test Target
UI tests live in their own UI Testing target. Xcode adds it when you create the project or from the test navigator later.
Launching the App
Every UI test starts by launching a fresh app instance with XCUIApplication. This gives you a clean state to work from.
let app = XCUIApplication()
app.launch()Finding Elements
You reach controls through queries like buttons, staticTexts, and textFields. Each query finds elements by their kind on screen.
app.buttons["Add"]Tapping a Button
Call tap on an element to simulate a finger press. The app responds exactly as it would for a real user.
app.buttons["Add"].tap()Typing Text
Tap a field first, then send keystrokes with typeText. This is how you fill in forms during a test.
app.textFields["name"].typeText("Sam")Accessibility Identifiers
Give views a stable accessibilityIdentifier so tests find them even when the visible label changes or is localized.
.accessibilityIdentifier("addButton")Asserting What Appears
Check the result with exists. It returns true when the element is on screen, confirming your action worked.
XCTAssertTrue(app.staticTexts["Done"].exists)Waiting for Elements
If content loads after a delay, use waitForExistence with a timeout. The test pauses until the element shows up. ⏳
label.waitForExistence(timeout: 3)Record to Learn
New to queries? Use Xcode's record button. It writes code as you tap, giving you a starting point to clean up.
Keep UI Tests Few
UI tests are slower than unit tests. Cover key flows like sign-in and checkout, and leave the rest to faster unit tests.
Quick Check
What is the most reliable way for a UI test to find a control?
Recap
You drove the app with XCUITest: launch it, query controls, tap and type, then assert with exists. Real user flows, automated. 🎉
คำถามที่พบบ่อย
บทเรียน “การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest”
ทำให้การแตะและการตรวจสอบบนหน้าจอเป็นอัตโนมัติ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยของ ViewModels
- การทดสอบโค้ดแบบอะซิงโครนัส
- การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest
- การทดสอบด้วยภาพบันทึกและพรีวิว