用geb上传文件

时间:2013-03-27 12:04:08

标签: groovy selenium-webdriver geb

一切! 我想在我的测试中上传文件。在Geb-book上写下它是如何制作的,但它对我不起作用。

我有字段:

<input id="bla-bl-bla" type="file" name="foo-foo-foo">

按钮:

<input type="submit" value="Загрузить файл" name="bar-bar-bar">

我试试:

    def "choose banner format"() {
    when: "choose fake format"
        $("select", name: Const.namespaceId + "createBanner:j_id83:j_id91")
    and: "wait load"
        waitFor(25, 2) { $("input", name : Const.namespaceId + "createBanner:upload:j_id256")}
    then: "can upload file"
        $("input", id : "name") << name_banner
        String file_1 = "/full/path/to/file"
        String uploadfile = "foo-foo-foo"
        $("input").uploadfile  =  banner
        $("input", name : "bar-bar-bar").click()
        def createActLink = $("input", name : "save")
        createActLink.click()
        sleep 20000
}

但我有错误:

Expected a condition, but found an assignment. Did you intend to write '==' ?

Geb:0.9.0-RC-1 Groovy:2.1.1 浏览器:FF 19

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,您应该认真考虑使用页面模型重写此代码,如here所述。它会让你的生活更轻松。虽然如果你想要的只是让代码工作,试试这样的东西......

def "choose banner format"() {
given:
    String file_1 = "/full/path/to/file"
    String uploadfile = "foo-foo-foo"
when: 
    //if j_id83 and j_id91 are integers, caste them to strings!
    $("select", name: Const.namespaceId.toString() + "createBanner:j_id83:j_id91")
    waitFor(25, 2) { $("input", name : Const.namespaceId + "createBanner:upload:j_id256")}
    $("input", id : "name") << name_banner
    $("input").uploadfile  =  banner
    $("input", name : "bar-bar-bar").click()
    waitFor{ $("input", name : "save") }
    def createActLink = $("input", name : "save")
    createActLink.click()
then:
    waitFor{
      // choose an element to refresh on page after input is clicked   
    } 

}