Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: tools/chrome_proxy/webdriver/common.py

Issue 2554383002: Add timeouts to LoadPage and ExecJavascipt. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/chrome_proxy/webdriver/simple_smoke.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2016 The Chromium Authors. All rights reserved. 1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import argparse 5 import argparse
6 import json 6 import json
7 import os 7 import os
8 import re 8 import re
9 import socket
9 import shlex 10 import shlex
10 import sys 11 import sys
11 import time 12 import time
12 import traceback 13 import traceback
13 14
14 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
15 os.pardir, 'third_party', 'webdriver', 'pylib')) 16 os.pardir, 'third_party', 'webdriver', 'pylib'))
16 from selenium import webdriver 17 from selenium import webdriver
17 from selenium.webdriver.chrome.options import Options 18 from selenium.webdriver.chrome.options import Options
18 19
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 'clearHostResolverCache();}') 198 'clearHostResolverCache();}')
198 199
199 def SetURL(self, url): 200 def SetURL(self, url):
200 """Sets the URL that the browser will navigate to during the test. 201 """Sets the URL that the browser will navigate to during the test.
201 202
202 Args: 203 Args:
203 url: The string URL to navigate to 204 url: The string URL to navigate to
204 """ 205 """
205 self._url = url 206 self._url = url
206 207
207 # TODO(robertogden): Add timeout. 208 def LoadPage(self, timeout=30):
208 def LoadPage(self):
209 """Starts Chromium with any arguments previously given and navigates to the 209 """Starts Chromium with any arguments previously given and navigates to the
210 given URL. 210 given URL.
211
212 Args:
213 timeout: Page load timeout in seconds.
211 """ 214 """
212 if not self._driver: 215 if not self._driver:
213 self._StartDriver() 216 self._StartDriver()
217 self._driver.set_page_load_timeout(timeout)
214 self._driver.get(self._url) 218 self._driver.get(self._url)
215 219
216 # TODO(robertogden): Add timeout. 220 def ExecuteJavascript(self, script, timeout=30):
217 def ExecuteJavascript(self, script): 221 """Executes the given javascript in the browser's current page in an
218 """Executes the given javascript in the browser's current page as if it were 222 anonymous function.
219 on the console. 223
224 If you expect a result and don't get one, try adding a return statement or
225 using ExecuteJavascriptStatement() below.
220 226
221 Args: 227 Args:
222 script: A string of Javascript code. 228 script: A string of Javascript code.
229 timeout: Timeout for the Javascript code to return in seconds.
223 Returns: 230 Returns:
224 A string of the verbatim output from the Javascript execution. 231 A string of the verbatim output from the Javascript execution.
225 """ 232 """
226 if not self._driver: 233 if not self._driver:
227 self._StartDriver() 234 self._StartDriver()
228 return self._driver.execute_script("return " + script) 235 # TODO(crbug/672114): Use 'driver.set_script_timeout(timeout)' instead.
sclittle 2016/12/07 18:08:56 nit: put a username in the TODO(), and mention the
Robert Ogden 2016/12/07 18:48:22 Done.
236 default_timeout = socket.getdefaulttimeout()
237 socket.setdefaulttimeout(timeout)
238 script_result = self._driver.execute_script(script)
239 socket.setdefaulttimeout(default_timeout)
240 return script_result
241
242 def ExecuteJavascriptStatement(self, script, timeout=30):
243 """Wraps ExecuteJavascript() for use with a single statement.
244
245 Behavior is analogous to 'function(){ return <script> }();'
246
247 Args:
248 script: A string of Javascript code.
249 timeout: Timeout for the Javascript code to return in seconds.
250 Returns:
251 A string of the verbatim output from the Javascript execution.
252 """
253 return self.ExecuteJavascript("return " + script, timeout)
229 254
230 def GetPerformanceLogs(self, method_filter=r'Network\.responseReceived'): 255 def GetPerformanceLogs(self, method_filter=r'Network\.responseReceived'):
231 """Returns all logged Performance events from Chrome. 256 """Returns all logged Performance events from Chrome.
232 257
233 Args: 258 Args:
234 method_filter: A regex expression to match the method of logged events 259 method_filter: A regex expression to match the method of logged events
235 against. Only logs who's method matches the regex will be returned. 260 against. Only logs who's method matches the regex will be returned.
236 Returns: 261 Returns:
237 Performance logs as a list of dicts, since the last time this function was 262 Performance logs as a list of dicts, since the last time this function was
238 called. 263 called.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 """ 393 """
369 sys.stderr.write("**************************************\n") 394 sys.stderr.write("**************************************\n")
370 sys.stderr.write("**************************************\n") 395 sys.stderr.write("**************************************\n")
371 sys.stderr.write("** **\n") 396 sys.stderr.write("** **\n")
372 sys.stderr.write("** TEST FAILURE **\n") 397 sys.stderr.write("** TEST FAILURE **\n")
373 sys.stderr.write("** **\n") 398 sys.stderr.write("** **\n")
374 sys.stderr.write("**************************************\n") 399 sys.stderr.write("**************************************\n")
375 sys.stderr.write("**************************************\n") 400 sys.stderr.write("**************************************\n")
376 sys.stderr.write(msg, '\n') 401 sys.stderr.write(msg, '\n')
377 sys.exit(1) 402 sys.exit(1)
OLDNEW
« no previous file with comments | « no previous file | tools/chrome_proxy/webdriver/simple_smoke.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698