Skip to content

Commit a583343

Browse files
authored
Merge pull request #18 from scalatest/feature-element-child-lookup-functions
Element Child Lookup Functions
2 parents 7ed187e + 91634c5 commit a583343

File tree

3 files changed

+618
-26
lines changed

3 files changed

+618
-26
lines changed

src/main/scala/org/scalatestplus/selenium/WebBrowser.scala

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,102 @@ trait WebBrowser {
13001300
if (txt != null) txt else "" // Just in case, I'm not sure if Selenium would ever return null here
13011301
}
13021302

1303+
/**
1304+
* Returns the first child <code>Element</code> selected by the provided query, or throws <code>TestFailedException</code>
1305+
* if no <code>Element</code> is selected.
1306+
*
1307+
* <p>
1308+
* The class of the <code>Element</code> returned will be a subtype of <code>Element</code> if appropriate.
1309+
* For example, if this query selects a text field, the class of the returned <code>Element</code> will
1310+
* be <code>TextField</code>.
1311+
* </p>
1312+
*
1313+
* @param query the <code>Query</code> used to lookup the child element
1314+
* @return the child <code>Element</code> selected by the provided query
1315+
* @throws TestFailedException if nothing is selected by the provided query
1316+
*/
1317+
def childElement(query: Query)(implicit pos: source.Position = implicitly[source.Position]): Element = {
1318+
try {
1319+
createTypedElement(underlying.findElement(query.by), pos)
1320+
}
1321+
catch {
1322+
case e: org.openqa.selenium.NoSuchElementException =>
1323+
// the following is avoid the suite instance to be bound/dragged into the messageFun, which can cause serialization problem.
1324+
val queryStringValue = query.queryString
1325+
throw new TestFailedException(
1326+
(_: StackDepthException) => Some("Element '" + queryStringValue + "' not found."),
1327+
Some(e),
1328+
pos
1329+
)
1330+
}
1331+
}
1332+
1333+
/**
1334+
* Returns the first child <code>Element</code> selected by the provided query, wrapped in a <code>Some</code>, or <code>None</code>
1335+
* if no <code>Element</code> is selected.
1336+
*
1337+
* <p>
1338+
* The class of the <code>Element</code> returned will be a subtype of <code>Element</code> if appropriate.
1339+
* For example, if this query selects a text field, the class of the returned <code>Element</code> will
1340+
* be <code>TextField</code>.
1341+
* </p>
1342+
*
1343+
* @param query the <code>Query</code> used to lookup the child element
1344+
* @return the child <code>Element</code> selected by the provided query, wrapped in a <code>Some</code>, or <code>None</code> if
1345+
* no <code>Element</code> is selected
1346+
*/
1347+
def findChildElement(query: Query)(implicit pos: source.Position = implicitly[source.Position]): Option[Element] =
1348+
try {
1349+
Some(createTypedElement(underlying.findElement(query.by), pos))
1350+
}
1351+
catch {
1352+
case _: org.openqa.selenium.NoSuchElementException => None
1353+
}
1354+
1355+
/**
1356+
* Returns an <code>Iterator</code> over all child <code>Element</code>s selected by the provided query.
1357+
*
1358+
* <p>
1359+
* The class of the <code>Element</code>s produced by the returned <code>Iterator</code> will be a
1360+
* subtypes of <code>Element</code> if appropriate. For example, if an <code>Element</code>representing
1361+
* a text field is returned by the <code>Iterator</code>, the class of the returned <code>Element</code> will
1362+
* be <code>TextField</code>.
1363+
* </p>
1364+
*
1365+
* <p>
1366+
* If no <code>Elements</code> are selected by the provided query, this method will return an empty <code>Iterator</code> will be returned.
1367+
* <p>
1368+
*
1369+
* @param query the <code>Query</code> used to lookup the child elements
1370+
* @return the <code>Iterator</code> over all <code>Element</code>s selected by the provided query
1371+
*/
1372+
def findAllChildElements(query: Query)(implicit pos: source.Position = implicitly[source.Position]): Iterator[Element] =
1373+
underlying.findElements(query.by).asScala.toIterator.map { e => createTypedElement(e, pos) }
1374+
1375+
/**
1376+
* Returns the first <code>WebElement</code> selected by the provided query, or throws <code>TestFailedException</code>
1377+
* if no <code>WebElement</code> is selected.
1378+
*
1379+
* @param query the <code>Query</code> used to lookup the child element
1380+
* @return the child <code>WebElement</code> selected by the provided query
1381+
* @throws TestFailedException if nothing is selected by the provided query
1382+
*/
1383+
def childWebElement(query: Query)(implicit pos: source.Position = implicitly[source.Position]): WebElement = {
1384+
try {
1385+
underlying.findElement(query.by)
1386+
}
1387+
catch {
1388+
case e: org.openqa.selenium.NoSuchElementException =>
1389+
// the following is avoid the suite instance to be bound/dragged into the messageFun, which can cause serialization problem.
1390+
val queryStringValue = query.queryString
1391+
throw new TestFailedException(
1392+
(_: StackDepthException) => Some("WebElement '" + queryStringValue + "' not found."),
1393+
Some(e),
1394+
pos
1395+
)
1396+
}
1397+
}
1398+
13031399
/**
13041400
* Returns the result of invoking <code>equals</code> on the underlying <code>Element</code>, passing
13051401
* in the specified <code>other</code> object.

0 commit comments

Comments
 (0)