Commit 45671618 authored by jcampan@chromium.org's avatar jcampan@chromium.org

Migrating the SSL UI tests to be browser tests.

Reenabling all of them.
Hopefully it'll help with flakiness.

Note that I am keeping the SSL UI tests alive until the browser test framework has been ported to Mac and Linux.

BUG=6584,2136
TEST=Run the browser tests.
Review URL: http://codereview.chromium.org/101013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14901 0039d316-1c4b-4281-b951-d872f2087c98
parent 30369d92
...@@ -264,11 +264,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { ...@@ -264,11 +264,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
// Now let's press tab to move the focus. // Now let's press tab to move the focus.
for (int j = 0; j < 7; ++j) { for (int j = 0; j < 7; ++j) {
// Let's make sure the focus is on the expected element in the page. // Let's make sure the focus is on the expected element in the page.
ui_test_utils::JavaScriptRunner js_runner( std::string actual;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
browser()->GetSelectedTabContents()->AsWebContents(), browser()->GetSelectedTabContents()->AsWebContents(),
L"", L"",
L"window.domAutomationController.send(getFocusedElement());"); L"window.domAutomationController.send(getFocusedElement());",
std::string actual = js_runner.Run(); &actual));
ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); ASSERT_STREQ(kExpElementIDs[j], actual.c_str());
ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false, ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
...@@ -300,11 +301,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) { ...@@ -300,11 +301,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
::Sleep(kActionDelayMs); ::Sleep(kActionDelayMs);
// Let's make sure the focus is on the expected element in the page. // Let's make sure the focus is on the expected element in the page.
ui_test_utils::JavaScriptRunner js_runner( std::string actual;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
browser()->GetSelectedTabContents()->AsWebContents(), browser()->GetSelectedTabContents()->AsWebContents(),
L"", L"",
L"window.domAutomationController.send(getFocusedElement());"); L"window.domAutomationController.send(getFocusedElement());",
std::string actual = js_runner.Run(); &actual));
ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
} }
......
This diff is collapsed.
...@@ -100,7 +100,7 @@ class NavigationEntry { ...@@ -100,7 +100,7 @@ class NavigationEntry {
// Raw accessors for all the content status flags. This contains a // Raw accessors for all the content status flags. This contains a
// combination of any of the ContentStatusFlags defined above. It is used // combination of any of the ContentStatusFlags defined above. It is used
// by the UI tests for checking and for certain copying. Use the per-status // by some tests for checking and for certain copying. Use the per-status
// functions for normal usage. // functions for normal usage.
void set_content_status(int content_status) { void set_content_status(int content_status) {
content_status_ = content_status; content_status_ = content_status;
...@@ -120,7 +120,7 @@ class NavigationEntry { ...@@ -120,7 +120,7 @@ class NavigationEntry {
// Copy and assignment is explicitly allowed for this class. // Copy and assignment is explicitly allowed for this class.
}; };
// The type of the page an entry corresponds to. Used by ui tests. // The type of the page an entry corresponds to. Used by tests.
enum PageType { enum PageType {
NORMAL_PAGE = 0, NORMAL_PAGE = 0,
ERROR_PAGE, ERROR_PAGE,
......
...@@ -358,6 +358,9 @@ class TabContents : public PageNavigator, ...@@ -358,6 +358,9 @@ class TabContents : public PageNavigator,
// Called when the blocked popup notification is shown or hidden. // Called when the blocked popup notification is shown or hidden.
virtual void PopupNotificationVisibilityChanged(bool visible); virtual void PopupNotificationVisibilityChanged(bool visible);
// Returns the number of constrained windows in this tab. Used by tests.
size_t constrained_window_count() { return child_windows_.size(); }
// Views and focus ----------------------------------------------------------- // Views and focus -----------------------------------------------------------
// TODO(brettw): Most of these should be removed and the caller should call // TODO(brettw): Most of these should be removed and the caller should call
// the view directly. // the view directly.
......
...@@ -149,11 +149,13 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) { ...@@ -149,11 +149,13 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) {
} }
std::string FocusedOnPage(WebContents* web_contents) { std::string FocusedOnPage(WebContents* web_contents) {
ui_test_utils::JavaScriptRunner js_runner( std::string result;
ui_test_utils::ExecuteJavaScriptAndExtractString(
web_contents, web_contents,
L"", L"",
L"window.domAutomationController.send(getFocusedElement());"); L"window.domAutomationController.send(getFocusedElement());",
return js_runner.Run(); &result);
return result;
} }
// This tests the FindInPage end-state, in other words: what is focused when you // This tests the FindInPage end-state, in other words: what is focused when you
...@@ -186,11 +188,12 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { ...@@ -186,11 +188,12 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) {
EXPECT_EQ(1, FindInPage(L"Google", FWD, IGNORE_CASE, false)); EXPECT_EQ(1, FindInPage(L"Google", FWD, IGNORE_CASE, false));
// Move the selection to link 1, after searching. // Move the selection to link 1, after searching.
ui_test_utils::JavaScriptRunner js_runner( std::string result;
ui_test_utils::ExecuteJavaScriptAndExtractString(
web_contents, web_contents,
L"", L"",
L"window.domAutomationController.send(selectLink1());"); L"window.domAutomationController.send(selectLink1());",
js_runner.Run(); &result);
// End the find session. // End the find session.
web_contents->StopFinding(false); web_contents->StopFinding(false);
......
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><style>body,td,a,p,.h{font-family:arial,sans-serif}.h{font-size:20px}.h{color:#3366cc}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}#gbar{float:left;font-weight:bold;height:22px;padding-left:2px}.gbh,.gb2 div{border-top:1px solid #c9d7f1;font-size:0;height:0}.gbh{position:absolute;top:24px;width:100%}.gb2 div{margin:5px}#gbi{background:#fff;border:1px solid;border-color:#c9d7f1 #36c #36c #a2bae7;font-size:13px;top:24px;z-index:1000}#guser{padding-bottom:7px !important}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.73em;vertical-align:top}.gb2 a{display:block;padding:.2em .5em}}#gbi,.gb2{display:none;position:absolute;width:8em}.gb2{z-index:1001}#gbar a,#gbar a:active,#gbar a:visited{color:#00c;font-weight:normal}.gb2 a,.gb3 a{text-decoration:none}#gbar .gb2 a:hover{background:#36c;color:#fff;display:block}</style><script>window.google={kEI:"BuG9R7b_PI6SoASYhdTtDQ",kEXPI:"17259,17735,17870",kHL:"en"}; <html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><style>body,td,a,p,.h{font-family:arial,sans-serif}.h{font-size:20px}.h{color:#3366cc}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}#gbar{float:left;font-weight:bold;height:22px;padding-left:2px}.gbh,.gb2 div{border-top:1px solid #c9d7f1;font-size:0;height:0}.gbh{position:absolute;top:24px;width:100%}.gb2 div{margin:5px}#gbi{background:#fff;border:1px solid;border-color:#c9d7f1 #36c #36c #a2bae7;font-size:13px;top:24px;z-index:1000}#guser{padding-bottom:7px !important}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.73em;vertical-align:top}.gb2 a{display:block;padding:.2em .5em}}#gbi,.gb2{display:none;position:absolute;width:8em}.gb2{z-index:1001}#gbar a,#gbar a:active,#gbar a:visited{color:#00c;font-weight:normal}.gb2 a,.gb3 a{text-decoration:none}#gbar .gb2 a:hover{background:#36c;color:#fff;display:block}</style><script>window.google={kEI:"BuG9R7b_PI6SoASYhdTtDQ",kEXPI:"17259,17735,17870",kHL:"en"};
function sf(){document.f.q.focus()} function sf(){document.f.q.focus()}
window.rwt=function(b,d,e,g,h,f,i){var a=encodeURIComponent||escape,c=b.href.split("#");b.href="/url?sa=t"+(d?"&oi="+a(d):"")+(e?"&cad="+a(e):"")+"&ct="+a(g)+"&cd="+a(h)+"&url="+a(c[0]).replace(/\+/g,"%2B")+"&ei=BuG9R7b_PI6SoASYhdTtDQ"+(f?"&usg="+f:"")+i+(c[1]?"#"+c[1]:"");b.onmousedown="";return true}; window.rwt=function(b,d,e,g,h,f,i){var a=encodeURIComponent||escape,c=b.href.split("#");b.href="/url?sa=t"+(d?"&oi="+a(d):"")+(e?"&cad="+a(e):"")+"&ct="+a(g)+"&cd="+a(h)+"&url="+a(c[0]).replace(/\+/g,"%2B")+"&ei=BuG9R7b_PI6SoASYhdTtDQ"+(f?"&usg="+f:"")+i+(c[1]?"#"+c[1]:"");b.onmousedown="";return true};
window.gbar={};(function(){var a=window.gbar,b,g,h;function l(c,f,e){c.display=h?"none":"block";c.left=f+"px";c.top=e+"px"}a.tg=function(c){var f=0,e=0,d,m=0,n,j=window.navExtra,k,i=document;g=g||i.getElementById("gbar").getElementsByTagName("span");(c||window.event).cancelBubble=!m;if(!b){b=i.createElement(Array.every||window.createPopup?"iframe":"DIV");b.frameBorder="0";b.scrolling="no";b.src="#";g[7].parentNode.appendChild(b).id="gbi";if(j&&g[7])for(n in j){k=i.createElement("span");k.appendChild(j[n]);g[7].parentNode.insertBefore(k,g[7]).className="gb2"}i.onclick=a.close}while(d=g[++m]){if(e){l(d.style,e+1,f+25);f+=d.firstChild.tagName=="DIV"?9:20}if(d.className=="gb3"){do e+=d.offsetLeft;while(d=d.offsetParent)}}b.style.height=f+"px";l(b.style,e,24);h=!h};a.close=function(c){h&&a.tg(c)}})();</script></head><body onload="sf();if(document.images){new Image().src='/images/nav_logo3.png'}" topmargin="3" alink="#ff0000" bgcolor="#ffffff" link="#0000cc" marginheight="3" text="#000000" vlink="#551a8b"><div id="gbar"><nobr><span class="gb1">Web</span> <span class="gb1"><a href="http://images.google.com/imghp?hl=en&amp;tab=wi">Images</a></span> <span class="gb1"><a href="http://maps.google.com/maps?hl=en&amp;tab=wl">Maps</a></span> <span class="gb1"><a href="http://news.google.com/nwshp?hl=en&amp;tab=wn">News</a></span> <span class="gb1"><a href="http://www.google.com/prdhp?hl=en&amp;tab=wf">Shopping</a></span> <span class="gb1"><a href="http://mail.google.com/mail/?hl=en&amp;tab=wm">Gmail</a></span> <span class="gb3"><a href="http://www.google.com/intl/en/options/" onclick="this.blur();gbar.tg(event);return !1"><u>more</u> <small></small></a></span> <span class="gb2"><a href="http://video.google.com/?hl=en&amp;tab=wv">Video</a></span> <span class="gb2"><a href="http://groups.google.com/grphp?hl=en&amp;tab=wg">Groups</a></span> <span class="gb2"><a href="http://books.google.com/bkshp?hl=en&amp;tab=wp">Books</a></span> <span class="gb2"><a href="http://scholar.google.com/schhp?hl=en&amp;tab=ws">Scholar</a></span> <span class="gb2"><a href="http://finance.google.com/finance?hl=en&amp;tab=we">Finance</a></span> <span class="gb2"><a href="http://blogsearch.google.com/?hl=en&amp;tab=wb">Blogs</a></span> <span class="gb2"><div></div></span> <span class="gb2"><a href="http://www.youtube.com/?hl=en&amp;tab=w1">YouTube</a></span> <span class="gb2"><a href="http://www.google.com/calendar/render?hl=en&amp;tab=wc">Calendar</a></span> <span class="gb2"><a href="http://picasaweb.google.com/home?hl=en&amp;tab=wq">Photos</a></span> <span class="gb2"><a href="http://docs.google.com/?hl=en&amp;tab=wo">Documents</a></span> <span class="gb2"><a href="http://www.google.com/reader/view/?hl=en&amp;tab=wy">Reader</a></span> <span class="gb2"><div></div></span> <span class="gb2"><a href="http://www.google.com/intl/en/options/">even more »</a></span> </nobr></div><div class="gbh" style="left: 0pt;"></div><div class="gbh" style="right: 0pt;"></div><div id="guser" style="padding: 0pt 0pt 4px; font-size: 84%;" width="100%" align="right"><nobr><b>jcampan@gmail.com</b> | <a href="http://www.google.com/url?sa=p&amp;pref=ig&amp;pval=3&amp;q=http://www.google.com/ig%3Fhl%3Den%26source%3Diglk&amp;usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg">iGoogle</a> | <a href="https://www.google.com/accounts/ManageAccount">My Account</a> | <a href="http://www.google.com/accounts/Logout?continue=http://www.google.com/">Sign out</a></nobr></div><center><br id="lgpd" clear="all"><img alt="Google" src="google_files/logo.gif" height="110" width="276"><br><br><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tbody><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap="nowrap"><input name="hl" value="en" type="hidden"><input maxlength="2048" name="q" size="55" title="Google Search" value=""><br><input name="btnG" value="Google Search" type="submit"><input name="btnI" value="I'm Feeling Lucky" type="submit"></td><td nowrap="nowrap" width="25%"><font size="-2">&nbsp;&nbsp;<a href="http://www.google.com/advanced_search?hl=en">Advanced Search</a><br>&nbsp;&nbsp;<a href="http://www.google.com/preferences?hl=en">Preferences</a><br>&nbsp;&nbsp;<a href="http://www.google.com/language_tools?hl=en">Language Tools</a></font></td></tr></tbody></table></form><br><br><font size="-1"><a href="http://www.google.com/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href="http://www.google.com/services/">Business Solutions</a> - <a href="http://www.google.com/intl/en/about.html">About Google</a></font><p><font size="-2">©2008 Google</font></p></center></body></html> window.gbar={};(function(){var a=window.gbar,b,g,h;function l(c,f,e){c.display=h?"none":"block";c.left=f+"px";c.top=e+"px"}a.tg=function(c){var f=0,e=0,d,m=0,n,j=window.navExtra,k,i=document;g=g||i.getElementById("gbar").getElementsByTagName("span");(c||window.event).cancelBubble=!m;if(!b){b=i.createElement(Array.every||window.createPopup?"iframe":"DIV");b.frameBorder="0";b.scrolling="no";b.src="#";g[7].parentNode.appendChild(b).id="gbi";if(j&&g[7])for(n in j){k=i.createElement("span");k.appendChild(j[n]);g[7].parentNode.insertBefore(k,g[7]).className="gb2"}i.onclick=a.close}while(d=g[++m]){if(e){l(d.style,e+1,f+25);f+=d.firstChild.tagName=="DIV"?9:20}if(d.className=="gb3"){do e+=d.offsetLeft;while(d=d.offsetParent)}}b.style.height=f+"px";l(b.style,e,24);h=!h};a.close=function(c){h&&a.tg(c)}})();</script></head><body onload="sf();if(document.images){new Image().src='/images/nav_logo3.png'}" topmargin="3" alink="#ff0000" bgcolor="#ffffff" link="#0000cc" marginheight="3" text="#000000" vlink="#551a8b"><div id="gbar"><nobr><span class="gb1">Web</span> <span class="gb1"><a href="imghp?hl=en&amp;tab=wi">Images</a></span> <span class="gb1"><a href="maps?hl=en&amp;tab=wl">Maps</a></span> <span class="gb1"><a href="nwshp?hl=en&amp;tab=wn">News</a></span> <span class="gb1"><a href="prdhp?hl=en&amp;tab=wf">Shopping</a></span> <span class="gb1"><a href="mail/?hl=en&amp;tab=wm">Gmail</a></span> <span class="gb3"><a href="intl/en/options/" onclick="this.blur();gbar.tg(event);return !1"><u>more</u> <small></small></a></span> <span class="gb2"><a href="?hl=en&amp;tab=wv">Video</a></span> <span class="gb2"><a href="grphp?hl=en&amp;tab=wg">Groups</a></span> <span class="gb2"><a href="bkshp?hl=en&amp;tab=wp">Books</a></span> <span class="gb2"><a href="schhp?hl=en&amp;tab=ws">Scholar</a></span> <span class="gb2"><a href="finance?hl=en&amp;tab=we">Finance</a></span> <span class="gb2"><a href="?hl=en&amp;tab=wb">Blogs</a></span> <span class="gb2"><div></div></span> <span class="gb2"><a href="?hl=en&amp;tab=w1">YouTube</a></span> <span class="gb2"><a href="calendar/render?hl=en&amp;tab=wc">Calendar</a></span> <span class="gb2"><a href="home?hl=en&amp;tab=wq">Photos</a></span> <span class="gb2"><a href="?hl=en&amp;tab=wo">Documents</a></span> <span class="gb2"><a href="reader/view/?hl=en&amp;tab=wy">Reader</a></span> <span class="gb2"><div></div></span> <span class="gb2"><a href="intl/en/options/">even more »</a></span> </nobr></div><div class="gbh" style="left: 0pt;"></div><div class="gbh" style="right: 0pt;"></div><div id="guser" style="padding: 0pt 0pt 4px; font-size: 84%;" width="100%" align="right"><nobr><b>jcampan@gmail.com</b> | <a href="url?sa=p&amp;pref=ig&amp;pval=3&amp;q=ig%3Fhl%3Den%26source%3Diglk&amp;usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg">iGoogle</a> | <a href="https://www.google.com/accounts/ManageAccount">My Account</a> | <a href="accounts/Logout?continue=">Sign out</a></nobr></div><center><br id="lgpd" clear="all"><img alt="Google" src="google_files/logo.gif" height="110" width="276"><br><br><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tbody><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap="nowrap"><input name="hl" value="en" type="hidden"><input maxlength="2048" name="q" size="55" title="Google Search" value=""><br><input name="btnG" value="Google Search" type="submit"><input name="btnI" value="I'm Feeling Lucky" type="submit"></td><td nowrap="nowrap" width="25%"><font size="-2">&nbsp;&nbsp;<a href="advanced_search?hl=en">Advanced Search</a><br>&nbsp;&nbsp;<a href="preferences?hl=en">Preferences</a><br>&nbsp;&nbsp;<a href="language_tools?hl=en">Language Tools</a></font></td></tr></tbody></table></form><br><br><font size="-1"><a href="intl/en/ads/">Advertising&nbsp;Programs</a> - <a href="services/">Business Solutions</a> - <a href="intl/en/about.html">About Google</a></font><p><font size="-2">©2008 Google</font></p></center></body></html>
\ No newline at end of file
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
#include "chrome/test/ui_test_utils.h" #include "chrome/test/ui_test_utils.h"
#include "base/json_reader.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "chrome/browser/browser.h" #include "chrome/browser/browser.h"
#include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/tab_contents/navigation_controller.h" #include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/web_contents.h" #include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/notification_registrar.h" #include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h" #include "chrome/common/notification_service.h"
#include "chrome/views/widget/accelerator_handler.h" #include "chrome/views/widget/accelerator_handler.h"
...@@ -65,6 +66,32 @@ class NavigationNotificationObserver : public NotificationObserver { ...@@ -65,6 +66,32 @@ class NavigationNotificationObserver : public NotificationObserver {
DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
}; };
class DOMOperationObserver : public NotificationObserver {
public:
explicit DOMOperationObserver(TabContents* tab_contents) {
registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
Source<TabContents>(tab_contents));
RunMessageLoop();
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE);
Details<DomOperationNotificationDetails> dom_op_details(details);
response_ = dom_op_details->json();
MessageLoopForUI::current()->Quit();
}
std::string response() const { return response_; }
private:
NotificationRegistrar registrar_;
std::string response_;
DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
};
} // namespace } // namespace
void RunMessageLoop() { void RunMessageLoop() {
...@@ -98,42 +125,68 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, ...@@ -98,42 +125,68 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
WaitForNavigations(controller, number_of_navigations); WaitForNavigations(controller, number_of_navigations);
} }
JavaScriptRunner::JavaScriptRunner(WebContents* web_contents, Value* ExecuteJavaScript(TabContents* tab_contents,
const std::wstring& frame_xpath, const std::wstring& frame_xpath,
const std::wstring& jscript) const std::wstring& original_script) {
: web_contents_(web_contents), // TODO(jcampan): we should make the domAutomationController not require an
frame_xpath_(frame_xpath), // automation id.
jscript_(jscript) { std::wstring script = L"window.domAutomationController.setAutomationId(0);" +
NotificationService::current()-> original_script;
AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE, tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath,
Source<WebContents>(web_contents)); script);
DOMOperationObserver dom_op_observer(tab_contents);
std::string json = dom_op_observer.response();
// Wrap |json| in an array before deserializing because valid JSON has an
// array or an object as the root.
json.insert(0, "[");
json.append("]");
scoped_ptr<Value> root_val(JSONReader::Read(json, true));
if (!root_val->IsType(Value::TYPE_LIST))
return NULL;
ListValue* list = static_cast<ListValue*>(root_val.get());
Value* result;
if (!list || !list->GetSize() ||
!list->Remove(0, &result)) // Remove gives us ownership of the value.
return NULL;
return result;
} }
void JavaScriptRunner::Observe(NotificationType type, bool ExecuteJavaScriptAndExtractInt(TabContents* tab_contents,
const NotificationSource& source, const std::wstring& frame_xpath,
const NotificationDetails& details) { const std::wstring& script,
Details<DomOperationNotificationDetails> dom_op_details(details); int* result) {
result_ = dom_op_details->json(); DCHECK(result);
// The Jasonified response has quotes, remove them. scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
if (result_.length() > 1 && result_[0] == '"') if (!value.get())
result_ = result_.substr(1, result_.length() - 2); return false;
NotificationService::current()-> return value->GetAsInteger(result);
RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
Source<WebContents>(web_contents_));
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
} }
std::string JavaScriptRunner::Run() { bool ExecuteJavaScriptAndExtractBool(TabContents* tab_contents,
// The DOMAutomationController requires an automation ID, even though we are const std::wstring& frame_xpath,
// not using it in this case. const std::wstring& script,
web_contents_->render_view_host()->ExecuteJavascriptInWebFrame( bool* result) {
frame_xpath_, L"window.domAutomationController.setAutomationId(0);"); DCHECK(result);
scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
if (!value.get())
return false;
web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath_, return value->GetAsBoolean(result);
jscript_);
ui_test_utils::RunMessageLoop();
return result_;
} }
bool ExecuteJavaScriptAndExtractString(TabContents* tab_contents,
const std::wstring& frame_xpath,
const std::wstring& script,
std::string* result) {
DCHECK(result);
scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script));
if (!value.get())
return false;
return value->GetAsString(result);
}
} // namespace ui_test_utils } // namespace ui_test_utils
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
class Browser; class Browser;
class GURL; class GURL;
class NavigationController; class NavigationController;
class WebContents; class Value;
class TabContents;
// A collections of functions designed for use with InProcessBrowserTest. // A collections of functions designed for use with InProcessBrowserTest.
namespace ui_test_utils { namespace ui_test_utils {
...@@ -43,38 +44,32 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, ...@@ -43,38 +44,32 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
int number_of_navigations); int number_of_navigations);
// This class enables you to send JavaScript as a string from the browser to the // Executes the passed |script| in the frame pointed to by |frame_xpath| (use
// renderer for execution in a frame of your choice. // empty string for main frame) and returns the value the evaluation of the
class JavaScriptRunner : public NotificationObserver { // script returned. The caller owns the returned value.
public: Value* ExecuteJavaScript(TabContents* tab_contents,
// Constructor. |web_contents| is a pointer to the WebContents you want to run const std::wstring& frame_xpath,
// the JavaScript code in. |frame_xpath| is a path to the frame to run it in. const std::wstring& script);
// |jscript| is a string containing the JavaScript code to run, for example:
// "window.domAutomationController.send(alert('hello world'));". The // The following methods executes the passed |script| in the frame pointed to by
// JavaScript code will execute when Run is called. Note: In order for the // |frame_xpath| (use empty string for main frame) and sets |result| to the
// domAutomationController to work, you must call EnableDOMAutomation() in // value returned by the script evaluation.
// your test class first. // They return true on success, false if the script evaluation failed or did not
JavaScriptRunner(WebContents* web_contents, // evaluate to the expected type.
const std::wstring& frame_xpath, // Note: In order for the domAutomationController to work, you must call
const std::wstring& jscript); // EnableDOMAutomation() in your test first.
bool ExecuteJavaScriptAndExtractInt(TabContents* tab_contents,
virtual void Observe(NotificationType type, const std::wstring& frame_xpath,
const NotificationSource& source, const std::wstring& script,
const NotificationDetails& details); int* result);
bool ExecuteJavaScriptAndExtractBool(TabContents* tab_contents,
// Executes the JavaScript code passed in to the constructor. See also comment const std::wstring& frame_xpath,
// about EnableDOMAutomation in the constructor. const std::wstring& script,
std::string Run(); bool* result);
bool ExecuteJavaScriptAndExtractString(TabContents* tab_contents,
private: const std::wstring& frame_xpath,
WebContents* web_contents_; const std::wstring& script,
std::wstring frame_xpath_; std::string* result);
std::wstring jscript_;
std::string result_;
DISALLOW_COPY_AND_ASSIGN(JavaScriptRunner);
};
} }
#endif // CHROME_TEST_UI_TEST_UTILS_H_ #endif // CHROME_TEST_UI_TEST_UTILS_H_
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