0

I'm using EarlGrey framework and I want to select the whole text and capture it from the textfield or search bar. Is there a way to do it?

Thanks in advance!

3 Answers3

1

You extracted the value with XCTest APIs method.

The right way can be found in EarlGrey FAQ 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
    }
}
Roman Zakharov
  • 2,185
  • 8
  • 19
0

Finally, found out !

Using application.navigationBars["Navigation bar Name"].searchFields["Label of the textField / Search Bar"].value

This fetched the value from the textfield. One thing to be noted is that, the value retrieved will be of the type "any"

0
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
    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '22 at 06:16