|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The SFC licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +import os |
| 21 | +import platform |
| 22 | +import time |
| 23 | +import subprocess |
| 24 | + |
| 25 | +from selenium.webdriver.remote.command import Command |
| 26 | +from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver |
| 27 | +from selenium.common.exceptions import WebDriverException |
| 28 | +from selenium.webdriver.support.ui import WebDriverWait |
| 29 | + |
| 30 | +LOAD_TIMEOUT = 5 |
| 31 | + |
| 32 | + |
| 33 | +class WebDriver(RemoteWebDriver): |
| 34 | + """ |
| 35 | + Controls the BlackBerry Browser and allows you to drive it. |
| 36 | +
|
| 37 | + :Args: |
| 38 | + - device_password - password for the BlackBerry device or emulator you are |
| 39 | + trying to drive |
| 40 | + - bb_tools_dir path to the blackberry-deploy executable. If the default |
| 41 | + is used it assumes it is in the $PATH |
| 42 | + - hostip - the ip for the device you are trying to drive. Falls back to |
| 43 | + 169.254.0.1 which is the default ip used |
| 44 | + - port - the port being used for WebDriver on device. defaults to 1338 |
| 45 | + - desired_capabilities: Dictionary object with non-browser specific |
| 46 | + capabilities only, such as "proxy" or "loggingPref". |
| 47 | +
|
| 48 | + Note: To get blackberry-deploy you will need to install the BlackBerry |
| 49 | + WebWorks SDK - the default install will put it in the $PATH for you. |
| 50 | + Download at https://developer.blackberry.com/html5/downloads/ |
| 51 | + """ |
| 52 | + def __init__(self, device_password, bb_tools_dir=None, |
| 53 | + hostip='169.254.0.1', port=1338, desired_capabilities={}): |
| 54 | + remote_addr = 'http://{}:{}'.format(hostip, port) |
| 55 | + |
| 56 | + filename = 'blackberry-deploy' |
| 57 | + if platform.system() == "Windows": |
| 58 | + filename += '.bat' |
| 59 | + |
| 60 | + if bb_tools_dir is not None: |
| 61 | + if os.path.isdir(bb_tools_dir): |
| 62 | + bb_deploy_location = os.path.join(bb_tools_dir, filename) |
| 63 | + if not os.path.isfile(bb_deploy_location): |
| 64 | + raise WebDriverException('Invalid blackberry-deploy location: {}'.format(bb_deploy_location)) |
| 65 | + else: |
| 66 | + raise WebDriverException('Invalid blackberry tools location, must be a directory: {}'.format(bb_tools_dir)) |
| 67 | + else: |
| 68 | + bb_deploy_location = filename |
| 69 | + |
| 70 | + """ |
| 71 | + Now launch the BlackBerry browser before allowing anything else to run. |
| 72 | + """ |
| 73 | + try: |
| 74 | + launch_args = [bb_deploy_location, |
| 75 | + '-launchApp', |
| 76 | + str(hostip), |
| 77 | + '-package-name', 'sys.browser', |
| 78 | + '-package-id', 'gYABgJYFHAzbeFMPCCpYWBtHAm0', |
| 79 | + '-password', str(device_password)] |
| 80 | + |
| 81 | + with open(os.devnull, 'w') as fp: |
| 82 | + p = subprocess.Popen(launch_args, stdout=fp) |
| 83 | + |
| 84 | + returncode = p.wait() |
| 85 | + |
| 86 | + if returncode == 0: |
| 87 | + # wait for the BlackBerry10 browser to load. |
| 88 | + is_running_args = [bb_deploy_location, |
| 89 | + '-isAppRunning', |
| 90 | + str(hostip), |
| 91 | + '-package-name', 'sys.browser', |
| 92 | + '-package-id', 'gYABgJYFHAzbeFMPCCpYWBtHAm0', |
| 93 | + '-password', str(device_password)] |
| 94 | + |
| 95 | + WebDriverWait(None, LOAD_TIMEOUT)\ |
| 96 | + .until(lambda x: subprocess.check_output(is_running_args) |
| 97 | + .find('result::true'), |
| 98 | + message='waiting for BlackBerry10 browser to load') |
| 99 | + |
| 100 | + RemoteWebDriver.__init__(self, |
| 101 | + command_executor=remote_addr, |
| 102 | + desired_capabilities=desired_capabilities) |
| 103 | + else: |
| 104 | + raise WebDriverException('blackberry-deploy failed to launch browser') |
| 105 | + except Exception as e: |
| 106 | + raise WebDriverException('Something went wrong launching blackberry-deploy', stacktrace=getattr(e, 'stacktrace', None)) |
| 107 | + |
| 108 | + def quit(self): |
| 109 | + """ |
| 110 | + Closes the browser and shuts down the |
| 111 | + """ |
| 112 | + try: |
| 113 | + RemoteWebDriver.quit(self) |
| 114 | + except http_client.BadStatusLine: |
| 115 | + pass |
0 commit comments