尝试加载 yaml 文件,遍历哈希并执行一些逻辑

Trying to load a yaml file, iterate through the hash and execute some logic

正在尝试加载 yaml 文件、遍历哈希并执行一些逻辑。我正在使用 Watir-Webdriver/Ruby 来回答随机选择的问题,questions/answers 是已知的并存储在 YAML 文件中 (kba.yml)。

我正在使用 Ruby/Page Objects/YAML file/Watir-Webdriver.

我的 YAML 文件(kba.yml):

1:
  question: "Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'."
  answer: "Google"

2:  
  question: "According to our records, you graduated from which of the following High Schools?"
  answer: "NONE OF THE ABOVE/DOES NOT APPLY"

3:  
  question: "I was born within a year or on the year of the date below."
  answer_1: "1942"
  answer_2: "1941"
  answer_3: "1943"

我加载 YAML 文件的位置(在我的 env.rb 中):

require 'yaml'
KBA = YAML.load_file('config/data/kba.yml')

我的代码从 yaml 文件迭代散列并执行一些逻辑:

循环第一个随机问题:

KBA.each do |key, value| 
  value.each do |question, answer|
    puts "blah question: #{question}"
      if (@browser.h2s[0].text.sub (/^\d.\s/), '') == question
        puts "Question matched and selected: #{question}"
        case answer
        when @browser.labels[0].text
          select_first_q_first_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[1].text
          select_first_q_second_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[2].text
          select_first_q_third_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[3].text
          select_first_q_fourth_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[4].text
          select_first_q_fifth_radio 
          puts "Answer matched and selected #{answer}"
        else 
          puts "The answer is not present for this question!"
        end
      else
        puts "This question is not stored in the dataset: #{@browser.h2s[0].text}"
      end
    end
end

循环第二个随机问题:

KBA.each do |question, answer| 
    if (@browser.h2s[1].text.sub (/^\d.\s/), '') == question
      puts "Question matched and selected: #{question}"
      case answer
      when @browser.labels[0].text
        select_second_q_first_radio
        puts "Answer matched and selected #{answer}"
      when @browser.labels[1].text
        select_second_q_second_radio
        puts "Answer matched and selected #{answer}"
      when @browser.labels[2].text
        select_second_q_third_radio
        puts "Answer matched and selected #{answer}"
      when @browser.labels[3].text
        select_second_q_fourth_radio
        puts "Answer matched and selected #{answer}"
      when @browser.labels[4].text
        select_second_q_fifth_radio 
        puts "Answer matched and selected #{answer}"
      else 
        puts "The answer is not present for this question!"
      end
    else
      puts "This question is not stored in the dataset: #{@browser.h2s[1].text}"
    end  
  end

循环第三个随机问题:

  KBA.each do |question, answer| 
      if (@browser.h2s[2].text.sub (/^\d.\s/), '') == question
        puts "Question matched and selected: #{question}"
        case answer
        when @browser.labels[0].text
          select_third_q_first_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[1].text
          select_third_q_second_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[2].text
          select_third_q_third_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[3].text
          select_third_q_fourth_radio
          puts "Answer matched and selected #{answer}"
        when @browser.labels[4].text
          select_third_q_fifth_radio 
          puts "Answer matched and selected #{answer}"
        else 
          puts "The answer is not present for this question!"
        end
      else
        puts "This question is not stored in the dataset: #{@browser.h2s[2].text}"
      end 
    end
end

从输出来看,我的代码似乎并没有真正检查问题和答案的正确值,我该怎么做?

这是输出:

blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: question
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer_1
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer_2
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
blah question: answer_3
This question is not stored in the dataset: 1. Which of the following is a current or previous employer? If there is not a matched employer name, please select 'NONE OF THE ABOVE'.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 2. I was born within a year or on the year of the date below.
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?
This question is not stored in the dataset: 3. Which of the following represents the last four digits of your primary checking account number?

更新:页面的页面对象有 3 个问题:

class LOA2KBAQuestions

  include PageObject
  include ErrorMessages
  include DataMagic

  h2(:question, :class => "question")

  #radio buttons for the answers for the first question
  radio_button(:first_q_first_radio, :id => "answers_question_0_1")
  radio_button(:first_q_second_radio, :id => "answers_question_0_2")
  radio_button(:first_q_third_radio, :id => "answers_question_0_3")
  radio_button(:first_q_fourth_radio, :id => "answers_question_0_4")
  radio_button(:first_q_fifth_radio, :id => "answers_question_0_5")

  #radio buttons for the answers for the second question
  radio_button(:second_q_first_radio, :id => "answers_question_1_1")
  radio_button(:second_q_second_radio, :id => "answers_question_1_2")
  radio_button(:second_q_third_radio, :id => "answers_question_1_3")
  radio_button(:second_q_fourth_radio, :id => "answers_question_1_4")
  radio_button(:second_q_fifth_radio, :id => "answers_question_1_5")

  #radio buttons for the answers for the third question
  radio_button(:third_q_first_radio, :id => "answers_question_2_1")
  radio_button(:third_q_second_radio, :id => "answers_question_2_2")
  radio_button(:third_q_third_radio, :id => "answers_question_2_3")
  radio_button(:third_q_fourth_radio, :id => "answers_question_2_4")
  radio_button(:third_q_fifth_radio, :id => "answers_question_2_5")

  button(:submit, :text => "Submit Answers")
end

我也像这样访问实际问题(它们都在 h2 标签中,我使用正则表达式删除问题前的 #。我通过在 h2 标签后添加 [x] 来访问问题):

@browser.h2s[0].text.sub (/^\d.\s/), ''

所以我最终没有为 h2 标签使用声明的页面对象,因为我不确定如何使用它来访问 3 个问题。

更新 - 示例 HTML 的 3 个问题页面:

  <div class="question">

    <h2>


        1.
        Which of the following is a current or previou…

    </h2>
    <div class="answers">
        <p>
            <label>
                <input id="answers_question_0_1" type="radio" value="1" name="answers[question_0]" checked="checked"></input>


                RITEWAY

            </label>
        </p>
        <p>
            <label>
                <input id="answers_question_0_2" type="radio" value="2" name="answers[question_0]"></input>


                NEW ALTERNATIVES

            </label>
        </p>
        <p></p>
        <p></p>
        <p></p>
    </div>

</div>
<div class="question">

    <h2>


        2.
        I was born within a year or on the year of the…

    </h2>
    <div class="answers">
        <p>
            <label>
                <input id="answers_question_1_1" type="radio" value="1" name="answers[question_1]" checked="checked"></input>


                1936

            </label>
        </p>
        <p>
            <label>
                <input id="answers_question_1_2" type="radio" value="2" name="answers[question_1]"></input>


                1939

            </label>
        </p>
        <p>
            <label>
                <input id="answers_question_1_3" type="radio" value="3" name="answers[question_1]"></input>


                1942

            </label>
        </p>
        <p></p>
        <p></p>
    </div>

</div>

问题在于 YAML 文件的循环方式。例如,在:

KBA.each do |key, value|
  value.each do |question, answer|
  end
end

value 是一个散列,因此 questionanswer 不会是预期值。它们实际上是 key/value 对。应该是:

KBA.each do |key, value|
  question = value['question']
  answer = value['answer']
end

我不会尝试遍历每个 question/answer YAML 文件,而是查找具体的问题和答案。在以下页面对象中,answer_questions 将:

  1. 根据h2元素判断题目是什么
  2. 查看 YAML 文件以找到问题的所有有效答案。
  3. 查找并 select 与有效答案之一匹配的单选按钮。

示例页面对象:

class MyPage
  include PageObject

  divs(:question, :class => 'question')

  def answer_questions
    question_elements.each do |question|
      # Determine what the question is
      question_text = question.h2_element.text.sub(/^\d.\s/, '')

      # Get the valid answers for the question
      question_data = KBA.find { |_, value| value['question'] == question_text }
      unless question_data
        puts "Question not found: #{question_text}"
        next
      end
      valid_answers = question_data[1].select { |k, _| k =~ /answer/ }.values

      # Set the radio buttons if they match one of the answers
      answer = question.radio_button_elements.find do |radio|
        label = radio.parent.text
        valid_answers.include?(label)
      end
      unless answer
        puts "Unable to answer question: #{question_text}"
        next
      end
      answer.select
    end
  end
end