2

I'm following the Swift example under the heading Is there a way to return a specific element from the FAQ to try to retrieve a property from an element.

I copied the example directly from the FAQ but after calling performAction, textValue still has its original value. In fact, whatever I set the inout parameter to in the action block, the variable retains its original value once the action returns.

What am I missing? Here is the code I have:

func grey_getText(inout text: String) -> GREYActionBlock {
    return GREYActionBlock.actionWithName("get text",
      constraints: grey_respondsToSelector(Selector("text")),
      performBlock: { element, errorOrNil -> Bool in
          text = element.text
          print("in block: \(text)")
          return true
    })
}

and in the test method:

var textValue = ""
let domainField = EarlGrey().selectElementWithMatcher(grey_text("Floor One"))

domainField.assertWithMatcher(grey_sufficientlyVisible())
domainField.performAction(grey_getText(&textValue))

print("outside block: \(textValue)")

prints

in block: Floor One
outside block: 

I'm using XCode version 7.3.1

Joe Taylor
  • 2,145
  • 1
  • 19
  • 35
  • This seems like something to do with the way you are using blocks, do you get the same result if you assign `GREYActionBlock.actionWithName(...` directly to a var and use it (instead of calling the function `grey_getText ` to get it)? – Gautam Jul 07 '16 at 18:24

2 Answers2

3

Checkout the code in this pull request for the correct implementation of grey_getText. https://github.com/google/EarlGrey/pull/139

The EarlGrey team is aware the docs are out of date and we're working on a solution.

3
func incrementer(inout x: Int) -> () -> () {
  print("in incrementer \(x)")
  func plusOne() {
    print("in plusOne before \(x)")
    x += 1;
    print("in plusOne after \(x)")
  }
  return plusOne
}

var y = 0;
let f = incrementer(&y)
print("before \(y)")
f();
print("after \(y)")

Although we would expect y to be 1 at the end of execution, y remains 0. Here is the actual output:

in incrementer 0
before 0
in plusOne before 0
in plusOne after 1
after 0

This is because in-out parameter is not "call-by-reference", but "call-by-copy-restore". as the PR pointed by bootstraponline specifies.

Gautam
  • 286
  • 1
  • 3