วิธีดึงข้อมูลตัวแปรจากป้ายกำกับในการทดสอบ UI

นี่คือสิ่งที่ฉันมีจนถึงตอนนี้ และฉันแค่พยายามรับผลลัพธ์ที่ส่งออกในป้ายกำกับ และทดสอบกับผลลัพธ์ที่ตั้งไว้:

func testExample() {

    let app = XCUIApplication()
    let enterRomanNumeralsHereTextField = app.textFields["Enter roman numerals here"]
    enterRomanNumeralsHereTextField.tap()
    let convertButton = app.buttons["Convert"]

    //1
    enterRomanNumeralsHereTextField.typeText("LL")
    convertButton.tap()
    XCTAssertTrue(app.staticTexts.element(matching:.any, identifier: "Roman Numerals").label == "NotValidRomanNumber")


    //2
    enterRomanNumeralsHereTextField.typeText("LXXXIX")
    convertButton.tap()
    XCTAssertEqual(app.otherElements.containing(.staticText, identifier:"Roman Numerals").element.value as! String, "89")

    //3
    enterRomanNumeralsHereTextField.typeText("")
    enterRomanNumeralsHereTextField.typeText("LXXVIII")
    convertButton.tap()
    XCTAssertEqual(app.otherElements.containing(.staticText, identifier:"Roman Numerals").element.value as! String, "")



    // Use recording to get started writing UI tests.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

ความพยายามครั้งแรก (ป้ายกำกับ 1) ทำให้ฉันเกิดข้อผิดพลาดในการสร้างโดยบอกว่า "ไม่สามารถเรียกค่าของประเภทที่ไม่ใช่ฟังก์ชัน XCUIElement ได้ ความพยายามครั้งที่สองสร้างขึ้น แต่การทดสอบล้มเหลวเพราะแม้ว่าฉลากจะสร้างค่าที่ถูกต้องจริง ๆ แต่การทดสอบก็อ่านฉลากเป็น ว่างเปล่า ซึ่งนำเราไปสู่ความพยายามครั้งที่สามของฉันซึ่งผ่านการทดสอบเพราะฉันเปรียบเทียบกับฉลากเปล่าซึ่งเป็นสิ่งที่แสดงให้เห็น

อย่างที่บอกไปข้างต้น ฉันแค่สงสัยว่าจะดึงค่าป้ายกำกับที่เป็นผลมาจาก "การคำนวณ" หรือการกดปุ่มได้อย่างไร


person KoolaidLips    schedule 31.05.2017    source แหล่งที่มา


คำตอบ (1)


น่าเสียดายที่เมื่อมีการเข้าถึง UILabel ใน UITest คุณสมบัติ XCUIElement's value ไม่ได้ถูกตั้งค่าด้วยคุณสมบัติ text ของ UILabel มันว่างเปล่าเสมอ

XCUIElement ยังมีคุณสมบัติ label ที่คุณสามารถใช้เพื่อวัตถุประสงค์ของคุณได้ คุณเพียงแต่ต้องแน่ใจว่าคุณไม่ได้ตั้งค่า accessibilityLabel บน UILabel ของคุณ ใช้ accessibilityIdentifier แทน:

ในแอปของคุณ:

label.accessibilityIdentifier = "Roman Numerals"

ใน UITest ของคุณ:

XCTAssertEqual(app.staticTexts.element(matching:.any, identifier: "Roman Numerals").label, "89")
person joern    schedule 10.06.2017
comment
ฉันมีปัญหากับมันมากจริงๆ ขอบคุณ. - person andrei; 01.11.2017
comment
@joem คุณทราบได้อย่างไรว่าควรใช้คุณสมบัติใด ?? - person tomkaith13; 13.01.2018
comment
@ tomkaith13 ฉันได้ดู ˋXCUIElementAttributesˋ ซึ่ง ˋXCUIElementˋ เป็นไปตามนั้น โปรโตคอลนั้นกำหนดคุณลักษณะที่เปิดเผยสำหรับองค์ประกอบ UI ทั้งหมด จากนั้นฉันก็ดูค่าแอตทริบิวต์เหล่านั้นในกรณีที่คุณค้นหา ˋUILabelˋ - person joern; 13.01.2018
comment
@joem ขอบคุณมากครับ ฉันจะดูที่นั่นเมื่อฉันติดขัดเช่นกัน - person tomkaith13; 13.01.2018