【问题标题】:Geb picking a link from a listGeb 从列表中选择一个链接
【发布时间】:2014-05-29 13:33:46
【问题描述】:

我最近一直在阅读 Geb 的书,并试图掌握它,因为它似乎是一个很棒的工具。我觉得我快到了,但仍有一些我似乎无法掌握的真正核心概念。

以网站为例:- http://www.escapistmagazine.com/videos/view/zero-punctuation

现在,如果我想测试列出的第一个视频是否链接到正确的页面,我首先想到的是执行以下操作

class ZeroPunctuationIndexPage extends Page {
    static url = "http://www.escapistmagazine.com/videos/view/zero-punctuation"
    static at = {title == "Zero Punctuation Video Gallery | The Escapist"}
    static content = {

        selectFirstVideo {$("a", 0, class: "filmstrip_video")}
        firstVideoTitle {$("i", 0, class: "filmstrip_video")}

    }
}

class ZeroPunctuationIndexSpec extends GebReportingSpec {

    def "Click the latest video and play it"(){
        given:
        to ZeroPunctuationIndexPage

        when:
        selectFirstVideo.click()

        then:
        title.endsWith(firstVideoTitle)

    }
}

基本上我想如果我选择包含视频的第一个类 (filmstrip_video),然后选择其中的链接,然后我可以单击它,并将链接中的视频标题与新页面标题进行比较。

再看一遍我并不惊讶它没有工作,但我不知道该怎么办。

如果有人能给我一个快速的解决方案,我将不胜感激。我没有对逃避现实页面做任何特别的事情,我只是选择了一个有视频索引的地方来尝试编写测试。

谢谢!

【问题讨论】:

    标签: groovy spock geb


    【解决方案1】:

    它没有获得链接并且失败了,所以我修复了它并仔细检查了。现在,它工作正常并且通过了。此外,当您选择第一个元素时,您不需要显式写入 0,因为默认情况下它将选择第 0 个元素。

    class ZeroPunctuationIndexPage extends Page {
        static url = "http://www.escapistmagazine.com/videos/view/zero-punctuation"
        static at = {title == "Zero Punctuation Video Gallery | The Escapist"}
        static content = {
    
            selectFirstVideo {$("div.filmstrip_video").find('a')}
            firstVideoTitle {$("div.filmstrip_video").find('i')}
    
        }
    }
    
    
    class ZeroPunctuationIndexSpec extends GebReportingSpec {
    
            def "Click the latest video and play it"(){
                given:
                to ZeroPunctuationIndexPage
    
                when:
                waitFor { selectFirstVideo.click() }
    
                then:
                title.endsWith(firstVideoTitle.text())
    
            }
    }
    

    干杯!

    【讨论】:

    • 非常感谢!现在更有意义了。
    【解决方案2】:

    试试:

    title.endsWith(firstVideoTitle.text())
    

    我在我的 iPad 上,所以我无法测试它,但它应该可以工作。

    【讨论】:

    • 代码在 selectFirstVideo {$("a", 0, class: "filmstrip_video")} 失败,所以它根本没有进入标题部分:/
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多