Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webdriver / pylib / selenium / webdriver / phantomjs / webdriver.py
1 #!/usr/bin/python
2 #
3 # Copyright 2012 Software freedom conservancy
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # 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, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import base64
18 import httplib
19 from selenium.webdriver.remote.command import Command
20 from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
21 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
22 from selenium.common.exceptions import WebDriverException
23 from service import Service
24
25 class WebDriver(RemoteWebDriver):
26     """
27     Wrapper to communicate with PhantomJS through Ghostdriver.
28
29     You will need to follow all the directions here:
30     https://github.com/detro/ghostdriver
31     """
32
33     def __init__(self, executable_path="phantomjs",
34                  port=0, desired_capabilities=DesiredCapabilities.PHANTOMJS):
35         """
36         Creates a new instance of the PhantomJS / Ghostdriver.
37
38         Starts the service and then creates new instance of the driver.
39
40         :Args:
41          - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
42          - port - port you would like the service to run, if left as 0, a free port will be found.
43          - desired_capabilities: Dictionary object with non-browser specific
44            capabilities only, such as "proxy" or "loggingPref".
45         """
46         self.service = Service(executable_path, port=port)
47         self.service.start()
48
49         try:
50             RemoteWebDriver.__init__(self,
51                 command_executor=self.service.service_url,
52                 desired_capabilities=desired_capabilities)
53         except:
54             self.quit()
55             raise
56
57     def quit(self):
58         """
59         Closes the browser and shuts down the PhantomJS executable
60         that is started when starting the PhantomJS
61         """
62         try:
63             RemoteWebDriver.quit(self)
64         except:
65             # We don't care about the message because something probably has gone wrong
66             pass
67         finally:
68             self.service.stop()
69
70     def __del__(self):
71         try:
72             self.service.stop()
73         except:
74             pass