【问题标题】:Re-using tests among Geb Specs在 Geb Specs 中重用测试
【发布时间】:2015-09-21 20:51:26
【问题描述】:

我正在尝试重复使用我在另一个 Geb Spec 中编写的 Geb Spec 测试,因此我不需要重新编写代码。我总是需要不同页面中的产品编号,所以我想做类似以下的事情;

class BasePageGebSpec extends GebReportingSpec {
     def firstProductOnBrowsePage(){
        when:
        to BrowsePage
        then:
        waitFor { BrowsePage }
        productId { $("meta", 0, itemprop: "mpn").@content }
        return productID // ???
    } 
}

在另一个 GebSpec 中,我希望使用上面的 firstProductOnBrowsePage,如下所示:

 class ProductDetailsPageGebSpec extends BasePageGebSpec {
     def "See first products details page"(){
        when:
        to ProductDetailsPage, productId: firstProductOnBrowsePage()

       then:
       waitFor { $("h2", class:"title").size() != 0 }
       assert true
    }
}

任何帮助将不胜感激,

谢谢!

【问题讨论】:

    标签: groovy spock geb


    【解决方案1】:

    使用traits,你几乎可以得到你想要的(但是测试在一个特征中不起作用)。您还可以考虑创建一个规范类来测试您拥有的每个页面的产品编号,然后不必担心在每个单独页面的规范类中测试此功能。

    trait BasePageGebSpec extends GebReportingSpec {
     def testingFirstBrowse() {
        waitFor { BrowsePage }
        productId { $("meta", 0, itemprop: "mpn").@content }
        return productID
     }
    }
    
     class ProductDetailsPageGebSpec implements BasePageGebSpec {
        def firstProductOnBrowsePage(){
            when:
                to BrowsePage
            then:
                testingFirstBrowse()
        } 
    }
    

    【讨论】:

    • 我不明白这个例子。 GebReportingSpec 是一个类,一个特征只能扩展其他特征,所以如果我像你一样定义一个特征,我只会遇到编译问题。
    • 你是对的。我应该使用“实现”而不是“扩展”;它假设 GebReportingSpec 是一个特征。最重要的一点是 Spock 测试在 trait 中不起作用。充其量您可以使用一些通用测试代码将方法外部化,但您仍然需要在每个单独的 Spec 类中对其进行测试。
    【解决方案2】:

    BrowsePage上添加productId作为内容:

    class BrowsePage extends Page {
      static content = {
        productId { $("meta", 0, itemprop: "mpn").@content }
      }
    }
    

    然后在你的规范中使用它:

    class ProductDetailsPageGebSpec extends BasePageGebSpec {
      def "See first products details page"(){
        when:
        to ProductDetailsPage, productId: to(BrowsePage).productId
    
        then:
        waitFor { $("h2", class:"title").size() != 0 }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      相关资源
      最近更新 更多