forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DavidBurns OperaDriver for Python. This has been built around the sam…
…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
1 parent
c6532b2
commit 09323b4
Showing
7 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters