我正在尝试利用空手道中的黄瓜功能将 Excel / csv sheet 作为示例部分中的文件传递

I am trying to utilise cucumber features in karate for passing the Excel / csv sheet as a file in Examples Section

我正在尝试在功能文件的 "Scenario Outline" 部分下使用 "Examples"。

在功能文件中,我尝试用以下格式表示它。这会按预期工作吗?

Feature: save data to db
Background: no definition
Scenario Outline: to validate file is getting read by example section

Given url 'https://www.googleapis.com/geolocation/v1/geolocate'
And param key = 'AIzaSyB2jt4BQ9McqBXAe8dYcp1CwKf0oGFlWuc'
And def dogs = read('classpath:external_table/json.feature')
And request { "homeMobileCountryCode": '<homeMobileCountryCode>' 
,"homeMobileNetworkCode": '<homeMobileNetworkCode>' } dogs
When method post
And print response
Then status 200

Examples: {'datafile':'src/test/java/external_table/testdata.xlsx'}

首先语法错误。示例 table 应该从下一行开始,而不是与单词 'Examples'.

在同一行

其次,示例table中没有header,因此在场景步骤中将其用作占位符。

第三个是占位符'',它看起来像一个,在示例中没有的场景步骤中定义table。

我对空手道不熟悉,无法正确修改场景大纲。请参阅此 link 以了解这一点 -- http://www.baeldung.com/cucumber-scenario-outline

不,它不会按预期工作。正如其他人已经回答的那样,语法错误。

我不明白为什么你在这里回答了这个问题还要再问:

也许你们的领导坚持要使用 Excel。根据经验,让我这样说,使用 Cucumber 本机示例或空手道的 data-driven 功能要简单得多,您可以在 JSON 数组上循环,请参考:https://github.com/intuit/karate#the-karate-way

如果你仍然坚持要使用Excel并且你不愿意写一个小的Java实用程序来将Excel(或CSV)转换为JSON,那么请放弃使用空手道,它不是适合你的工具。祝一切顺利。

如果您真的必须处理使用 CSV 或 Excel 中的示例来驱动场景,那么这里是实现此操作的方法。

  1. 为您的示例命名,最好使用企业理解的名称。

  2. 使用该名称编写一个场景 - 不要使用场景大纲

  3. 获取一个步骤完成获取Excel和示例的所有工作,使用名称来引导步骤获取正确的示例。

让我们通过一个简单的英超联赛球队列表来了解一下,并假设您必须从 Excel 电子表格

中动态获取这些信息
Scenario: Commpile statistics for premier league teams
  Given I am working with the premier league
  When I compile teams statistics
  Then I should see team statistics
  And there should be no errors

为了完成这项工作,我们必须将所有细节从场景下推到步骤定义,理想情况下是这些步骤定义的辅助方法。所以我们最终得到类似

的东西
Given "I am working with the premier league" do
  @teams = load_premier_league_teams
end

然后是辅助方法

def load_premier_league_teams
  get_the_teams_from_the_spreadsheet
end

现在所有关于处理电子表格的事情都在 Cucumber 之外和您的编程语言中处理。

然后在 When 步骤中,您可以对每个团队进行如下操作

When "I compile the teams statistics" do
  @results = compile_statistics(@teams)
end

并编写另一个辅助方法

def compile_statistics(teams)
  results = []
  teams.each do |t|
    results << compile_team_stats(t)
  ...
end

我们再次将有关我们如何做事的所有细节从 Cucumber 中推出。

您可以对 'There should be no errors' 步骤执行相同的操作,使用 @results 变量遍历结果并收集所有错误。

Cucumber 不是一个用来编程的工具。使用此技术,您仍然可以使用它来 运行 任何您想要的东西,同时保持您的场景简单且非常快速。