除了.exists函数之外,有没有办法检查XCTest中是否存在元素?

时间:2021-07-17 16:25:45

Currently, I am working in XCTest iOS framework and using .exists function to check the element presence. Want to know if there is any other way to check the presence of element on UI as .exists is getting problem. Tests get successful on the first run but when it runs second time, it gets failed because script clicks the element which is not exist on the UI might be because elements loaded first time in app remains hidden but exists.

目前,我正在使用XCTest iOS框架并使用.exists函数来检查元素的存在。想知道是否有任何其他方法来检查UI上元素的存在,因为.exists正在遇到问题。测试在第一次运行时获得成功,但是当它第二次运行时,它会失败,因为脚本单击UI上不存在的元素可能是因为第一次在应用程序中加载的元素仍然隐藏但存在。

Please let me know any function which checks the current screen elements presence.

请让我知道检查当前屏幕元素存在的任何功能。

2 个解决方案

#1


0  

I too am looking for a way to wait until an element appears instead of using the sleep method. sometimes UI elements take longer to load, but that doesn't mean it won't appear, eventually. At which point you can check use the .exists

我也在寻找一种等待元素出现而不是使用sleep方法的方法。有时UI元素需要更长时间才能加载,但这并不意味着它最终不会出现。此时,您可以检查使用.exists

There is a XCUIApplication Class Reference, not hosted by apple, but its in OBJ-C: http://masilotti.com/xctest-documentation/index.html

有一个XCUIApplication类参考,不是由apple托管,而是在OBJ-C中:http://masilotti.com/xctest-documentation/index.html

UPDATE: I think I found a solution from this link Delay/Wait in a test case of Xcode UI testing

更新:我想我在Xcode UI测试的测试用例中找到了这个链接延迟/等待的解决方案

Using the NSPredicate class worked for me.

使用NSPredicate类为我工作。

    let welcomePage = app.staticTexts["Landing Page"]
    let existsPredicate = NSPredicate(format: "exists == 1")

    expectationForPredicate(existsPredicate, evaluatedWithObject: LandingPageLoad, handler: nil)
    waitForExpectationsWithTimeout(5, handler: nil)
  • First create a constant that of a value or object you're waiting for
  • 首先创建一个您正在等待的值或对象的常量
  • Create another constant using the NSPredicate class to compare a value of an object
  • 使用NSPredicate类创建另一个常量来比较对象的值
  • Then use the expectationForPredicate method along with vars
  • 然后使用expectationForPredicate方法和vars
  • And lastly give the handler a timeout upper limit
  • 最后给处理程序一个超时上限

#2


0  

I refer to this answer and this XCUIElementQuery function can handle the case the element is not created:

我引用这个答案,这个XCUIElementQuery函数可以处理未创建元素的情况:

 open func matching(identifier: String) -> XCUIElementQuery

ref: Testing if an element is visible with XCode 7 UITest

ref:使用XCode 7 UITest测试元素是否可见

#1


0  

I too am looking for a way to wait until an element appears instead of using the sleep method. sometimes UI elements take longer to load, but that doesn't mean it won't appear, eventually. At which point you can check use the .exists

我也在寻找一种等待元素出现而不是使用sleep方法的方法。有时UI元素需要更长时间才能加载,但这并不意味着它最终不会出现。此时,您可以检查使用.exists

There is a XCUIApplication Class Reference, not hosted by apple, but its in OBJ-C: http://masilotti.com/xctest-documentation/index.html

有一个XCUIApplication类参考,不是由apple托管,而是在OBJ-C中:http://masilotti.com/xctest-documentation/index.html

UPDATE: I think I found a solution from this link Delay/Wait in a test case of Xcode UI testing

更新:我想我在Xcode UI测试的测试用例中找到了这个链接延迟/等待的解决方案

Using the NSPredicate class worked for me.

使用NSPredicate类为我工作。

    let welcomePage = app.staticTexts["Landing Page"]
    let existsPredicate = NSPredicate(format: "exists == 1")

    expectationForPredicate(existsPredicate, evaluatedWithObject: LandingPageLoad, handler: nil)
    waitForExpectationsWithTimeout(5, handler: nil)
  • First create a constant that of a value or object you're waiting for
  • 首先创建一个您正在等待的值或对象的常量
  • Create another constant using the NSPredicate class to compare a value of an object
  • 使用NSPredicate类创建另一个常量来比较对象的值
  • Then use the expectationForPredicate method along with vars
  • 然后使用expectationForPredicate方法和vars
  • And lastly give the handler a timeout upper limit
  • 最后给处理程序一个超时上限

#2


0  

I refer to this answer and this XCUIElementQuery function can handle the case the element is not created:

我引用这个答案,这个XCUIElementQuery函数可以处理未创建元素的情况:

 open func matching(identifier: String) -> XCUIElementQuery

ref: Testing if an element is visible with XCode 7 UITest

ref:使用XCode 7 UITest测试元素是否可见