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



7
8
9
10
# File 'lib/selenium/client/javascript_expression_builder.rb', line 7

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



5
6
7
# File 'lib/selenium/client/javascript_expression_builder.rb', line 5

def script
  @script
end

Instance Method Details

#append(text) ⇒ Object



12
13
14
15
# File 'lib/selenium/client/javascript_expression_builder.rb', line 12

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

#element_value_is(expected_value) ⇒ Object



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

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

#element_value_is_not(expected_value) ⇒ Object



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

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

#find_element(locator) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/selenium/client/javascript_expression_builder.rb', line 33

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



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/selenium/client/javascript_expression_builder.rb', line 63

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



77
78
79
80
81
82
83
84
85
# File 'lib/selenium/client/javascript_expression_builder.rb', line 77

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



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

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



17
18
19
# File 'lib/selenium/client/javascript_expression_builder.rb', line 17

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

#no_pending_effectsObject



21
22
23
# File 'lib/selenium/client/javascript_expression_builder.rb', line 21

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

#not_visible(locator) ⇒ Object



29
30
31
# File 'lib/selenium/client/javascript_expression_builder.rb', line 29

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

#quote_escaped(a_string) ⇒ Object



111
112
113
# File 'lib/selenium/client/javascript_expression_builder.rb', line 111

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

#text_match(pattern) ⇒ Object



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

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



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

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

#window_script(expression) ⇒ Object



107
108
109
# File 'lib/selenium/client/javascript_expression_builder.rb', line 107

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