Geb 手册上的第一个示例未执行

发布于 2024-12-11 19:18:14 字数 1000 浏览 1 评论 0原文

我有以下设置:

  • 安装了 JDK & JRE 6u29
  • 安装了 selenium 独立版 2.8
  • Groovy 1.8.3
  • Geb 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 中条件未通过 秒

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

I have the following setup:

  • installed JDK & JRE 6u29
  • installed selenium standalone 2.8
  • Groovy 1.8.3
  • Geb 0.6.1

Using just the GroovyConsole i tried to execute the very first example given in the Geb manual :

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

but am getting the following error:

WARNING: Sanitizing stacktrace:

geb.waiting.WaitTimeoutException: condition did not pass in 5.0
seconds

Is there something wrong w/ the example? Am I doing something incorrect? this is very frustrating seeing as how the VERY first example won't even run!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羅雙樹 2024-12-18 19:18:14

该脚本将 wikipedia 输入到搜索框中,但并未点击 Google 搜索 按钮来启动搜索。

如果你加上:

// hit the "Google Search" button

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

就在

// enter wikipedia into the search field

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

你会走得更远一点之后。

The script is entering wikipedia into the Search box, but it's not hitting the Google Search button to kick off the search.

If you add:

// hit the "Google Search" button

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

right after

// enter wikipedia into the search field

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

you'll get a little farther.

幸福还没到 2024-12-18 19:18:14

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

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

waitFor(10) (at WikipediaPage)

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

gradlew firefoxTest --debug --stacktrace

The example makes use of the auto load feature in Google whereby search results are displayed as you type in the search, hence you don't need to click on the search button. When you run the test, you should see that the search results are displayed and the Wikipedia link is the first.

The WaitTimeoutException you are getting is most probably because the browser closes too quickly after arriving at the Wikipedia page. To fix that, just update the waitFor call to make it wait longer before closing the browser i.e.

waitFor(10) (at WikipediaPage)

Alternatively, if you run gradle in debug mode, the process is much slower and hence allows the test to check for the title before the browser is terminated

gradlew firefoxTest --debug --stacktrace
分分钟 2024-12-18 19:18:14

我遇到了与您遇到的同样的问题。

第一个 WaitFor 问题:

第一个等待可以通过 J. Levine 的回答来解决。添加:

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

之后:

$(“输入”,名称:“q”).value(“维基百科”)。

第二个WaitFor问题:

维基百科从google打开的页面标题与维基百科主页不同。在主页上,它是主页上的 Wikipedia<title></code> (Google 打开的是 <code><title>Wikipedia,免费的百科全书<title>。</code>

所以改变:

waitFor { title == "维基百科" } }
致:
waitFor { title == "维基百科,免费百科全书" }
}

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

I had the same problem you are experiencing.

The first WaitFor issue:

The first wait for can be fixed by J. Levine's answer. Adding:

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

after:

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

The second WaitFor issue:

The title of the page that Wikipedia opens to from google is different from the Wikipedia Homepage. On the homepage it is <title>Wikipedia<title> on the main page(which google opens to is <title>Wikipedia, the free encyclopedia<title>.

So change:

waitFor { title == "Wikipedia" } }
To:
waitFor { title == "Wikipedia, the free encyclopedia" }
}

And that should fix the second wait issue

○愚か者の日 2024-12-18 19:18:14

以上都不适合我。经过一些调试后,我得到的代码工作如下:

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

None of the above worked for me. After some debugging I got the code working like this:

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" }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文