Index: chrome/test/automation/dom_automation_browsertest.cc |
diff --git a/chrome/test/automation/dom_automation_browsertest.cc b/chrome/test/automation/dom_automation_browsertest.cc |
index e8c623bc60616e0273d80194f6f19cd51d06a5e8..ba267d4ea3e58a68c630502583570ada4c9d09f7 100644 |
--- a/chrome/test/automation/dom_automation_browsertest.cc |
+++ b/chrome/test/automation/dom_automation_browsertest.cc |
@@ -13,6 +13,43 @@ |
namespace { |
+// Asserts that |expected_text| matches all the text in this element. This |
+// includes the value of textfields and inputs. |
+void EnsureTextMatches( |
+ DOMElementProxyRef proxy, const std::string& expected_text) { |
+ std::string text; |
+ ASSERT_TRUE(proxy->GetText(&text)); |
+ ASSERT_EQ(expected_text, text); |
+} |
+ |
+// Asserts that |expected_html| matches the element's inner html. |
+void EnsureInnerHTMLMatches( |
+ DOMElementProxyRef proxy, const std::string& expected_html) { |
+ std::string html; |
+ ASSERT_TRUE(proxy->GetInnerHTML(&html)); |
+ ASSERT_EQ(expected_html, html); |
+} |
+ |
+// Asserts that |expected_name| matches the element's name. |
+void EnsureNameMatches( |
+ DOMElementProxyRef proxy, const std::string& expected_name) { |
+ std::string name; |
+ ASSERT_TRUE(proxy->GetName(&name)); |
+ ASSERT_EQ(expected_name, name); |
+} |
+ |
+// Asserts that |expected_value| eventually matches the element's value for |
+// |attribute|. This function will block until the timeout is exceeded, in |
+// which case it will fail, or until the two values match. |
+void EnsureAttributeEventuallyMatches( |
+ DOMElementProxyRef proxy, |
+ const std::string& attribute, |
+ const std::string& new_value) { |
+ ASSERT_TRUE(proxy->is_valid()); |
+ if (!proxy->DoesAttributeEventuallyMatch(attribute, new_value)) |
+ FAIL() << "Executing or parsing JavaScript failed"; |
+} |
+ |
// Tests the DOMAutomation framework for manipulating DOMElements within |
// browser tests. |
class DOMAutomationTest : public InProcessBrowserTest { |
@@ -46,15 +83,15 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, MAYBE_FindByXPath) { |
// Find first element. |
DOMElementProxyRef first_div = main_doc->FindElement(By::XPath("//div")); |
ASSERT_TRUE(first_div); |
- ASSERT_NO_FATAL_FAILURE(first_div->EnsureNameMatches("0")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(first_div, "0")); |
// Find many elements. |
std::vector<DOMElementProxyRef> elements; |
ASSERT_TRUE(main_doc->FindElements(By::XPath("//div"), &elements)); |
ASSERT_EQ(2u, elements.size()); |
for (size_t i = 0; i < elements.size(); i++) { |
- ASSERT_NO_FATAL_FAILURE(elements[i]->EnsureNameMatches( |
- base::UintToString(i))); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches( |
+ elements[i], base::UintToString(i))); |
} |
// Find 0 elements. |
@@ -77,7 +114,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, MAYBE_FindByXPath) { |
while (node) { |
nested_count++; |
span_name.append("span"); |
- ASSERT_NO_FATAL_FAILURE(node->EnsureNameMatches(span_name)); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(node, span_name)); |
node = node->FindElement(By::XPath("./span")); |
} |
ASSERT_EQ(3, nested_count); |
@@ -93,15 +130,15 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, FindBySelectors) { |
DOMElementProxyRef first_myclass = |
main_doc->FindElement(By::Selectors(".myclass")); |
ASSERT_TRUE(first_myclass); |
- ASSERT_NO_FATAL_FAILURE(first_myclass->EnsureNameMatches("0")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(first_myclass, "0")); |
// Find many elements. |
std::vector<DOMElementProxyRef> elements; |
ASSERT_TRUE(main_doc->FindElements(By::Selectors(".myclass"), &elements)); |
ASSERT_EQ(2u, elements.size()); |
for (size_t i = 0; i < elements.size(); i++) { |
- ASSERT_NO_FATAL_FAILURE(elements[i]->EnsureNameMatches( |
- base::UintToString(i))); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches( |
+ elements[i], base::UintToString(i))); |
} |
// Find 0 elements. |
@@ -121,7 +158,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, FindBySelectors) { |
while (node) { |
nested_count++; |
span_name.append("span"); |
- ASSERT_NO_FATAL_FAILURE(node->EnsureNameMatches(span_name)); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(node, span_name)); |
node = node->FindElement(By::Selectors("span")); |
} |
ASSERT_EQ(3, nested_count); |
@@ -142,15 +179,15 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, MAYBE_FindByText) { |
// Find first element. |
DOMElementProxyRef first_text = main_doc->FindElement(By::Text("div_text")); |
ASSERT_TRUE(first_text); |
- ASSERT_NO_FATAL_FAILURE(first_text->EnsureNameMatches("0")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(first_text, "0")); |
// Find many elements. |
std::vector<DOMElementProxyRef> elements; |
ASSERT_TRUE(main_doc->FindElements(By::Text("div_text"), &elements)); |
ASSERT_EQ(2u, elements.size()); |
for (size_t i = 0; i < elements.size(); i++) { |
- ASSERT_NO_FATAL_FAILURE(elements[i]->EnsureNameMatches( |
- base::UintToString(i))); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches( |
+ elements[i], base::UintToString(i))); |
} |
// Find 0 elements. |
@@ -166,7 +203,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, MAYBE_FindByText) { |
while (node) { |
nested_count++; |
span_name.append("span"); |
- ASSERT_NO_FATAL_FAILURE(node->EnsureNameMatches(span_name)); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(node, span_name)); |
node = node->FindElement(By::Text("span_text")); |
} |
ASSERT_EQ(3, nested_count); |
@@ -174,7 +211,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, MAYBE_FindByText) { |
// Find only visible text. |
DOMElementProxyRef shown_td = main_doc->FindElement(By::Text("table_text")); |
ASSERT_TRUE(shown_td); |
- ASSERT_NO_FATAL_FAILURE(shown_td->EnsureNameMatches("shown")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureNameMatches(shown_td, "shown")); |
// Find text in inputs. |
ASSERT_TRUE(main_doc->FindElement(By::Text("textarea_text"))); |
@@ -189,7 +226,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, WaitFor1VisibleElement) { |
DOMElementProxyRef div = |
main_doc->WaitFor1VisibleElement(By::Selectors("div")); |
ASSERT_TRUE(div.get()); |
- ASSERT_NO_FATAL_FAILURE(div->EnsureInnerHTMLMatches("div_inner")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureInnerHTMLMatches(div, "div_inner")); |
} |
IN_PROC_BROWSER_TEST_F(DOMAutomationTest, WaitForElementsToDisappear) { |
@@ -210,8 +247,8 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, EnsureAttributeEventuallyMatches) { |
DOMElementProxyRef anchor = main_doc->FindElement(By::Selectors("a")); |
ASSERT_TRUE(anchor.get()); |
- ASSERT_NO_FATAL_FAILURE(anchor->EnsureAttributeEventuallyMatches( |
- "href", "http://www.google.com")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureAttributeEventuallyMatches( |
+ anchor, "href", "http://www.google.com")); |
} |
IN_PROC_BROWSER_TEST_F(DOMAutomationTest, Frames) { |
@@ -231,10 +268,10 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, Frames) { |
DOMElementProxyRef frame_div = |
frame1->FindElement(By::XPath("/html/body/div")); |
ASSERT_TRUE(frame_div); |
- ASSERT_NO_FATAL_FAILURE(frame_div->EnsureInnerHTMLMatches("frame 1")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureInnerHTMLMatches(frame_div, "frame 1")); |
frame_div = frame2->FindElement(By::XPath("/html/body/div")); |
ASSERT_TRUE(frame_div); |
- ASSERT_NO_FATAL_FAILURE(frame_div->EnsureInnerHTMLMatches("frame 2")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureInnerHTMLMatches(frame_div, "frame 2")); |
// Get both inner iframes, checking their contents are correct. |
DOMElementProxyRef iframe1 = |
@@ -244,10 +281,10 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, Frames) { |
ASSERT_TRUE(iframe1 && iframe2); |
frame_div = iframe1->FindElement(By::XPath("/html/body/div")); |
ASSERT_TRUE(frame_div); |
- ASSERT_NO_FATAL_FAILURE(frame_div->EnsureInnerHTMLMatches("iframe 1")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureInnerHTMLMatches(frame_div, "iframe 1")); |
frame_div = iframe2->FindElement(By::XPath("/html/body/div")); |
ASSERT_TRUE(frame_div); |
- ASSERT_NO_FATAL_FAILURE(frame_div->EnsureInnerHTMLMatches("iframe 2")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureInnerHTMLMatches(frame_div, "iframe 2")); |
// Get nested frame. |
ASSERT_EQ(iframe1.get(), main_doc->GetDocumentFromFrame("0", "0").get()); |
@@ -262,24 +299,24 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, Events) { |
// Click link and make sure text changes. |
DOMElementProxyRef link = main_doc->FindElement(By::Selectors("a")); |
ASSERT_TRUE(link && link->Click()); |
- ASSERT_NO_FATAL_FAILURE(link->EnsureTextMatches("clicked")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches(link, "clicked")); |
// Click input button and make sure textfield changes. |
DOMElementProxyRef button = main_doc->FindElement(By::Selectors("#button")); |
DOMElementProxyRef textfield = |
main_doc->FindElement(By::Selectors("#textfield")); |
ASSERT_TRUE(textfield && button && button->Click()); |
- ASSERT_NO_FATAL_FAILURE(textfield->EnsureTextMatches("clicked")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches(textfield, "clicked")); |
// Type in the textfield. |
ASSERT_TRUE(textfield->SetText("test")); |
- ASSERT_NO_FATAL_FAILURE(textfield->EnsureTextMatches("test")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches(textfield, "test")); |
// Type in the textarea. |
DOMElementProxyRef textarea = |
main_doc->FindElement(By::Selectors("textarea")); |
ASSERT_TRUE(textarea && textarea->Type("test")); |
- ASSERT_NO_FATAL_FAILURE(textarea->EnsureTextMatches("textareatest")); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches(textarea, "textareatest")); |
} |
IN_PROC_BROWSER_TEST_F(DOMAutomationTest, StringEscape) { |
@@ -291,7 +328,7 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, StringEscape) { |
DOMElementProxyRef textarea = |
main_doc->FindElement(By::Selectors("textarea")); |
ASSERT_TRUE(textarea); |
- ASSERT_NO_FATAL_FAILURE(textarea->EnsureTextMatches(WideToUTF8(L"\u00FF"))); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches(textarea, WideToUTF8(L"\u00FF"))); |
const wchar_t* set_and_expect_strings[] = { |
L"\u00FF and \u00FF", |
@@ -300,8 +337,8 @@ IN_PROC_BROWSER_TEST_F(DOMAutomationTest, StringEscape) { |
}; |
for (size_t i = 0; i < 3; i++) { |
ASSERT_TRUE(textarea->SetText(WideToUTF8(set_and_expect_strings[i]))); |
- ASSERT_NO_FATAL_FAILURE(textarea->EnsureTextMatches( |
- WideToUTF8(set_and_expect_strings[i]))); |
+ ASSERT_NO_FATAL_FAILURE(EnsureTextMatches( |
+ textarea, WideToUTF8(set_and_expect_strings[i]))); |
} |
} |