将Specs2与FluentLenium

将Specs2与FluentLenium

本文介绍了将Specs2与FluentLenium Api一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Play2 Framework 2.1提供的Scala 2.10,Specs2 13.1-SNAPSHOT和FluentLenium Api.

I use Scala 2.10, Specs2 13.1-SNAPSHOT and the FluentLenium Api provided by Play2 Framework 2.1.

我的IntegrationSpec文件中包含以下代码行,找到一个子元素(根据FluentLenium规范):

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)

由于泛型,它是否是一种...不兼容的Scala/Java?还是我不知道的正常行为?

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")

推荐答案

haveSize匹配器要求被匹配的元素在范围内具有org.specs2.data.Sized类型类. Java集合的相应类型类为:

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)

或者也许

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

import scala.collection.convert.JavaConverters._

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

这篇关于将Specs2与FluentLenium Api一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:06