我想寫一個用swing編寫的scala應用程序的自動化測試。我正在修改來自this article的代碼,該代碼利用getName()在樹中找到正確的UI元素。從scala訪問java.awt.Container.getComponents
這是我的代碼的獨立版本。它是不完整的,未經測試的,並且可能以各種方式破壞和/或非慣用和/或不理想,但是我的問題是關於下面列出的編譯器錯誤。
import scala.swing._
import java.awt._
import ListView._
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.FunSpec
import org.scalatest.matchers.ShouldMatchers
object MyGui extends SimpleSwingApplication {
def top = new MainFrame {
title = "MyGui"
val t = new Table(3, 3)
t.peer.setName("my-table")
contents = t
}
}
object TestUtils {
def getChildNamed(parent: UIElement, name: String): UIElement = {
// Debug line
println("Class: " + parent.peer.getClass() +
" Name: " + parent.peer.getName())
if (name == parent.peer.getName()) { return parent }
if (parent.peer.isInstanceOf[java.awt.Container]) {
val children = parent.peer.getComponents()
/// COMPILER ERROR HERE ^^^
for (child <- children) {
val matching_child = getChildNamed(child, name)
if (matching_child != null) { return matching_child }
}
}
return null
}
}
@RunWith(classOf[JUnitRunner])
class MyGuiSpec extends FunSpec {
describe("My gui window") {
it("should have a table") {
TestUtils.getChildNamed(MyGui.top, "my-table")
}
}
}
當我編譯這個文件,我得到:
29: error: value getComponents is not a member of java.awt.Component
val children = parent.peer.getComponents()
^
one error found
As far as I can tell,getComponents其實java.awt.Component中的一員。我使用the code in this answer來轉儲parent.peer
上的方法,我可以看到getComponents在列表中。
解決方案提供了另一種解決問題的方法(以類似的方式自動化gui測試),但我真的很想理解爲什麼我無法訪問getComponents。
哎呀,我必須看着那個20倍,並沒有看到這是一個不同的類。謝謝。 – bstpierre 2013-04-11 11:46:12