Skip to content

Commit

Permalink
DavidBurns OperaDriver for Python. This has been built around the sam…
Browse files Browse the repository at this point in the history
…e way that ChromeDriver is except that it needs an environment variable for where to find the Selenium Server Jar, like Ruby

r15494
  • Loading branch information
AutomatedTester committed Jan 13, 2012
1 parent c6532b2 commit 09323b4
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ recursive-include py/selenium/webdriver *.py
recursive-include py/selenium/webdriver/common *.py
recursive-include py/selenium/common *.py
recursive-include py/selenium/webdriver/chrome *.py
recursive-include py/selenium/webdriver/opera *.py
recursive-include py/selenium/webdriver/firefox *.py *.xpi
recursive-include py/selenium/webdriver/firefox/x86 *.so
recursive-include py/selenium/webdriver/firefox/amd64 *.so
Expand Down
9 changes: 8 additions & 1 deletion py/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ py_test(
deps = [ ":test_chrome" ],
browsers = [ "chrome" ])

py_test(
name = "opera_test",
opera_specific_tests = [ "test/selenium/webdriver/opera/*_tests.py" ],
deps = [ ":test_opera" ],
browsers = [ "opera" ])

py_test(
name = "ie_test",
deps = [ ":test_ie" ],
Expand Down Expand Up @@ -58,5 +64,6 @@ py_test(
"chrome",
"ff",
"ie",
"remote_firefox"
"remote_firefox",
"opera"
])
14 changes: 14 additions & 0 deletions py/selenium/webdriver/opera/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2010 WebDriver committers
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
80 changes: 80 additions & 0 deletions py/selenium/webdriver/opera/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/python
#
# Copyright 2011 Webdriver_name committers
# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess
from subprocess import PIPE
import time
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common import utils

class Service(object):
"""
Object that manages the starting and stopping of the OperaDriver
"""

def __init__(self, executable_path, port=0):
"""
Creates a new instance of the Service
:Args:
- executable_path : Path to the OperaDriver
- port : Port the service is running on """

self.port = port
self.path = executable_path
if self.port == 0:
self.port = utils.free_port()

def start(self):
"""
Starts the OperaDriver Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(["java", "-jar", self.path, "-port", "%s" % self.port])
except:
raise WebDriverException(
"OperaDriver executable needs to be available in the path. \
")
time.sleep(10)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to the OperaDriver")

@property
def service_url(self):
"""
Gets the url of the OperaDriver Service
"""
return "http://localhost:%d/wd/hub" % self.port

def stop(self):
"""
Tells the OperaDriver to stop and cleans up the process
"""
#If its dead dont worry
if self.process is None:
return

self.process.kill()
self.process.wait()

79 changes: 79 additions & 0 deletions py/selenium/webdriver/opera/webdriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/python
#
# Copyright 2011 Webdriver_name committers
# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import httplib
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.command import Command
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from service import Service

class WebDriver(RemoteWebDriver):
"""
Controls the OperaDriver and allows you to drive the browser.
"""

def __init__(self, executable_path=os.environ["SELENIUM_SERVER_JAR"], port=0,
desired_capabilities=DesiredCapabilities.OPERA):
"""
Creates a new instance of the Opera driver.
Starts the service and then creates new instance of Opera Driver.
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the
Environment Variable SELENIUM_SERVER_JAR
- port - port you would like the service to run, if left as 0, a free port will be found.
- desired_capabilities: Dictionary object with desired capabilities (Can be used to provide various Opera switches).
"""
self.service = Service(executable_path, port=port)
self.service.start()

RemoteWebDriver.__init__(self,
command_executor=self.service.service_url,
desired_capabilities=desired_capabilities)

def quit(self):
"""
Closes the browser and shuts down the OperaDriver executable
that is started when starting the OperaDriver
"""
try:
RemoteWebDriver.quit(self)
except httplib.BadStatusLine:
pass
finally:
self.service.stop()

def save_screenshot(self, filename):
"""
Gets the screenshot of the current window. Returns False if there is
any IOError, else returns True. Use full paths in your filename.
"""
png = RemoteWebDriver.execute(self, Command.SCREENSHOT)['value']
try:
f = open(filename, 'wb')
f.write(base64.decodestring(png))
f.close()
except IOError:
return False
finally:
del png
return True

6 changes: 6 additions & 0 deletions rake-tasks/browsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
:available => chrome?
},
"opera" => {
:python => {
:ignore => "opera",
:dir => "opera",
:file_string => "opera",
:class => "Opera"
},
:java => {
:class => "com.opera.core.systems.OperaDriver",
:deps => [ "//third_party/java/opera-driver" ]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def setup_python3():
'selenium.webdriver.support',
'selenium.webdriver.firefox',
'selenium.webdriver.ie',
'selenium.webdriver.opera',
'selenium.webdriver.remote',
'selenium.webdriver.support', ],
package_data={
Expand Down

0 comments on commit 09323b4

Please sign in to comment.