使用Specs2与FluentLenium API错误(Error using Specs2 wit

2019-10-17 17:48发布

我用Scala的2.10,Specs2 13.1快照和Play2框架2.1提供的FluentLenium API。

我有这行代码在我IntegrationSpec文件,找到一个子元素(根据FluentLenium规格):

browser.find(".myClass").find("#mySubElement") must haveSize(1)

该行导致以下编译错误:

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)

它是一种...不相容性斯卡拉/ Java的,由于仿制药? 或者一个正常的行为,我没有搞清楚?

这条线然而(忽略任何匹配),以及编译:

browser.find(".myClass").find("#mySubElement")

Answer 1:

所述haveSize匹配所需要的元件被匹配成具有org.specs2.data.Sized范围类型类。 对于Java集合对应的类型类是:

implicit def javaCollectionIsSized[T <: java.util.Collection[_]]: Sized[T] = 
  new Sized[T] {
    def size(t: T) = t.size()
  }

我怀疑在这里,类型推断是问题,你可以尝试用下面的丑陋的代码驯服它:

browser.find(".myClass").
        find("#mySubElement").
        asInstanceOf[FluentList[FluentWebElement]] must haveSize(1)

或者可能

browser.find(".myClass").
        find("#mySubElement").
        asInstanceOf[Collection[_]] must haveSize(1)

要么

import scala.collection.convert.JavaConverters._

browser.find(".myClass").
        find("#mySubElement").
        asScala must haveSize(1)


文章来源: Error using Specs2 with FluentLenium Api