简体   繁体   English

如何使用 XCUITest 在 SkyFloatingLabelTextField 中获取浮动标题的内容?

[英]How to get the content of floating title in SkyFloatingLabelTextField using XCUITest?

In my XCUITests I need to check content of floating title label in SkyFloatingLabelTextField after validation error.在我的 XCUITests 中,我需要在验证错误后检查 SkyFloatingLabelTextField 中浮动标题标签的内容。

I've set accessibilityIdentifier for SkyFloatingLabelTextField element, so I can tap and enter text into it without any issue, but how can I access its title (titleLabel?) using only XCUIElement?我已经为 SkyFloatingLabelTextField 元素设置了 accessibilityIdentifier ,所以我可以点击并在其中输入文本而不会出现任何问题,但是如何仅使用 XCUIElement 访问其标题(titleLabel?)?

I tried to set titleLabel as accessibilityElement with its own accessibilityIdentifier, but I wasn't able to access the text field in my tests after that.我尝试将 titleLabel 设置为具有自己的可访问性标识符的可访问性元素,但之后我无法访问测试中的文本字段。

You will need add extension SkyFloatingLabelTextField:您将需要添加扩展 SkyFloatingLabelTextField:

extension SkyFloatingLabelTextField {
    override open var accessibilityIdentifier: String? {
        didSet {
            isAccessibilityElement = false
            titleLabel.isAccessibilityElement = true
            titleLabel.accessibilityIdentifier = (accessibilityIdentifier ?? "") + "Title"
        }
    }
}

In controller you need add accessibilityIdentifier:在控制器中,您需要添加accessibilityIdentifier:

@IBOutlet weak var textField: SkyFloatingLabelTextField!
textField.accessibilityIdentifier = "YourTextFieldIdentifier"

If you want get title from textfild in your XCTestCase:如果您想从 XCTestCase 中的 textfild 获取标题:

let app = XCUIApplication()
app.launch()
let elementsQuery = app.scrollViews.otherElements

let getTitle = elementsQuery.textFields["YourTextFieldIdentifier"].staticTexts["YourTextFieldIdentifierTitle"]
            XCTAssertEqual(getTitle.label, "Your set title text")

elementsQuery.textFields["YourTextFieldIdentifier"].tap()
elementsQuery.textFields["YourTextFieldIdentifier"].typeText("Type text")

SkyFloatingLabelTextField继承自UITextField因此任何适用于UITextField方法也应该适用于SkyFloatingLabelTextField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SkyFloatingLabelTextField快速验证textField - How to validate textField in swift using SkyFloatingLabelTextField 如何将文本值预加载到 SkyFloatingLabelTextField - How to preload text value into SkyFloatingLabelTextField iOS XCUITest - 如何使用 xcuitest 从应用切换器中删除应用? - iOS XCUITest - How to remove an app from the app switcher using xcuitest? 如何在 SkyFloatingLabelTextField 中垂直和水平对齐文本? - How to vertically and horizontally align text in SkyFloatingLabelTextField? XCUITest使用Springboard测试本机Callkit UI。 如何获得来电者标签? - XCUITest Testing Native Callkit UI Using Springboard. How to get the caller label? XCUITest-如何检查webview是否已更新其内容? - XCUITest - How to check if webview has updated its content? 给定XCUITest中的文本,如何获取单元格的索引? - How to get the index of a cell given the text inside in XCUITest? 使用 XCUITest 时如何更改模拟器上的日期和时间 - How to change date and time on simulator when using XCUITest 如何使用 XCUITest 测试 UIImageView 是否包含具有特定名称的图像? - How to test if a UIImageView contains an image with a specific name using XCUITest? XCUITest - 如何模拟设备的锁定 - XCUITest - How to simulate locking of the device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM