webdriver.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. import os
  18. import platform
  19. import subprocess
  20. try:
  21. import http.client as http_client
  22. except ImportError:
  23. import httplib as http_client
  24. from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  25. from selenium.common.exceptions import WebDriverException
  26. from selenium.webdriver.support.ui import WebDriverWait
  27. LOAD_TIMEOUT = 5
  28. class WebDriver(RemoteWebDriver):
  29. """
  30. Controls the BlackBerry Browser and allows you to drive it.
  31. :Args:
  32. - device_password - password for the BlackBerry device or emulator you are
  33. trying to drive
  34. - bb_tools_dir path to the blackberry-deploy executable. If the default
  35. is used it assumes it is in the $PATH
  36. - hostip - the ip for the device you are trying to drive. Falls back to
  37. 169.254.0.1 which is the default ip used
  38. - port - the port being used for WebDriver on device. defaults to 1338
  39. - desired_capabilities: Dictionary object with non-browser specific
  40. capabilities only, such as "proxy" or "loggingPref".
  41. Note: To get blackberry-deploy you will need to install the BlackBerry
  42. WebWorks SDK - the default install will put it in the $PATH for you.
  43. Download at https://developer.blackberry.com/html5/downloads/
  44. """
  45. def __init__(self, device_password, bb_tools_dir=None,
  46. hostip='169.254.0.1', port=1338, desired_capabilities={}):
  47. remote_addr = 'http://{}:{}'.format(hostip, port)
  48. filename = 'blackberry-deploy'
  49. if platform.system() == "Windows":
  50. filename += '.bat'
  51. if bb_tools_dir is not None:
  52. if os.path.isdir(bb_tools_dir):
  53. bb_deploy_location = os.path.join(bb_tools_dir, filename)
  54. if not os.path.isfile(bb_deploy_location):
  55. raise WebDriverException('Invalid blackberry-deploy location: {}'.format(bb_deploy_location))
  56. else:
  57. raise WebDriverException('Invalid blackberry tools location, must be a directory: {}'.format(bb_tools_dir))
  58. else:
  59. bb_deploy_location = filename
  60. """
  61. Now launch the BlackBerry browser before allowing anything else to run.
  62. """
  63. try:
  64. launch_args = [bb_deploy_location,
  65. '-launchApp',
  66. str(hostip),
  67. '-package-name', 'sys.browser',
  68. '-package-id', 'gYABgJYFHAzbeFMPCCpYWBtHAm0',
  69. '-password', str(device_password)]
  70. with open(os.devnull, 'w') as fp:
  71. p = subprocess.Popen(launch_args, stdout=fp)
  72. returncode = p.wait()
  73. if returncode == 0:
  74. # wait for the BlackBerry10 browser to load.
  75. is_running_args = [bb_deploy_location,
  76. '-isAppRunning',
  77. str(hostip),
  78. '-package-name', 'sys.browser',
  79. '-package-id', 'gYABgJYFHAzbeFMPCCpYWBtHAm0',
  80. '-password', str(device_password)]
  81. WebDriverWait(None, LOAD_TIMEOUT)\
  82. .until(lambda x: subprocess.check_output(is_running_args)
  83. .find('result::true'),
  84. message='waiting for BlackBerry10 browser to load')
  85. RemoteWebDriver.__init__(self,
  86. command_executor=remote_addr,
  87. desired_capabilities=desired_capabilities)
  88. else:
  89. raise WebDriverException('blackberry-deploy failed to launch browser')
  90. except Exception as e:
  91. raise WebDriverException('Something went wrong launching blackberry-deploy', stacktrace=getattr(e, 'stacktrace', None))
  92. def quit(self):
  93. """
  94. Closes the browser and shuts down the
  95. """
  96. try:
  97. RemoteWebDriver.quit(self)
  98. except http_client.BadStatusLine:
  99. pass