2

我有以下设置:

  • 安装 JDK & JRE 6u29
  • 安装硒独立2.8
  • Groovy 1.8.3
  • 盖布 0.6.1

我只使用 GroovyConsole 尝试执行 Geb 手册中给出的第一个示例:

import geb.Browser

Browser.drive {
    go "http://google.com/ncr"

// make sure we actually got to the page
assert title == "Google"

// enter wikipedia into the search field
$("input", name: "q").value("wikipedia")

// wait for the change to results page to happen
// (google updates the page dynamically without a new request)
waitFor { title.endsWith("Google Search") }

// is the first link to wikipedia?
def firstLink = $("li.g", 0).find("a.l")
assert firstLink.text() == "Wikipedia"

// click the link 
firstLink.click()

// wait for Google's javascript to redirect to Wikipedia
waitFor { title == "Wikipedia" }
}

但我收到以下错误:

警告:清理堆栈跟踪:

geb.waiting.WaitTimeoutException:条件未在 5.0 秒内通过

这个例子有什么问题吗?我做错了什么吗?看到第一个示例甚至无法运行,这非常令人沮丧!

4

4 回答 4

3

该脚本正在进入wikipedia“搜索”框,但没有Google Search按下按钮开始搜索。

如果添加:

// hit the "Google Search" button

$("input", name: "btnG").click()

紧接着

// enter wikipedia into the search field

$("input", name: "q").value("wikipedia")

你会走得更远一点。

于 2011-10-27T16:34:52.817 回答
2

我遇到了同样的问题。

第一个 WaitFor 问题:

J. Levine 的回答可以解决第一次等待。添加:

$("输入",名称:"btnG").click()

后:

$("输入",名称:"q").value("wikipedia")。

第二个 WaitFor 问题:

维基百科从谷歌打开的页面标题与维基百科主页不同。在主页上它在主页<title>Wikipedia<title>上(谷歌打开的页面是<title>Wikipedia, the free encyclopedia<title>.

所以改变:

waitFor { title == "维基百科" } }
收件人:
waitFor { title == "维基百科,免费的百科全书" }
}

这应该可以解决第二个等待问题

于 2014-10-22T15:06:09.030 回答
2

该示例利用了 Google 中的自动加载功能,在您输入搜索内容时会显示搜索结果,因此您无需单击搜索按钮。运行测试时,您应该会看到搜索结果已显示,并且 Wikipedia 链接是第一个。

您收到的 WaitTimeoutException 很可能是因为浏览器在到达 Wikipedia 页面后关闭得太快。要解决这个问题,只需更新 waitFor 调用,使其在关闭浏览器之前等待更长时间,即

waitFor(10) (at WikipediaPage)

或者,如果您在调试模式下运行 gradle,该过程会慢得多,因此允许测试在浏览器终止之前检查标题

gradlew firefoxTest --debug --stacktrace
于 2013-03-24T07:31:05.363 回答
1

以上都不适合我。经过一些调试后,我得到了这样的代码:

Browser.drive {

    go "http://google.com"

    // make sure we actually got to the page
    assert title == "Google"

    // enter wikipedia into the search field
    $("input", name: "q").value("wikipedia")

    // wait for the change to results page to happen
    // (google updates the page dynamically without a new request)
    waitFor { title.endsWith("Google Search") }

    // is the first link to wikipedia?
    def firstLink = $("li.g", 0).find("a")
    assert firstLink.text() == "Wikipedia, the free encyclopedia"

    // click the link
    firstLink.click()

    // wait for Google's javascript to redirect to Wikipedia
    waitFor { title == "Wikipedia, the free encyclopedia" }
}
于 2015-02-28T20:35:05.500 回答