rubycapybararubyxl

trying to increment a value of the object by assigning to a variable i . Please advice


Below is my code.I'm trying to input value from the excel sheet to the text boxes in the page. Eg : In text box id is 'allocationRegContrib[17].newFundValue' I want to input a value say 20. Similarly for another text box whose id is 'allocationRegContrib[18].newFundValue', I want to input a value say 40. How to achieve. Similarly the id goes on upto 60. But I do not want to input in all the textboxes. So I'm trying to use like fill_in "allocationRegContrib[i].newFundValue".

     @i=17
    for j in (workbook.first_row..workbook.last_row)

     for k in (workbook.first_column..workbook.last_column)

      if(k==1)
    fill_in "searchInput", :with => workbook.cell(j,1)
    find(:xpath, '//*[@id="sdcaLink"]').click
    sleep 3


    else
    choose("sdcaOption", :option => "YES")
    select(workbook.cell(j,k), :from => 'sdcaDuration')
    fill_in "allocationRegContrib[i].newFundValue", :with => workbook.cell(j,k)
    @i=i+1
    find(:xpath, '//*[@id="specialDCAupdate"]').click

But it does not work for me. Error is "unable to locate the capybara element allocationRegContrib[i].newFundValue". Please Advice


Solution

  • You may have two issues:

    i and @i aren't the same thing. I think you may want to use i in all the places. i is a local variable (that is, a simple plain old variable). @i is an instance variable of a class, like a "field" or "property" in other languages.

    fill_in "allocationRegContrib[i].newFundValue", :with => workbook.cell(j,k)
    

    should probably be:

    fill_in "allocationRegContrib[#{i}].newFundValue", :with => workbook.cell(j,k)
    

    #{i} puts in the value of the variable i into the string, then the capybara matcher can find, for example, element allocationRegContrib[17].newFundValue (when i=17)