0

我有一个测试规范,可以使用唯一的数据集运行。对此的最佳实践有点不清楚。应该如何修改下面的代码以运行:

@Stepwise
class marktest extends ShopBootStrap  {

   private boolean useProductionUrl = false

   def "Can Access Shop DevLogin page"() {
       // DevStartLogin: 'New OE Start' button click
       setup:
           println System.getProperty("webdriver.chrome.driver")
       when:
           to ShopDevStartPage
       then:
           at ShopDevStartPage
   }

   def "on start enrollment, select 'United States' and click 'continue' button"() {
       when: "enter Sponsor ID and click New OE Start"
           to ShopDevStartPage
           sponsorId.value(ShopDevStartPage.SPONSORID)
           NewOEButton.click()
       then:
           waitFor { NewEnrollmentPage }
   }
}

1)数据集1

private boolean useProductionUrl = false
protocol = System.getProperty("protocol") ?: "https"
baseDomain = System.getProperty("base.url") ?: "beta.com"
testPassword = System.getProperty("test.password") ?: "dontyouwish"

2)数据集2

private boolean useProductionUrl = true
protocol = System.getProperty("protocol") ?: "https"
baseDomain = System.getProperty("base.url") ?: "production.com"
testPassword = System.getProperty("test.password") ?: "dywyk"
4

1 回答 1

0

通常,要使测试依赖于数据,请使用where块,可能与@Unroll注释一起使用。

但是,您的案例根本不是数据驱动测试的最佳示例。and应该设置在 中,类似于您提供的片段
。请参阅Geb 中的这一部分,因为这就是您正在使用的。baseDomainprotocolGebConfig.groovy

简单示例(在 GebConfig.groovy 中):

environments {
  production {
    baseUrl = "https://production.com"
  }
  beta {
    baseUrl = "https://beta.com"
  }
}

如果这样做,您的个人测试不需要关心环境,因为这已经内置在 Geb 中。例如,当导航到页面时,它们的基本 URL 会自动设置。您没有在示例中提供那部分代码(如何定义页面),因此我无法直接帮助您。

现在,在您的情况下,就“密码”而言,您可以从环境变量或系统属性中读取它,您将其设置为接近您配置 Geb 的位置geb.envgeb.build.baseUrl系统属性。
请注意,我只是出于实际原因考虑这一点,而不考虑密码的保密性。

您将在使用它的页面类中获取该变量。
页面类中的示例代码:

static content = {
    //...
    passwordInput = { $('input[type="password"]') }
    //...
}

void enterPassword() {
    passwordInput.value(System.getProperty('test.password'))
}

要完成这项工作,您需要在系统属性设置为正确值的情况下开始测试。

例如,如果直接从命令行开始,您将添加参数-Dgeb.env=beta -Dtest.password=dontyouwish。如果从 Gradle 任务运行,则需要systemProperty向该任务添加适当的键和值。如果从 IDE 运行,请参阅您的 IDE 文档,了解如何在运行程序时设置 Java 系统属性。

于 2019-02-26T01:16:10.643 回答