Class: Selenium::Client::JavascriptExpressionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/client/javascript_expression_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(javascript_framework_name = nil) ⇒ JavascriptExpressionBuilder

Returns a new instance of JavascriptExpressionBuilder.



26
27
28
29
# File 'lib/selenium/client/javascript_expression_builder.rb', line 26

def initialize(javascript_framework_name=nil)
  @framework = javascript_framework_for(javascript_framework_name) if javascript_framework_name
  @script = ""
end

Instance Attribute Details

#scriptObject (readonly)

Returns the value of attribute script.



24
25
26
# File 'lib/selenium/client/javascript_expression_builder.rb', line 24

def script
  @script
end

Instance Method Details

#append(text) ⇒ Object



31
32
33
34
# File 'lib/selenium/client/javascript_expression_builder.rb', line 31

def append(text)
  @script << text
  self
end

#element_value_is(expected_value) ⇒ Object



64
65
66
# File 'lib/selenium/client/javascript_expression_builder.rb', line 64

def element_value_is(expected_value)
  append "(element != null && element.value == '#{quote_escaped(expected_value)}')"
end

#element_value_is_not(expected_value) ⇒ Object



68
69
70
# File 'lib/selenium/client/javascript_expression_builder.rb', line 68

def element_value_is_not(expected_value)
  append "(element == null || element.value != '#{quote_escaped(expected_value)}')"
end

#find_element(locator) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/selenium/client/javascript_expression_builder.rb', line 52

def find_element(locator)
  append  <<-EOS
    var element;

    try {
      element = selenium.browserbot.findElement('#{quote_escaped(locator)}');
    } catch(e) {
      element = null;
    }
  EOS
end

#find_text(pattern, options) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/selenium/client/javascript_expression_builder.rb', line 72

def find_text(pattern, options)
  if options[:element].nil?
    find_text_in_document pattern, options
  else
    find_text_in_element pattern, options
  end

  self
end

#find_text_in_document(pattern, options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/selenium/client/javascript_expression_builder.rb', line 82

def find_text_in_document(pattern, options)
  js_regexp = case pattern
  when Regexp
    pattern.inspect
  else
    /#{pattern}/.inspect
  end
  append <<-EOS
    var text_match;
    text_match = (null != selenium.browserbot.getCurrentWindow().document.body.innerHTML.match(#{js_regexp}));
  EOS

end

#find_text_in_element(pattern, options) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/selenium/client/javascript_expression_builder.rb', line 96

def find_text_in_element(pattern, options)
  find_element(options[:element])
  append <<-EOS
    var text_match;
    text_match = (element != null && #{text_match(pattern)});
  EOS

  self
end

#javascript_framework_for(framework_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/selenium/client/javascript_expression_builder.rb', line 115

def javascript_framework_for(framework_name)
  case framework_name.to_sym
  when :prototype
    JavascriptFrameworks::Prototype
  when :jquery
    JavascriptFrameworks::JQuery
  else
    raise "Unsupported Javascript Framework: #{framework_name}"
  end
end

#no_pending_ajax_requestsObject



36
37
38
# File 'lib/selenium/client/javascript_expression_builder.rb', line 36

def no_pending_ajax_requests
  append window_script("#{@framework.ajax_request_tracker} == 0")
end

#no_pending_effectsObject



40
41
42
# File 'lib/selenium/client/javascript_expression_builder.rb', line 40

def no_pending_effects
  append window_script("Effect.Queue.size() == 0")
end

#not_visible(locator) ⇒ Object



48
49
50
# File 'lib/selenium/client/javascript_expression_builder.rb', line 48

def not_visible(locator)
  append "!selenium.isVisible('#{quote_escaped(locator)}')"
end

#quote_escaped(a_string) ⇒ Object



130
131
132
# File 'lib/selenium/client/javascript_expression_builder.rb', line 130

def quote_escaped(a_string)
  a_string.gsub(/\\/, "\\\\\\").gsub(/'/, %q<\\\'>)
end

#text_match(pattern) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/selenium/client/javascript_expression_builder.rb', line 106

def text_match(pattern)
  case pattern
  when Regexp
    "null != element.innerHTML.match(#{pattern.inspect})"
  else
    "element.innerHTML == '#{quote_escaped(pattern)}'"
  end
end

#visible(locator) ⇒ Object



44
45
46
# File 'lib/selenium/client/javascript_expression_builder.rb', line 44

def visible(locator)
  append "selenium.isVisible('#{quote_escaped(locator)}')"
end

#window_script(expression) ⇒ Object



126
127
128
# File 'lib/selenium/client/javascript_expression_builder.rb', line 126

def window_script(expression)
  "selenium.browserbot.getCurrentWindow().#{expression};"
end