Skip to content

Instantly share code, notes, and snippets.

@bootstraponline
Created January 21, 2013 21:53
Show Gist options
  • Save bootstraponline/4589830 to your computer and use it in GitHub Desktop.
Save bootstraponline/4589830 to your computer and use it in GitHub Desktop.
# Implement is focused app using dumpsys window windows
def is_focused_app app
output = `#{adb_command} shell dumpsys window windows`
return false if output.nil?
target = nil
output.each_line do |line|
if line.include? 'mFocusedApp'
target = line
break
end
end
return false if target.nil?
focused = target.match(/\/\.([^}]+)}/)
return false if focused.nil?
focused[1] == app
end
puts is_focused_app 'MyActivity'
@jonasmaturana
Copy link

Cool! The regex has to be changed a bit though since the activity can be shown in one of these three ways:

mFocusedApp=... u0 x.y.z/MyActivity}}}
mFocusedApp=... u0 x.y.z/.MyActivity}}}
mFocusedApp=... u0 x.y.z/.foo.bar.MyActivity}}}

I think this would work [\.\/]([^.\/\}]+).

By using Enumerable.grep and including mFocusedApp we can do all the grepping in one line:

def focused_activity
  `#{adb_command} shell dumpsys window windows`.each_line.grep(/mFocusedApp.+[\.\/]([^.\/\}]+)}/){$1}.first
end

I think it more general focused_activty would be useful but a helper method like your is_focused_activity? is a good helper

@bootstraponline
Copy link
Author

@jonasmaturana I think your focused_activity is better. Then something like this can be written:

def wait_for_activity activity
  retriable :tries => 5, :interval => 2 do
    focused = focused_activity
    raise "Activity #{activity} not found. Current activity is #{focused}" if focused != activity
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment