Skip to content

Commit 7191c3e

Browse files
committed
Added Element's child lookup functions childElement, findChildElement, findAllChildElements and childWebElement.
1 parent 81d9f89 commit 7191c3e

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
@@ -1299,6 +1299,102 @@ trait WebBrowser {
12991299
if (txt != null) txt else "" // Just in case, I'm not sure if Selenium would ever return null here
13001300
}
13011301

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

0 commit comments

Comments
 (0)