Allow defining custom navigation on a per-page basis.

Create explicit 'navigate' and 'javascript' actions

BUG=263103

Review URL: https://chromiumcodereview.appspot.com/21174002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217762 0039d316-1c4b-4281-b951-d872f2087c98
parent 93eb532a
...@@ -30,6 +30,7 @@ class TabSwitching(page_measurement.PageMeasurement): ...@@ -30,6 +30,7 @@ class TabSwitching(page_measurement.PageMeasurement):
page_state = page_runner.PageState() page_state = page_runner.PageState()
page_state.PreparePage(page.page_set.pages[i], t) page_state.PreparePage(page.page_set.pages[i], t)
page_state.ImplicitPageNavigation(page.page_set.pages[i], t)
def MeasurePage(self, _, tab, results): def MeasurePage(self, _, tab, results):
"""Although this is called MeasurePage, we're actually using this function """Although this is called MeasurePage, we're actually using this function
......
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from telemetry.page.actions import page_action
class JavascriptAction(page_action.PageAction):
def __init__(self, attributes=None):
super(JavascriptAction, self).__init__(attributes)
def RunAction(self, page, tab, previous_action):
assert hasattr(self, 'expression')
tab.ExecuteJavaScript(self.expression)
\ No newline at end of file
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from telemetry.page.actions import page_action
class NavigateAction(page_action.PageAction):
def __init__(self, attributes=None):
super(NavigateAction, self).__init__(attributes)
def RunAction(self, page, tab, previous_action):
if page.is_file:
filename = page.serving_dirs_and_file[1]
target_side_url = tab.browser.http_server.UrlOf(filename)
else:
target_side_url = page.url
tab.Navigate(target_side_url, page.script_to_evaluate_on_commit)
tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
\ No newline at end of file
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
from telemetry.page import page as page_module
from telemetry.page.actions import navigate
from telemetry.unittest import tab_test_case
class NavigateActionTest(tab_test_case.TabTestCase):
def CreatePageFromUnittestDataDir(self, filename):
unittest_data_dir = os.path.join(os.path.dirname(__file__),
os.pardir, os.pardir, os.pardir,
'unittest_data')
self._browser.SetHTTPServerDirectories(unittest_data_dir)
return page_module.Page(
self._browser.http_server.UrlOf(filename),
None # In this test, we don't need a page set.
)
def testNavigateAction(self):
unittest_data_dir = os.path.join(os.path.dirname(__file__),
os.pardir, os.pardir, os.pardir,
'unittest_data')
self._browser.SetHTTPServerDirectories(unittest_data_dir)
page = self.CreatePageFromUnittestDataDir('blank.html')
i = navigate.NavigateAction()
i.RunAction(page, self._tab, None)
self.assertEquals(
self._tab.EvaluateJavaScript('document.location.pathname;'),
'/blank.html')
...@@ -20,6 +20,7 @@ from telemetry.page import page_filter as page_filter_module ...@@ -20,6 +20,7 @@ from telemetry.page import page_filter as page_filter_module
from telemetry.page import page_measurement_results from telemetry.page import page_measurement_results
from telemetry.page import page_runner_repeat from telemetry.page import page_runner_repeat
from telemetry.page import page_test from telemetry.page import page_test
from telemetry.page.actions import navigate
class _RunState(object): class _RunState(object):
...@@ -129,20 +130,14 @@ class PageState(object): ...@@ -129,20 +130,14 @@ class PageState(object):
This function will be called once per page before any actions are executed. This function will be called once per page before any actions are executed.
""" """
if page.is_file:
filename = page.serving_dirs_and_file[1]
target_side_url = tab.browser.http_server.UrlOf(filename)
else:
target_side_url = page.url
if test: if test:
test.WillNavigateToPage(page, tab) test.WillNavigateToPage(page, tab)
tab.Navigate(target_side_url, page.script_to_evaluate_on_commit) test.RunNavigateSteps(page, tab)
if test:
test.DidNavigateToPage(page, tab) test.DidNavigateToPage(page, tab)
else:
page.WaitToLoad(tab, 60) i = navigate.NavigateAction()
tab.WaitForDocumentReadyStateToBeInteractiveOrBetter() i.RunAction(page, tab, None)
page.WaitToLoad(tab, 60)
def CleanUpPage(self, page, tab): def CleanUpPage(self, page, tab):
if page.credentials and self._did_login: if page.credentials and self._did_login:
......
...@@ -8,6 +8,7 @@ from telemetry.page import gtest_test_results ...@@ -8,6 +8,7 @@ from telemetry.page import gtest_test_results
from telemetry.page import test_expectations from telemetry.page import test_expectations
from telemetry.page import page_test_results from telemetry.page import page_test_results
from telemetry.page.actions import all_page_actions from telemetry.page.actions import all_page_actions
from telemetry.page.actions import navigate
from telemetry.page.actions import page_action from telemetry.page.actions import page_action
def _GetActionFromData(action_data): def _GetActionFromData(action_data):
...@@ -193,7 +194,7 @@ class PageTest(object): ...@@ -193,7 +194,7 @@ class PageTest(object):
finally: finally:
self.options = None self.options = None
def _RunCompoundAction(self, page, tab, actions): def _RunCompoundAction(self, page, tab, actions, run_setup_methods=True):
for i, action in enumerate(actions): for i, action in enumerate(actions):
prev_action = actions[i - 1] if i > 0 else None prev_action = actions[i - 1] if i > 0 else None
next_action = actions[i + 1] if i < len(actions) - 1 else None next_action = actions[i + 1] if i < len(actions) - 1 else None
...@@ -205,16 +206,38 @@ class PageTest(object): ...@@ -205,16 +206,38 @@ class PageTest(object):
if not (next_action and next_action.RunsPreviousAction()): if not (next_action and next_action.RunsPreviousAction()):
action.WillRunAction(page, tab) action.WillRunAction(page, tab)
self.WillRunAction(page, tab, action) if run_setup_methods:
self.WillRunAction(page, tab, action)
try: try:
action.RunAction(page, tab, prev_action) action.RunAction(page, tab, prev_action)
finally: finally:
self.DidRunAction(page, tab, action) if run_setup_methods:
self.DidRunAction(page, tab, action)
# Closing the connections periodically is needed; otherwise we won't be # Closing the connections periodically is needed; otherwise we won't be
# able to open enough sockets, and the pages will time out. # able to open enough sockets, and the pages will time out.
util.CloseConnections(tab) util.CloseConnections(tab)
def RunNavigateSteps(self, page, tab):
"""Navigates the tab to the page URL attribute.
If 'navigate_steps' is defined for the page, this will attempt to
run it as a compound action.
"""
if hasattr(page, 'navigate_steps'):
navigate_actions = GetCompoundActionFromPage(page, 'navigate_steps')
if not any(isinstance(action, navigate.NavigateAction)
for action in navigate_actions):
raise page_action.PageActionFailed(
'No NavigateAction in navigate_steps')
self._RunCompoundAction(page, tab, navigate_actions, False)
else:
# TODO(edmundyan): Make a default navigate_steps action on the page object
# once we can deprecate page.WaitToLoad()
i = navigate.NavigateAction()
i.RunAction(page, tab, None)
page.WaitToLoad(tab, 60)
@property @property
def action_name_to_run(self): def action_name_to_run(self):
return self._action_name_to_run return self._action_name_to_run
...@@ -47,8 +47,7 @@ class RecordPage(page_test.PageTest): ...@@ -47,8 +47,7 @@ class RecordPage(page_test.PageTest):
should_reload = False should_reload = False
for compound_action in self._CompoundActionsForPage(page): for compound_action in self._CompoundActionsForPage(page):
if should_reload: if should_reload:
tab.Navigate(page.url) self.RunNavigateSteps(page, tab)
tab.WaitForDocumentReadyStateToBeComplete()
self._RunCompoundAction(page, tab, compound_action) self._RunCompoundAction(page, tab, compound_action)
should_reload = True should_reload = True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment