Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 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 | """Telemetry-based Chrome automation functions.""" |
| 6 | |
| 7 | from telemetry.core import browser_finder, browser_options |
| 8 | |
| 9 | |
| 10 | # The following constant is the name of the logged-in user that is specified |
| 11 | # by the Telemetry chromeOS login extension |
| 12 | # (chromium/src/tools/telemetry/telemetry/core/chrome/chromeos_login_ext). |
| 13 | # The value here must match what is specified in that login extension. |
| 14 | LOGIN_USER = 'test@test.test' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame^] | 15 | _BROWSER_TYPE_LOGIN = 'system' |
| 16 | _BROWSER_TYPE_GUEST = 'system-guest' |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 17 | |
| 18 | |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame^] | 19 | def _get_browser(browser_type): |
| 20 | options = browser_options.BrowserOptions() |
| 21 | options.browser_type = browser_type |
| 22 | browser_to_create = browser_finder.FindBrowser(options) |
| 23 | return browser_to_create.Create() |
| 24 | |
| 25 | |
| 26 | def logged_in_browser(): |
| 27 | """Returns a logged in browser. |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 28 | |
| 29 | Wrapping this within a Python with/as construct will take care of |
| 30 | automatically logging into Chrome at the start and logging out of Chrome |
| 31 | at the end, e.g.: |
| 32 | |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame^] | 33 | with chrome.logged_in_browser() as browser: |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 34 | do_test() # Will be logged in for this. |
| 35 | # Logged out at this point. |
| 36 | |
| 37 | @return A Telemetry Browser object supporting context management. |
| 38 | """ |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame^] | 39 | return _get_browser(_BROWSER_TYPE_LOGIN) |
| 40 | |
| 41 | |
| 42 | def incognito_browser(): |
| 43 | """Returns an incognito browser.""" |
| 44 | return _get_browser(_BROWSER_TYPE_GUEST) |