问题
I use Scala 2.10, Specs2 13.1-SNAPSHOT and the FluentLenium Api provided by Play2 Framework 2.1.
I have this line of code in my IntegrationSpec
file, finding a child element (according to FluentLenium spec):
browser.find(".myClass").find("#mySubElement") must haveSize(1)
That line leads to the following compilation error:
error: type mismatch;
found : org.fluentlenium.core.domain.FluentList[_ <: org.fluentlenium.core.domain.FluentWebElement]
required: org.fluentlenium.core.domain.FluentList[?0(in value $anonfun)] where type ?0(in value $anonfun) <: org.fluentlenium.core.domain.FluentWebElement
Note: org.fluentlenium.core.domain.FluentWebElement >: ?0, but Java-defined class FluentList is invariant in type E.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
Is it a kind of...incompatibilty Scala/Java due to generics?? or a normal behaviour that I didn't figure out?
This line however (omitting any matcher), well compiles:
browser.find(".myClass").find("#mySubElement")
回答1:
The haveSize
matcher require the element being matched to have an org.specs2.data.Sized
typeclass in scope. The corresponding typeclass for java collections is:
implicit def javaCollectionIsSized[T <: java.util.Collection[_]]: Sized[T] =
new Sized[T] {
def size(t: T) = t.size()
}
I suspect that type inference here is the issue and you could try to tame it with the following ugly code:
browser.find(".myClass").
find("#mySubElement").
asInstanceOf[FluentList[FluentWebElement]] must haveSize(1)
Or maybe
browser.find(".myClass").
find("#mySubElement").
asInstanceOf[Collection[_]] must haveSize(1)
Or
import scala.collection.convert.JavaConverters._
browser.find(".myClass").
find("#mySubElement").
asScala must haveSize(1)
来源:https://stackoverflow.com/questions/14970301/error-using-specs2-with-fluentlenium-api