Thursday, November 6, 2008

To modify the DOM of a webpage @runtime with Ruby & Watir you can execute JavaScript like so:

  • spoofValue = 'bogus'
  • @ie = Watir::IE.new
  • @ie.goto('http://www.ruby.org/')
  • ieWindow = @ie.document.parentWindow
  • ieWindow.execScript("var newOption = document.createElement('option')")
  • ieWindow.execScript("newOption.text = 'Spoofed'")
  • ieWindow.execScript("newOption.value = '" + spoofValue + "'")
  • ieWindow.execScript("var selectControl = document.getElementById('666')")
  • ieWindow.execScript("selectControl.add(newOption)")

and then you can select the new bogus option like so:

  • @ie.select_list(:id, '666').select_value(spoofValue )

No comments: