简体   繁体   中英

Iterate/foreach over a FluentLenium List in Scala

I am new to Scala and I'm having an issue while writing a selenium test. I am retrieving a list of webelements (for our purposes lets assume they can't be retrieved individually). The list is coming back as a FluentList .

I can operate on the fluent list to retrieve elements with some of its own functions, like first() , but when I try and iterate over the list I get the following error:

 value foreach is not a member of org.fluentlenium.core.domain.FluentList[org.fluentlenium.core.domain.FluentWebElement]

From my reading thus far I think I need to somehow convert this list to a Scala list, but have had no success so far.

Am I on the right track? Or should I being approaching this issue differently?

Relevant code:

val icons = browser.$(Selectors.social_icons)

icons.foreach {
  icon.isDisplayed
}
option_text must be equalTo browser.$(Selectors.answered_question)

When working with Java lists, you have to convert them to Scala lists to use the methods you're expecting. You have two options:

Explicitly convert the lists with scala.collection.JavaConverters

import scala.collection.JavaConverters._
icons.asScala.foreach ...

Implicitly convert the lists with scala.collection.JavaConversions :

import scala.collection.JavaConversion._
icons.foreach ...

While the implicit version is clearly cleaner, some people prefer the explicit version because it clearly marks the conversion from Java to Scala, and might help when debugging mysterious implicit conversions.

You can always check the Java docs to see what type to expect. For example, the documentation of FluentList shows:

FluentList<E extends FluentWebElement> extends List<E> ...

So we can expect Scala to convert this to a scala.List[FluentWebElement .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM