Calabash Android: how to send app to the backgroun

2019-03-05 15:03发布

问题:

Is there a way in Calabash Android where I can send my app to the background? In other words, simulate the device/hardware Home button?

Secondly, can the app be brought back into the foreground?

回答1:

This can be done in the following way:

Then /^I go home$/ do
  system "#{default_device.adb_command} shell input keyevent KEYCODE_HOME"
end


P.S. You can also add sleep <some_value_in_seconds> if necessary, after system "..." line.



回答2:

As far as I know, that action does not currently exist. You can find (mostly) all of the available operations here https://github.com/calabash/calabash-android/tree/master/ruby-gem/lib/calabash-android. Most of the interesting options are in the operations.rb file. The performAction method would be the method most likely to help you as it has a 'go_back' and 'press_menu' feature, but currently no 'go_home' feature. When you're in a calabsh console, you can type performAction 'list_actions' to see all possible actions. I'm not sure if it's a reasonable workaround, but you could try something like this:

until (query "*").empty? do
  performAction 'go_back'
end

This will sipmly press the back button until you've arrived on the home screen. If you'd like to get back to your app, however, you'd need to re-run start_test_server_in_background as you will not be able to get any query information from the home screen. Anyways, good luck and I hope I could help out at least a little!