ios 如何在EarlGrey中捕获/提取文本字段中的文本值?

weylhg0b  于 2022-11-26  发布在  iOS
关注(0)|答案(3)|浏览(133)

我正在使用EarlGrey框架,我想选择整个文本并从文本字段或搜索栏中捕获它。有什么方法可以做到吗?
提前感谢!

ldxq2e6h

ldxq2e6h1#

您使用XCTest API方法提取了该值。
正确的方法可以在EarlGrey常见问题解答https://github.com/google/EarlGrey/blob/master/docs/faq.md中找到

// Swift
//
// Must use a wrapper class to force pass by reference in Swift 3 closures.
// inout params cannot be modified within closures. http://stackoverflow.com/a/28252105
open class Element {
  var text = ""
}

/*
 *  Example Usage:
 *
 *  let element = Element()
 *  domainField.performAction(grey_replaceText("hello.there"))
 *             .performAction(grey_getText(element))
 *
 *  GREYAssertTrue(element.text != "", reason: "get text failed")
 */
public func grey_getText(_ elementCopy: Element) -> GREYActionBlock {
  return GREYActionBlock.action(withName: "get text",
  constraints: grey_respondsToSelector(#selector(getter: UILabel.text))) { element,
                                                                           errorOrNil -> Bool in
        let elementObject = element as? NSObject
        let text = elementObject?.perform(#selector(getter: UILabel.text),
                                          with: nil)?.takeRetainedValue() as? String
        elementCopy.text = text ?? ""
        return true
    }
}
cnh2zyt3

cnh2zyt32#

终于,查出来了!
使用application.navigationBars["Navigation bar Name"].searchFields["Label of the textField / Search Bar"].value
这将从文本字段中提取值。需要注意的是,检索到的值将为**“any”**类型

2guxujil

2guxujil3#

Can anyone write the below code with objective-c?

// Swift
//
// Must use a wrapper class to force pass by reference in Swift 3 closures.
// inout params cannot be modified within closures. http://stackoverflow.com/a/28252105
open class Element {
  var text = ""
}

/*
 *  Example Usage:
 *
 *  let element = Element()
 *  domainField.performAction(grey_replaceText("hello.there"))
 *             .performAction(grey_getText(element))
 *
 *  GREYAssertTrue(element.text != "", reason: "get text failed")
 */
public func grey_getText(_ elementCopy: Element) -> GREYActionBlock {
  return GREYActionBlock.action(withName: "get text",
  constraints: grey_respondsToSelector(#selector(getter: UILabel.text))) { element,
                                                                           errorOrNil -> Bool in
        let elementObject = element as? NSObject
        let text = elementObject?.perform(#selector(getter: UILabel.text),
                                          with: nil)?.takeRetainedValue() as? String
        elementCopy.text = text ?? ""
        return true
    }
}

相关问题