0

yaml 파일을로드하고 해시를 반복하고 일부 논리를 실행하려고합니다. 무작위로 선택된 질문에 Watir-Webdriver/Ruby를 사용하여 질문/대답을 알리고 YAML 파일 (kba.yml)에 저장합니다.yaml 파일을로드하고 해시를 반복하며 일부 논리를 실행하려고 시도합니다.

Ruby/페이지 개체/YAML 파일/Watir-Webdriver를 사용하고 있습니다. 나는 (내 env.rb 내부) 내 YAML 파일을로드

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 파일 (kba.yml)

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 
012 : 1 랜덤 질문에 대한

루프 : 일부 로직을 수행 제 2 랜덤 질문에 대한 3,516,

루프 :

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 

출력에서 ​​그것의 내 코드가 실제로 질의 응답의 적절한 값에 대해 확인되지 않은 좋아하는 것 : 세 번째 무작위 질문에 대한

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 

루프 내가 어떻게 그럴 수 있니? 나는 그들이 H2 태그에 모두 (또한

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 

내가 이런 실제 질문에 액세스 :

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? 

UPDATE : 페이지의 페이지 개체 3 개 질문

는 출력 hge 태그 다음에 [x]를 추가하여 정규 표현식을 사용하여 질문에 액세스하십시오.

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

그래서 h2 태그에 대해 선언 된 페이지 객체를 사용하지 않으므로 3 개의 질문에 액세스하는 방법을 잘 모르기 때문에 끝납니다.

UPDATE - 3 개 질문 페이지의 샘플 HTML :

<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> 
+1

그것은 도움이 될 수 있습니다. 예를 들어 첫 번째 질문, 두 번째 질문 및 세 번째 질문의 루프 사이에 어떤 차이가 있어야하는지는 분명하지 않습니다. 궁극적으로 해시에서 특정 값을 가져 오는 방법을 찾고 있다고 생각합니다. 예를 들어, 첫 번째 루프의'question'은 해시 키가 될 것입니다 - 예 : "question". 실제 질문을 원하면 'value ['question ']'이어야합니다. –

+0

응용 프로그램에서 생성 된 임의의 질문이 3 개 있습니다.이 임의의 잠재적 인 질문과 대답이 들어있는 데이터 yaml 파일 (kba.yml)이 있습니다. 그래서 각 질문에 대한 루프가 있습니다 (따라서 3 루프). 각 루프는 임의로 생성 된 질문을 내 yaml 파일과 비교합니다. – Farooq

+1

나는 본다. 루핑은 좀 더 복잡한 접근법처럼 보입니다. 질문을 받고 YAML 파일에서 특정 답변을 찾는 것이 더 쉬울까요? 어느 쪽이든, 당신은 페이지를 제공 할 수 있습니까? (즉, 3 개의 질문 중 HTML)? –

답변

3

문제는 YAML 파일이 반복되고있는 상황이다. 예를 들어,에서 다음 questionanswer이 예상 값되지 않을 것 때문에

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

value는, 해시입니다. 실제로는 키/값 쌍입니다. 그것은 있었어야 :

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

대신 각 질문을 통해 interate 노력의 /를 YAML 파일을 대답, 내가 룩업 할 구체적인 질문을. 다음 페이지 객체에서 answer_questions은 :

  1. h2 요소를 기반으로하는 질문을 결정합니다.
  2. YAML 파일을보고 질문에 대한 모든 유효한 대답을 찾으십시오.
  3. 유효한 대답 중 하나와 일치하는 라디오 버튼을 찾아서 선택하십시오.

예 페이지 개체 : 당신은 당신의 코드가 구현하려고하는 논리를 설명 할 수있는 경우

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 
+0

Justin, 귀하의 솔루션은 3 가지 질문 모두에 대한 대답이 동일한 답변 일 수 있다는 점을 고려합니까? "위의 어느 부분도/적용되지 않습니다"및 dob/year 문제입니까? – Farooq

+1

'valid_answers' 배열은 dob/year 질문 (또는 여러 개의 가능한 답이있는 질문)을 설명합니다. 라디오 버튼의 위치는 특정 질문 div에 국한되어 있으므로 동일한 대답으로 여러 질문을 처리해야합니다. –

+0

더 잘 이해할 수있는 답변이 몇 가지 더 있습니다. 1) # 질문 부분에 대한 올바른 대답을 얻는 방법에 대해 간단하게 설명 할 수 있습니까? 나는 첫 줄의 [1] 부분을 확신하지 못했습니다. 2) 라디오 버튼이 정확히 선택되는 곳 (PageObjects에서 '선택'메소드가 표시되지 않음)에서 라디오 버튼 코드에서 해당 부분을 찾을 수 없습니까? – Farooq