Skip to content

Commit

Permalink
tweaks to bring in to line with capy master
Browse files Browse the repository at this point in the history
  • Loading branch information
smparkes committed Jul 18, 2010
1 parent 0e06daf commit 077ebab
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions lib/capybara/driver/envjs_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def timeout *args
end

class Capybara::Driver::Envjs < Capybara::Driver::Base
class Node < Capybara::Node
class Node < Capybara::Driver::Node
def text
node.innerText
native.innerText
end

def [](name)
value = if name.to_sym == :class
node.className
native.className
else
node.getAttribute(name.to_s)
native.getAttribute(name.to_s)
end
return value if value and not value.to_s.empty?
end
Expand All @@ -41,57 +41,55 @@ def [](name)
attr_name == "class" and attr_name = "className"
case
when 'select' == tag_name && 'value' == attr_name
if node['multiple']
all_unfiltered(".//option[@selected='selected']").map { |option| option.node.innerText }
if native['multiple']
all_unfiltered(".//option[@selected='selected']").map { |option| option.native.innerText }
else
node.value
native.value
end
# when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
# node[attr_name] == 'checked' ? true : false
else
node[attr_name]
native[attr_name]
end
end

def value
if tag_name == 'textarea'
node.innerText
native.innerText
else
super
native.getAttribute("value")
end
end

def set(value)
case node.tagName
case native.tagName
when "TEXTAREA"
node.innerText = value
native.innerText = value

This comment has been minimized.

Copy link
@bcardarella

bcardarella Nov 11, 2010

Contributor

Why is this setting 'innerText' for a textarea instead of setting 'value' ?

This comment has been minimized.

Copy link
@smparkes

smparkes Nov 12, 2010

Author Owner

Because the contents of textarea element is actually the contents of the tag, rather than the value. It's not an input (as far as I can see ...)

This comment has been minimized.

Copy link
@bcardarella

bcardarella Nov 12, 2010

Contributor

Maybe it is a Webkit thing but it actually sets the value of the textarea element when you enter text instead of setting innerText.

This comment has been minimized.

Copy link
@smparkes

smparkes Nov 12, 2010

Author Owner

I don't think it's a webkit thing. I think it's just an incompleteness in env.js. It looks like textareas are special and are supposed to make the value attribute work effectively the same as innerText. So this should be handled in env.js rather than in the capy driver. I didn't delve into it that deeply when I made the patch.

This comment has been minimized.

Copy link
@smparkes

smparkes Nov 12, 2010

Author Owner

You can open an issue on env-js (my fork).

Not too hard to fix, but I'm not sure when I'll get to it. A test case would be nice.

Do you know if innerText/textContent returns the value under Webkit?

This comment has been minimized.

Copy link
@bcardarella

bcardarella Nov 13, 2010

Contributor

innerText and textContent in WebKit both return empty strings when the text area has content.

This comment has been minimized.

Copy link
@smparkes

smparkes Nov 13, 2010

Author Owner

Yeah, doesn't look like webkit implements textContent on anything, but it does implement innerText on some things, so I'll have to see what the spec says about innerText on textareas.

Thanks for the help.

else
case node.getAttribute("type")
case native.getAttribute("type")
when "checkbox", "radio"
node.click if node.checked != value
else; node.setAttribute("value",value)
native.click if native.checked != value
else; native.setAttribute("value",value)
end
end
end

def select(option)
escaped = Capybara::XPath.escape(option)
option_node = all_unfiltered(".//option[text()=#{escaped}]").first || all_unfiltered(".//option[contains(.,#{escaped})]").first
option_node.node.selected = true
option_node.native.selected = true
rescue Exception => e
options = all_unfiltered(".//option").map { |o| "'#{o.text}'" }.join(', ')
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
end

def unselect(option)
if !node['multiple']
if !native['multiple']
raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
end

begin
escaped = Capybara::XPath.escape(option)
option_node = (all_unfiltered(".//option[text()=#{escaped}]") || all_unfiltered(".//option[contains(.,#{escaped})]")).first
option_node.node.selected = false
option_node.native.selected = false
rescue Exception => e
options = all_unfiltered(".//option").map { |o| "'#{o.text}'" }.join(', ')
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
Expand All @@ -109,7 +107,7 @@ def drag_to(element)
end

def tag_name
node.tagName.downcase
native.tagName.downcase
end

def visible?
Expand All @@ -120,7 +118,7 @@ def all_unfiltered selector
window = @driver.browser["window"]
null = @driver.browser["null"]
type = window["XPathResult"]["ANY_TYPE"]
result_set = window.document.evaluate selector, node, null, type, null
result_set = window.document.evaluate selector, native, null, type, null
nodes = []
while n = result_set.iterateNext()
nodes << Node.new(@driver, n)
Expand All @@ -138,7 +136,7 @@ def trigger event
def _event(target,cls,type,bubbles,cancelable)
e = @driver.browser["document"].createEvent(cls);
e.initEvent(type,bubbles,cancelable);
target.node.dispatchEvent(e);
target.native.dispatchEvent(e);
end

end
Expand Down

0 comments on commit 077ebab

Please sign in to comment.