1# Copyright 2015 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""An interface to access the local browser facade.""" 6 7import logging 8 9class BrowserFacadeNativeError(Exception): 10 """Error in BrowserFacadeNative.""" 11 pass 12 13 14class BrowserFacadeNative(object): 15 """Facade to access the browser-related functionality.""" 16 17 def __init__(self, resource): 18 """Initializes the USB facade. 19 20 @param resource: A FacadeResource object. 21 22 """ 23 self._resource = resource 24 25 26 def start_custom_chrome(self, kwargs): 27 """Start a custom Chrome with given arguments. 28 29 @param kwargs: A dict of keyword arguments passed to Chrome. 30 @return: True on success, False otherwise. 31 32 """ 33 return self._resource.start_custom_chrome(kwargs) 34 35 36 def start_default_chrome(self, restart=False, extra_browser_args=None): 37 """Start the default Chrome. 38 39 @param restart: True to start Chrome without clearing previous state. 40 @param extra_browser_args: A list containing extra browser args passed 41 to Chrome in addition to default ones. 42 @return: True on success, False otherwise. 43 44 """ 45 return self._resource.start_default_chrome(restart, extra_browser_args) 46 47 48 def set_http_server_directories(self, directories): 49 """Starts an HTTP server. 50 51 @param directories: Directories to start serving. 52 53 @return True on success. False otherwise. 54 55 """ 56 return self._resource.set_http_server_directories(directories) 57 58 59 def http_server_url_of(self, fullpath): 60 """Converts a path to a URL. 61 62 @param fullpath: String containing the full path to the content. 63 64 @return the URL for the provided path. 65 66 """ 67 return self._resource.http_server_url_of(fullpath) 68 69 70 def new_tab(self, url): 71 """Opens a new tab and loads URL. 72 73 @param url: The URL to load. 74 @return a str, the tab descriptor of the opened tab. 75 76 """ 77 logging.debug('Load URL %s', url) 78 return self._resource.load_url(url) 79 80 81 def close_tab(self, tab_descriptor): 82 """Closes a previously opened tab. 83 84 @param tab_descriptor: Indicate which tab to be closed. 85 86 """ 87 tab = self._resource.get_tab_by_descriptor(tab_descriptor) 88 logging.debug('Closing URL %s', tab.url) 89 self._resource.close_tab(tab_descriptor) 90 91 92 def wait_for_javascript_expression( 93 self, tab_descriptor, expression, timeout): 94 """Waits for the given JavaScript expression to be True on the 95 given tab. 96 97 @param tab_descriptor: Indicate on which tab to wait for the expression. 98 @param expression: Indiate for what expression to wait. 99 @param timeout: Indicate the timeout of the expression. 100 """ 101 self._resource.wait_for_javascript_expression( 102 tab_descriptor, expression, timeout) 103 104 105 def execute_javascript(self, tab_descriptor, statement, timeout): 106 """Executes a JavaScript statement on the given tab. 107 108 @param tab_descriptor: Indicate on which tab to execute the statement. 109 @param statement: Indiate what statement to execute. 110 @param timeout: Indicate the timeout of the statement. 111 """ 112 self._resource.execute_javascript( 113 tab_descriptor, statement, timeout) 114 115 116 def evaluate_javascript(self, tab_descriptor, expression, timeout): 117 """Evaluates a JavaScript expression on the given tab. 118 119 @param tab_descriptor: Indicate on which tab to evaluate the expression. 120 @param expression: Indiate what expression to evaluate. 121 @param timeout: Indicate the timeout of the expression. 122 @return the JSONized result of the given expression 123 """ 124 return self._resource.evaluate_javascript( 125 tab_descriptor, expression, timeout) 126