【问题标题】:Setting up a Step Definition in Ruby from a Scenario Outline根据场景大纲在 Ruby 中设置步骤定义
【发布时间】:2018-09-01 04:43:07
【问题描述】:

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

步骤定义

@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 的部分只是我定义了在应用程序中选择每个“个人”下拉菜单所需执行的操作。不知道我怎么能把它连接到桌子上!

上面的代码是以我将有单独的“场景”来打开每个下拉列表的格式设置的,但是我想利用上面的现有代码,对其进行调整并使用表格格式使其更整洁,更易于维护这项特征。

【问题讨论】:

    标签: ruby selenium cucumber


    【解决方案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
    

    为每个选项写一个场景

    您会发现代码更少,步骤定义也简单得多。

    另请注意,您的示例在When 之后没有Then。该场景的重点肯定是写出对选项进行过滤后会发生什么的预期。

    通常避免使用场景大纲,您可以简单且强大地进行 Cuke,而无需使用它们。

    【讨论】:

    • 谢谢@diabolist!是的,我想我会避免使用大纲,因为它会让我更加头疼!最适合用于更简单的事情!
    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    相关资源
    最近更新 更多