从场景大纲

时间:2018-03-22 16:30:28

标签: ruby selenium cucumber

我有以下步骤大纲,我已经开始使用并按以下格式构建:

步骤定义

@web1
Scenario Outline: A teacher filters the report
Given I am signed in as a teacher
And I navigate to the reports page
Then I click on the School Overview report
And I select an <option> from the dropdown

Examples:
    |option| 
    |teacher|
    |subject|
    |class|
    |year|

我想要实现的目标

对于每个选项,我想要选择一个不同的下拉选项(我已经在步骤定义中定义的逻辑),这样我就可以将它们组合在一起,而不是为类似的操作创建多个场景。 。

以前使用相同的步骤定义,我有多个场景做同样的事情 - 打开一个接一个的下拉列表。现在,但是我不确定如何为此设置步骤定义文件..这是我尝试过的:

And(/^I select an <option> from the dropdown$/) do |table|
    table.raw.each do |report_filter_types|

        if report_filter_types.eql?("teacher")
             # Making the teacher selection from the dropdown by selecting the first option
           # @wait.until {@driver.find_element(:css => '.container-fluid > .filter-bar-element:nth-of-type(1) > .ember-view > .ember-view.ember-basic-dropdown-trigger > span:nth-of-type(1)').click}
            #@wait.until {@driver.current_url.include?('teacher=')}
        elsif report_filter_types.eql?("subject")
            # Making the subject selection from the dropdown by selecting the first option
            @wait.until {@driver.find_element(:css => '.container-fluid > .filter-bar-element:nth-of-type(2)').click} 
            @wait.until {@driver.find_element(:css => '.ember-view.ember-basic-dropdown-trigger > span:nth-of-type(1)').click}
            @wait.until {@driver.find_element(:css => '.ember-basic-dropdown-content > .ember-view > li:nth-of-type(1)').click}
        elsif report_filter_types.eql?("class")
            # Making the classes selection from the dropdown by expanding it and then choosing the first option
            @wait.until {@driver.find_element(:css => '.container-fluid > .filter-bar-element:nth-of-type(3) > .ember-view > .ember-view.ember-basic-dropdown-trigger > span:nth-of-type(1)').click}
            @wait.until {@driver.find_element(:css => 'a.ember-view.grouped-classes-options').click}
            @wait.until {@driver.find_element(:css => '.ember-view > li:nth-of-type(1) > .ember-view > li:nth-of-type(1) > .ember-view > li').click}
        elsif report_filter_types.eql?("year")
            # Making a year selection from the dropdown by expanding it and choosing the first option
            @wait.until {@driver.find_element(:css => '.last > span:nth-of-type(1)').click}
            @wait.until {@driver.find_element(:css => '.ember-basic-dropdown-content > .ember-view > li:nth-of-type(1)').click}
        end
        puts "Report filtered by #{report_filter_types}"
    end
end

它所说的部分report_filter_types就是我定义了在应用程序中选择每个“个人”下拉列表时需要执行的操作的地方。不知道我怎么能把它连接到桌子上!

上面的代码是以我打开每个下拉列表的单独“方案”的格式设置的,但是我想利用上面的现有代码,调整它并使用表格格式使其更整洁,更容易维护此功能。

1 个答案:

答案 0 :(得分:1)

保持简单而不是写

Background:
  Given I am signed in as a teacher
  And I navigate to the reports page
  And I click on the School Overview report

Scenario: Filter on option
  When I filter on option

When "I filter on option" do
  ...
end

为每个选项编写一个场景

您会发现最终的代码更少,步骤定义更简单。

另请注意,您的示例在Then之后没有When。该方案的要点当然是要对过滤选项后应该发生的事情进行预期。

一般情况下,避免情景大纲,你可以简单而强大地使用Cuke而不使用它们。