mobile.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from .command import Command
  18. class Mobile(object):
  19. class ConnectionType(object):
  20. def __init__(self, mask):
  21. self.mask = mask
  22. @property
  23. def airplane_mode(self):
  24. return self.mask % 2 == 1
  25. @property
  26. def wifi(self):
  27. return (self.mask / 2) % 2 == 1
  28. @property
  29. def data(self):
  30. return (self.mask / 4) > 0
  31. ALL_NETWORK = ConnectionType(6)
  32. WIFI_NETWORK = ConnectionType(2)
  33. DATA_NETWORK = ConnectionType(4)
  34. AIRPLANE_MODE = ConnectionType(1)
  35. def __init__(self, driver):
  36. self._driver = driver
  37. @property
  38. def network_connection(self):
  39. return self.ConnectionType(self._driver.execute(Command.GET_NETWORK_CONNECTION)['value'])
  40. def set_network_connection(self, network):
  41. """
  42. Set the network connection for the remote device.
  43. Example of setting airplane mode::
  44. driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
  45. """
  46. mode = network.mask if isinstance(network, self.ConnectionType) else network
  47. return self.ConnectionType(self._driver.execute(
  48. Command.SET_NETWORK_CONNECTION, {
  49. 'name': 'network_connection',
  50. 'parameters': {'type': mode}})['value'])
  51. @property
  52. def context(self):
  53. """
  54. returns the current context (Native or WebView).
  55. """
  56. return self._driver.execute(Command.CURRENT_CONTEXT_HANDLE)
  57. @property
  58. def contexts(self):
  59. """
  60. returns a list of available contexts
  61. """
  62. return self._driver.execute(Command.CONTEXT_HANDLES)
  63. @context.setter
  64. def context(self, new_context):
  65. """
  66. sets the current context
  67. """
  68. self._driver.execute(Command.SWITCH_TO_CONTEXT, {"name": new_context})