0

I have a simple routine to delete a number of records from a screen by swiping left on them. It only deletes records in a certain state, i.e. ready to submit. The top record might not necessarily be in that state, so I cannot use a fixed y co-ordinate. Instead, I use the y co-ordinate of the first record that I find in that state.

Here is the code I am trying to execute:

while query("* marked:'SUBMIT'").count > 0
  y = query("UIButtonLabel marked:'SUBMIT'")[0]["rect"]["center_y"]
  uia('target.dragFromToForDuration({x:"481.5", y:"#{y}"}, {x:"350", y:"#{y}"}, "1")')
  touch("* marked:'Delete'")
  touch("view:'_UIAlertControllerActionView' marked:'Delete'")
end

The problem is that the variable y seems to be inaccessible buried within all those quote marks. The method succeeds, but the UI does not respond as expected. I tried the method in the console. When I substitute the variable for an integer, it works as expected, but the variable does not. I tried making it a global variable with no difference. I tried using a tap command instead to tap the button, which again failed silently.

irb(main):006:0> y = query("UIButtonLabel marked:'SUBMIT'")[0]["rect"]["center_y"]
218.5
irb(main):007:0> y
218.5
irb(main):008:0> uia('target.dragFromToForDuration({x:"481.5", y:"#{y}"}, {x:"350", y:"#{y}"}, "1")')
{
  "status" => "success",
   "index" => 1
}

Is there a way to reference a variable within this method in this way?

Uncle Gus
  • 61
  • 1
  • 7

1 Answers1

0

Strings created with '' will not perform interpolation. So your '...#{y}...' is literally going to have that text in it.

You can use double quotes (""), but then you'd have to escape everything:

uia("target.dragFromToForDuration({x:\"481.5\", y:\"#{y}\"}, {x:\"350\", y:\"#{y}\"}, \"1\")")

You can use %Q to avoid the escaping:

uia(%Q(target.dragFromToForDuration({x:"481.5", y:"#{y}"}, {x:"350", y:"#{y}"}, "1")))

I don't know this library, but it may also work like this:

uia("target.dragFromToForDuration({x:'481.5', y:'#{y}'}, {x:'350', y:'#{y}'}, '1')")
Nick Veys
  • 23,458
  • 4
  • 47
  • 64