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

Unified Diff: tools/chrome_proxy/webdriver/common.py

Issue 2554383002: Add timeouts to LoadPage and ExecJavascipt. (Closed)
Patch Set: Fix nit. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/chrome_proxy/webdriver/simple_smoke.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/chrome_proxy/webdriver/common.py
diff --git a/tools/chrome_proxy/webdriver/common.py b/tools/chrome_proxy/webdriver/common.py
index f6798c9d3a70d99e5cd0c65870b8ac3e76bc9635..ebe041b3f481526927a355add8c8ef7ba7fb35ce 100644
--- a/tools/chrome_proxy/webdriver/common.py
+++ b/tools/chrome_proxy/webdriver/common.py
@@ -6,6 +6,7 @@ import argparse
import json
import os
import re
+import socket
import shlex
import sys
import time
@@ -204,28 +205,53 @@ class TestDriver:
"""
self._url = url
- # TODO(robertogden): Add timeout.
- def LoadPage(self):
+ def LoadPage(self, timeout=30):
"""Starts Chromium with any arguments previously given and navigates to the
given URL.
+
+ Args:
+ timeout: Page load timeout in seconds.
"""
if not self._driver:
self._StartDriver()
+ self._driver.set_page_load_timeout(timeout)
self._driver.get(self._url)
- # TODO(robertogden): Add timeout.
- def ExecuteJavascript(self, script):
- """Executes the given javascript in the browser's current page as if it were
- on the console.
+ def ExecuteJavascript(self, script, timeout=30):
+ """Executes the given javascript in the browser's current page in an
+ anonymous function.
+
+ If you expect a result and don't get one, try adding a return statement or
+ using ExecuteJavascriptStatement() below.
Args:
script: A string of Javascript code.
+ timeout: Timeout for the Javascript code to return in seconds.
Returns:
A string of the verbatim output from the Javascript execution.
"""
if not self._driver:
self._StartDriver()
- return self._driver.execute_script("return " + script)
+ # TODO(robertogden): Use 'driver.set_script_timeout(timeout)' instead after
+ # crbug/672114 is fixed.
+ default_timeout = socket.getdefaulttimeout()
+ socket.setdefaulttimeout(timeout)
+ script_result = self._driver.execute_script(script)
+ socket.setdefaulttimeout(default_timeout)
+ return script_result
+
+ def ExecuteJavascriptStatement(self, script, timeout=30):
+ """Wraps ExecuteJavascript() for use with a single statement.
+
+ Behavior is analogous to 'function(){ return <script> }();'
+
+ Args:
+ script: A string of Javascript code.
+ timeout: Timeout for the Javascript code to return in seconds.
+ Returns:
+ A string of the verbatim output from the Javascript execution.
+ """
+ return self.ExecuteJavascript("return " + script, timeout)
def GetPerformanceLogs(self, method_filter=r'Network\.responseReceived'):
"""Returns all logged Performance events from Chrome.
« 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