Skip to content

Commit 536b6e2

Browse files
Rory Craig-Barneslukeis
authored andcommitted
Adding Python code to launch BlackBerry browser automatically
The BlackBerry10 browser doesn't launch automatically with WebDriver, which makes automated testing problematic. This patch enables the automatic launching of the BlackBerry10 browser, if the user has the proper env. setup. This patch assumes the IP/port of the device. The user must provide location of required tools and device password. Code review updates: * making BB tools easier * Fixed up python styling issues (pep8). Added a better description of the args. * Realized that blackberry tools when installed with the WebWorks SDK are in the path so changed it so those are handled by default. Also made the 'typical' ip address used to be the default, now the user only needs to supply a device password if everything is setup in the standard way. * Added some directions/hints on getting the webworks SDK, location should never change. * Removing Wait(3) + improving errors * I found a better way to wait for the BB10 browser to launch, so it now polls until the browser launches and is ready. * Improved error messaging to give more details for end user. * Switching to WebDriverWait last patch had the potential to get stuck in an infinite loop. Updated code to use WebDriverWait to watch for the browser to load. Also lead to much cleaner code. * Using LOAD_TIMEOUT for easier customization * Had a CONTS created but never used it. Using it now, reduced to 5s as that still consistently works. * Fixing Stackswallowing + nits * Stack is now properly passed along with an exception. Added message when loading browser Signed-off-by: Luke Inman-Semerau <[email protected]>
1 parent 806cdc3 commit 536b6e2

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

py/build.desc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ py_test(
2424
deps = [ ":test_phantomjs" ],
2525
browsers = [ "phantomjs" ])
2626

27+
py_test(
28+
name = "blackberry_test",
29+
deps = [ ":test_blackberry" ],
30+
browsers = [ "blackberry" ])
2731

2832
py_test(
2933
name = "chrome_test",
@@ -65,6 +69,7 @@ py_test(
6569
"chrome",
6670
"ff",
6771
"ie",
72+
"blackberry",
6873
"phantomjs",
6974
"remote_firefox",
7075
"safari",

py/selenium/webdriver/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .ie.webdriver import WebDriver as Ie
2525
from .opera.webdriver import WebDriver as Opera
2626
from .safari.webdriver import WebDriver as Safari
27+
from .blackberry.webdriver import WebDriver as BlackBerry
2728
from .phantomjs.webdriver import WebDriver as PhantomJS
2829
from .android.webdriver import WebDriver as Android
2930
from .remote.webdriver import WebDriver as Remote
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

rake-tasks/browsers.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
:browser_name => "chrome",
5050
:available => chrome?
5151
},
52+
"blackberry" => {
53+
:python => {
54+
:ignore => "blackberry",
55+
:dir => "blackberry",
56+
:file_string => "blackberry",
57+
:class => "BlackBerry",
58+
:constructor_args => "device_password='password'"
59+
},
60+
:browser_name => "blackberry"
61+
},
5262
"phantomjs" => {
5363
:python => {
5464
:ignore => "phantomjs",

0 commit comments

Comments
 (0)