Commit ffab8966 authored by tfarina@chromium.org's avatar tfarina@chromium.org

Add option for url_fetch_test to wait for a JS expression

This option extends url_fetch_test to support waiting for a JavaScript
expression to evaluate to true. This is used in the Chrome OS
desktopui_GmailLatency test. The browser goes to the test URL and then runs
the test to completion.

BUG=chromium-os:10869
TEST=test on my workstation on Ubuntu, x86-mario, qemu using a web page which
sets a JS value after a period.

Original Review URL: http://codereview.chromium.org/6249003/Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
Reviewed-by: default avatarAnton Sargent <asargent@chromium.org>
Reviewed-by: default avatarWilliam Chan <willchan@chromium.org>
Reviewed-by: default avatarThiago Farina <tfarina@chromium.org>

Review URL: http://codereview.chromium.org/6288003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71408 0039d316-1c4b-4281-b951-d872f2087c98
parent dfc634cf
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/file_path.h" #include "base/file_path.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
...@@ -16,7 +17,9 @@ ...@@ -16,7 +17,9 @@
namespace { namespace {
// Provides a UI Test that lets us take the browser to a url, and // Provides a UI Test that lets us take the browser to a url, and
// wait for a cookie value to be set before closing the page. // wait for a cookie value to be set or a JavaScript expression to evaluate
// true before closing the page. It is undefined what happens if you specify
// both a cookie and a JS expression.
class UrlFetchTest : public UITest { class UrlFetchTest : public UITest {
public: public:
UrlFetchTest() { UrlFetchTest() {
...@@ -46,8 +49,13 @@ class UrlFetchTest : public UITest { ...@@ -46,8 +49,13 @@ class UrlFetchTest : public UITest {
UITest::SetUp(); UITest::SetUp();
} }
void RunTest(const GURL& url, const char* wait_cookie_name, void RunTest(const GURL& url,
const char* wait_cookie_value, const char* var_to_fetch, const char* wait_cookie_name,
const char* wait_cookie_value,
const char* var_to_fetch,
const std::wstring& wait_js_expr,
const std::wstring& wait_js_frame_xpath,
int wait_js_timeout_ms,
UrlFetchTestResult* result) { UrlFetchTestResult* result) {
scoped_refptr<TabProxy> tab(GetActiveTab()); scoped_refptr<TabProxy> tab(GetActiveTab());
ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url));
...@@ -65,6 +73,12 @@ class UrlFetchTest : public UITest { ...@@ -65,6 +73,12 @@ class UrlFetchTest : public UITest {
TestTimeouts::huge_test_timeout_ms()); TestTimeouts::huge_test_timeout_ms());
ASSERT_TRUE(result->cookie_value.length()); ASSERT_TRUE(result->cookie_value.length());
} }
} else if (!wait_js_expr.empty()) {
bool completed = WaitUntilJavaScriptCondition(tab.get(),
wait_js_frame_xpath,
wait_js_expr,
wait_js_timeout_ms);
ASSERT_TRUE(completed);
} }
if (var_to_fetch) { if (var_to_fetch) {
std::string script = StringPrintf( std::string script = StringPrintf(
...@@ -102,6 +116,21 @@ bool WriteValueToFile(std::string value, const FilePath& path) { ...@@ -102,6 +116,21 @@ bool WriteValueToFile(std::string value, const FilePath& path) {
// In conjunction with --wait_cookie_name, this saves the cookie value to // In conjunction with --wait_cookie_name, this saves the cookie value to
// a file at the given path. (Incompatible with --wait_cookie_value) // a file at the given path. (Incompatible with --wait_cookie_value)
// //
// --wait_js_expr=<jscript_expr>
// Waits for a javascript expression to evaluate true before exiting
// successfully.
//
// --wait_js_timeout=<timeout_ms>
// In conjunction with --wait_js_condition, this sets the timeout in ms
// that we are prepared to wait. If this timeout is exceeded, we will exit
// with failure. Note that a timeout greater than the gtest timeout will not
// be honored.
//
// --wait_js_frame_xpath=<xpath>
// In conjuction with --wait_js_condition, the JavaScript expression is
// executed in the context of the frame that matches the provided xpath.
// If this is not specified (or empty string), then the main frame is used.
//
// --jsvar=<name> // --jsvar=<name>
// At the end of the test, fetch the named javascript variable from the page. // At the end of the test, fetch the named javascript variable from the page.
// //
...@@ -121,14 +150,27 @@ TEST_F(UrlFetchTest, UrlFetch) { ...@@ -121,14 +150,27 @@ TEST_F(UrlFetchTest, UrlFetch) {
cmd_line->GetSwitchValueASCII("wait_cookie_name"); cmd_line->GetSwitchValueASCII("wait_cookie_name");
std::string cookie_value = std::string cookie_value =
cmd_line->GetSwitchValueASCII("wait_cookie_value"); cmd_line->GetSwitchValueASCII("wait_cookie_value");
std::wstring js_expr =
UTF8ToWide(cmd_line->GetSwitchValueASCII("wait_js_expr"));
std::wstring js_frame_xpath =
UTF8ToWide(cmd_line->GetSwitchValueASCII("wait_js_frame_xpath"));
std::string js_timeout_ms_str =
cmd_line->GetSwitchValueASCII("wait_js_timeout");
std::string jsvar = cmd_line->GetSwitchValueASCII("jsvar"); std::string jsvar = cmd_line->GetSwitchValueASCII("jsvar");
int js_timeout_ms = -1; // no timeout, wait forever
if (!js_timeout_ms_str.empty())
base::StringToInt(js_timeout_ms_str, &js_timeout_ms);
UrlFetchTestResult result; UrlFetchTestResult result;
RunTest(GURL(cmd_line->GetSwitchValueASCII("url")), RunTest(GURL(cmd_line->GetSwitchValueASCII("url")),
cookie_name.length() > 0 ? cookie_name.c_str() : NULL, cookie_name.length() > 0 ? cookie_name.c_str() : NULL,
cookie_value.length() > 0 ? cookie_value.c_str() : NULL, cookie_value.length() > 0 ? cookie_value.c_str() : NULL,
jsvar.length() > 0 ? jsvar.c_str() : NULL, jsvar.length() > 0 ? jsvar.c_str() : NULL,
js_expr,
js_frame_xpath,
js_timeout_ms,
&result); &result);
// Write out the cookie if requested // Write out the cookie if requested
......
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