Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 8 commits
  • 11 files changed
  • 0 commit comments
  • 2 contributors
View
18 src/main/groovy/webdsl/WebDsl.groovy
@@ -20,6 +20,8 @@ import org.codehaus.groovy.runtime.GStringImpl
import webdsl.support.DslFactory
import webdsl.support.FormDsl
import webdsl.support.SelectorDsl
+import webdsl.support.ChildrenDsl
+import com.gargoylesoftware.htmlunit.html.DomText
class WebDsl {
@@ -74,6 +76,11 @@ class WebDsl {
page.getTitleText()
}
+ def back() {
+ webClient.currentWindow.getHistory().back()
+ page = webClient.currentWindow.enclosedPage
+ }
+
private boolean exists(String elementName) {
try {
getProperty(elementName)
@@ -116,10 +123,11 @@ class WebDsl {
def findSelectorsFor(name) {
def result = new SelectorDsl(this, factory)
page.body.children.each { element ->
- if(element.tagName == name) {
+ if(element.class != DomText && element.tagName == name) {
result << element
}
}
+ if(!result.selected) throw new MissingPropertyException(name, this.class)
result
}
@@ -141,6 +149,14 @@ class WebDsl {
}
}
+ def getChildren() {
+ new ChildrenDsl().children(this, page)
+ }
+
+ def children(options) {
+ new ChildrenDsl().children(this, page, options)
+ }
+
static def camel(String string) {
def buffer = new StringBuffer()
View
46 src/main/groovy/webdsl/support/ChildrenDsl.groovy
@@ -0,0 +1,46 @@
+/**
+ * Copyright to the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+package webdsl.support
+
+import webdsl.WebDsl
+import com.gargoylesoftware.htmlunit.html.HtmlElement
+
+
+class ChildrenDsl {
+
+ def children(WebDsl dsl, start) {
+ start.allHtmlChildElements.collect { dsl.factory.create(dsl, it) }
+ }
+
+ def children(WebDsl dsl, start, options) {
+ def result = []
+ start.allHtmlChildElements.each {HtmlElement child ->
+ def types = []
+ if (options?.type) types.addAll(options.type)
+ if (options?.types) types.addAll(options.types)
+
+ if (types && !isInTypes(types, child)) return
+ result << dsl.factory.create(dsl, child)
+ }
+ result
+ }
+
+ private boolean isInTypes(types, element) {
+ types.find { isType it, element }
+ }
+
+ private boolean isType(type, HtmlElement element) {
+ element.tagName == type
+ }
+
+}
View
26 src/main/groovy/webdsl/support/ElementDsl.groovy
@@ -41,10 +41,18 @@ class ElementDsl extends BaseElementDsl {
}
def getText() {
+ untrimmedText.trim()
+ }
+
+ def getUntrimmedText() {
element.getTextContent()
}
def getValue() {
+ untrimmedValue.trim()
+ }
+
+ def getUntrimmedValue() {
element.getAttribute("value")
}
@@ -53,25 +61,11 @@ class ElementDsl extends BaseElementDsl {
}
def getChildren() {
- element.allHtmlChildElements.collect { factory.create(pageContainer, it) }
+ new ChildrenDsl().children(pageContainer, element)
}
def children(options) {
- def result = []
- element.getAllHtmlChildElements().each {HtmlElement child ->
- if (options?.type && !isType(options.type, child)) return
- if (options?.types && !isInTypes(options.types, child)) return
- result << factory.create(pageContainer, child)
- }
- result
- }
-
- private boolean isInTypes(types, element) {
- types.find { isType it, element }
- }
-
- private boolean isType(type, HtmlElement element) {
- element.tagName == type
+ new ChildrenDsl().children(pageContainer, element, options)
}
def getLabel() {
View
14 src/main/groovy/webdsl/support/SelectorDsl.groovy
@@ -43,6 +43,20 @@ class SelectorDsl {
result
}
+ def getChildren() {
+ def collected = selected.collect {
+ new ChildrenDsl().children(dsl, it.element)
+ }
+ collected.flatten()
+ }
+
+ def children(options) {
+ def collected = selected.collect {
+ new ChildrenDsl().children(dsl, it.element, options)
+ }
+ collected.flatten()
+ }
+
void each(Closure closure) {
executeClosureWith(selected.&each, closure)
}
View
79 src/test/groovy/webdsl/DslChildrenTest.groovy
@@ -0,0 +1,79 @@
+/**
+ * Copyright to the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+package webdsl
+
+class DslChildrenTest extends AbstractServerTest {
+
+ protected String defaultPage() {
+ return "children"
+ }
+
+ void test_children() {
+ web.do {
+ assert(['html', 'head', 'title', 'body', 'div', 'span', 'span', 'div', 'div', 'ul', 'li', 'li', 'li'] == children.tagName)
+ }
+ }
+
+ void test_children_of_type() {
+ web.do {
+ assert(['hello', 'world'] == children(type:'span').text)
+ assert(['yo dog', 'message 0message 1message 2message 3', 'message 0'] == children(type:'div').text)
+ }
+ }
+
+ void test_children_of_type__allows_list() {
+ web.do {
+ assert(['hello', 'world'] == children(type:['span']).text)
+ }
+ }
+
+ void test_children_of_types() {
+ web.do {
+ assert(['hello', 'world'] == children(types:['span']).text)
+ assert(['yo dog', 'message 0message 1message 2message 3', 'message 0'] == children(types:['div']).text)
+ assert(['yo dog', 'hello', 'world', 'message 0message 1message 2message 3', 'message 0'] == children(types:['span', 'div']).text)
+ }
+ }
+
+ void test_children_of_types__allows_single_type() {
+ web.do {
+ assert(['hello', 'world'] == children(types: 'span').text)
+ }
+ }
+
+ void test_children_of_type__none_found() {
+ web.do {
+ assert([] == children(types: 'table').text)
+ }
+ }
+
+ void test_element_children() {
+ web.do {
+ assert errors.children.text == ["message 0", "message 1message 2message 3", "message 1", "message 2", "message 3"]
+ }
+ }
+
+ void test_element_children_of_type() {
+ web.do {
+ assert errors.children(type: "li").text == ["message 1", "message 2", "message 3"]
+ }
+ }
+
+ void test_element_children_of_types() {
+ web.do {
+ assert errors.children(types: ["li", "div"]).text == ["message 0", "message 1", "message 2", "message 3"]
+ }
+ }
+
+
+}
View
33 src/test/groovy/webdsl/DslSelectorChildrenTest.groovy
@@ -0,0 +1,33 @@
+/**
+ * Copyright to the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+package webdsl
+
+class DslSelectorChildrenTest extends AbstractServerTest {
+
+ protected String defaultPage() {
+ return "selector"
+ }
+
+ void test_selector_children() {
+ web.do {
+ assert(['tbody', 'tr', 'td', 'td', 'tr', 'td', 'td', 'tr', 'td', 'td'] == table.children.tagName)
+ }
+ }
+
+ void test_selector_children_with_options() {
+ web.do {
+ assert(['value 1', 'value 2', 'value 4', 'value 4'] == span.children(type:'span').text)
+ }
+ }
+
+}
View
2 src/test/groovy/webdsl/DslSelectorTest.groovy
@@ -49,7 +49,7 @@ class DslSelectorTest extends AbstractServerTest {
void test_selector_supports_collect_method() {
web.do {
assertEquals(["first", "last", "Pinky", "Jones", "Winky", "Jones"], names.tr.td.collect { text })
- assertEquals(["imageFirst.gif", "imageLast.gif"], names.tr[0].td.img.collect { attr("src") })
+ assertEquals(['div3_1', 'div3_2', 'div3_3', 'div3_4'], div[2].div.collect { attr("id") })
}
}
View
99 src/test/groovy/webdsl/WebDslTest.groovy
@@ -27,7 +27,7 @@ class WebDslTest extends AbstractServerTest {
}
}
- void test_exists() {
+ void test_exists_navigation() {
web.do {
assertTrue exists('namedRainbow')
@@ -41,6 +41,27 @@ class WebDslTest extends AbstractServerTest {
}
}
+ void test_exists_true() {
+ web.do {
+ assert exists('form0')
+ assert exists('table')
+ assert exists('table4')
+ assert exists('myOrderedList')
+ assert exists('errors')
+ assert exists('rainbow')
+ assert exists('auto2Id')
+ assert exists('textToEcho')
+ }
+ }
+
+ void test_exists_false() {
+ web.do {
+ assert !exists('table5')
+ assert !exists('form4')
+ assert !exists('orderedList')
+ }
+ }
+
void test_title() {
web.do {
assertEquals "Main Page 1", title
@@ -98,33 +119,6 @@ class WebDslTest extends AbstractServerTest {
}
}
- void test_textOfChildren() {
- def expected = ["message 0", "message 1message 2message 3", "message 1", "message 2", "message 3"]
- web.do {
- bind.actual = errors.children.text
- }
- assertEquals expected.size(), web.bind.actual.size()
- assertEquals expected, web.bind.actual
- }
-
- void test_textOfChildren_of_type() {
- def expected = ["message 1", "message 2", "message 3"]
- web.do {
- bind.actual = errors.children(type: "li").text
- }
- assertEquals expected.size(), web.bind.actual.size()
- assertEquals expected, web.bind.actual
- }
-
- void test_textOfChildren_of_types() {
- def expected = ["message 0", "message 1", "message 2", "message 3"]
- web.do {
- bind.actual = errors.children(types: ["li", "div"]).text
- }
- assertEquals expected.size(), web.bind.actual.size()
- assertEquals expected, web.bind.actual
- }
-
void test_form_defaults_to_first_form_on_page() {
web.do {
form {
@@ -676,4 +670,53 @@ class WebDslTest extends AbstractServerTest {
assertEquals "UPPER", "Upper".text
}
}
+
+ void test_tagName() {
+ web.do {
+ assert errors.tagName == "div"
+ assert table1.tagName == "table"
+ assert myUnorderedList.tagName == "ul"
+ assert myOrderedList.tagName == "ol"
+ assert form0.tagName == "form"
+ assert nameId.tagName == "input"
+ assert autoId.tagName == "select"
+ assert checkbox1.tagName == "input"
+ assert radio1_1.tagName == "input"
+ assert submit1.tagName == "input"
+ }
+ }
+
+ void test_text_is_trimmed() {
+ web.do {
+ assert multiline.text == "multi-line text"
+ }
+ }
+
+ void test_untrimmedText() {
+ web.do {
+ assert multiline.untrimmedText == "\n multi-line text\n"
+ }
+ }
+
+ void test_value_is_trimmed() {
+ web.do {
+ assert valueWithSpaces.value == "abc"
+ }
+ }
+
+ void test_untrimmedValue() {
+ web.do {
+ assert valueWithSpaces.untrimmedValue == " abc "
+ }
+ }
+
+ void test_back() {
+ web.do {
+ assert title == "Main Page 1"
+ rainbow.click()
+ assert title == "Rainbow Page"
+ back()
+ assert title == "Main Page 1"
+ }
+ }
}
View
19 src/test/resources/webapps/test/children.html
@@ -0,0 +1,19 @@
+<html>
+<head>
+ <title>Children</title>
+</head>
+<body>
+ <div>yo dog</div>
+ <span>hello</span>
+ <span>world</span>
+
+ <div id="errors" class="errors">
+ <div id="message0">message 0</div>
+ <ul>
+ <li>message 1</li>
+ <li>message 2</li>
+ <li>message 3</li>
+ </ul>
+ </div>
+</body>
+</html>
View
6 src/test/resources/webapps/test/main.html
@@ -171,5 +171,11 @@
<a id="rainbow" name="namedRainbow" href="rainbow.html">the rainbow</a>
<a id="cloud" name="namedCloud" href="cloud.html">the cloud</a>
+<div id="multiline">
+ multi-line text
+</div>
+
+<input id="valueWithSpaces" type="text" value=" abc "/>
+
</body>
</html>
View
15 src/test/resources/webapps/test/selector.html
@@ -18,8 +18,8 @@
<table id="names">
<tr>
- <td>first<img src="imageFirst.gif"/></td>
- <td>last<img src="imageLast.gif"/></td>
+ <td>first</td>
+ <td>last</td>
</tr>
<tr>
<td>Pinky</td>
@@ -30,5 +30,16 @@
<td>Jones</td>
</tr>
</table>
+
+<span>
+ <span>value 1</span>
+ <span>value 2</span>
+</span>
+<span>value 3</span>
+<span>
+ <span>
+ <span>value 4</span>
+ </span>
+</span>
</body>
</html>

No commit comments for this range

Something went wrong with that request. Please try again.