1

At the moment I have:

def addInfoToStory(idOfStory, *stories)

i = -1
numOfInputs = 9
  while i < numOfInputs
  stories.each.with_index do |story|
  $log.puts "i="+"#{i}"
  $log.puts story[i]
  $log.puts "i2="+"#{i}"
  @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: i+1).div.double_click
    if @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1").text_field(:id, "input").set(story[i])
       sleep 2
       @browser.send_keys(:tab)
    else @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1").select_list(:id, "select").set(story[i])
       sleep 2
       @browser.send_keys(:tab)
    end
  end 
 end
end

The majority of information are textarea boxes which work fine, but when I get to a dropdown, the value won't change. Any ideas why?

samayres1992
  • 779
  • 2
  • 12
  • 30
  • What is your if statement attempting to do? In its current state, it would always try to set the text field and presumably throw an exception when there is a dropdown instead of a text field. – Justin Ko Sep 18 '12 at 15:09
  • @JustinKo I'm trying to fill out a table of inputs with user inputted strings. For the most part it's just input text areas but for a couple of sections in the table they are drop down boxes, I was trying to do it all within the loop. Any ideas? – samayres1992 Sep 18 '12 at 15:46

2 Answers2

3

The easiest solution would be to have the if statement check if there is a text field or a select list present.

div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
    div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
    div_container.select_list(:id, "select").select(story[i])
end
sleep 2
@browser.send_keys(:tab)

Also note that for select_lists, it has to be .select instead of .set.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • is there any reason why you've set it as id: and class: instead of the normal way :id, :class? I can't seem to get it to work – samayres1992 Sep 19 '12 at 10:20
  • 1
    Take a look at this: http://stackoverflow.com/questions/4563766/hash-syntax-in-ruby – Željko Filipin Sep 19 '12 at 12:05
  • There is no reason for the mixture. The `:id` is due to habit. The `id:` is because that part was copied from what you had. Should not impact whether the code works (assuming you are using Ruby 1.9). So when you say it does not work, can you clarify what is not working? – Justin Ko Sep 19 '12 at 13:04
  • @JustinKo, seems to not be doing the original functions that I already had working before using what you suggested. I used to process through most of the script then stop on the dropdown but now it won't process through the normal inputs either. I've been playing around with it and this is what I have now, http://jsfiddle.net/bKKKa/ (I know its not js but pastey seems to have been deleted sadly haha) Any ideas? I was getting errors by using the local variable so I just used *stories instead, is that fine or should I use it back as so in the original? – samayres1992 Sep 19 '12 at 14:06
  • Your new script has quite a few differences compared to your original; for example you are no longer doing the double-click and the withinDiv locator is also different. I think you should only be changing a small bit at a time (ie just change the if statement). Once you know that works, you can worry about refactoring. – Justin Ko Sep 19 '12 at 14:27
  • @JustinKo ah seems playing about with it, made it worse. I fixed it back to how it used to be, but it seems that each input is having every array entered that is listed? http://jsfiddle.net/bKKKa/1/ – samayres1992 Sep 19 '12 at 14:36
  • That is because you have a two loops - the `while` and the `stories.each_with_index`. Based on how you are using the `while` loop, how come you do not just use the index value provided by the `each_with_index`? – Justin Ko Sep 19 '12 at 14:42
  • @JustinKo, it's probably down to me not being able to pick up functionality very quickly. I had never worked with ruby prior to a few weeks ago, so I'm just trying my best to learn it the best I can. The reason why was because I thought it was supposed to loop but it didn't ever seem to work and I was unsure whether it was required at the time so I used a while loop and that seemed to get the process half way down before the dropdown. I removed the while loop, but the process doesn't seem to loop now and it still enters all the arrays. http://jsfiddle.net/bKKKa/2/ , is that any better? – samayres1992 Sep 19 '12 at 14:51
  • Try `stories.each.with_index do |story, i|` – Justin Ko Sep 19 '12 at 15:05
  • Seems to have the same outcome as previous although that does look correct. In the terminal I get a command processor error from google (assumedly because it's being run on chrome) saying "window not found, the window may have been closed". Which is rather weird considering I'm working in the same window, but yes the browser double clicks on the first input, enters in all the arrays in the same field then closes the process. – samayres1992 Sep 19 '12 at 15:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16885/discussion-between-samayres1992-and-justin-ko) – samayres1992 Sep 19 '12 at 15:19
0

Required

storyFields.each_index {|i|
div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index:     (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
   div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
       div_container.select_list(:id, "select").select(story[i])
end
}

Previous answer worked to some extent but didn't loop correctly.

samayres1992
  • 779
  • 2
  • 12
  • 30