selenese.rb
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class SeleniumOnRails::Selenese
end
ActionView::Base.register_template_handler 'sel', SeleniumOnRails::Selenese
class SeleniumOnRails::Selenese
def initialize view
@view = view
end
def render template, local_assigns
name = (@view.assigns['page_title'] or local_assigns['page_title'])
lines = template.strip.split "\n"
html = ''
html << extract_comments(lines)
html << extract_commands(lines, name)
html << extract_comments(lines)
raise 'You cannot have comments in the middle of commands!' if next_line lines, :any
html
end
private
def next_line lines, expects
while lines.any?
l = lines.shift.strip
next if (l.empty? and expects != :comment)
comment = (l =~ /^\|.*\|$/).nil?
if (comment and expects == :command) or (!comment and expects == :comment)
lines.unshift l
return nil
end
return l
end
end
def extract_comments lines
comments = ''
while (line = next_line lines, :comment)
comments << line + "\n"
end
if defined? RedCloth
comments = RedCloth.new(comments).to_html
end
comments += "\n" unless comments.empty?
comments
end
def extract_commands lines, name
html = "<table>\n<tr><th colspan=\"3\">#{name}</th></tr>\n"
while (line = next_line lines, :command)
line = line[1..-2] #remove starting and ending |
cells = line.split '|'
if cells.first == 'includePartial'
html << include_partial(cells[1..-1])
next
end
raise 'There might only be a maximum of three cells!' if cells.length > 3
html << '<tr>'
(1..3).each do
cell = cells.shift
cell = (cell ? CGI.escapeHTML(cell.strip) : ' ')
html << "<td>#{cell}</td>"
end
html << "</tr>\n"
end
html << "</table>\n"
end
def include_partial params
partial = params.shift
locals = {}
params.each do |assignment|
next if assignment.empty?
_, var, value = assignment.split(/^([a-z_][a-zA-Z0-9_]*)\s*=\s*(.*)$/)
raise "Invalid format '#{assignment}'. Should be '|includePartial|partial|var1=value|var2=value|." unless var
locals[var.to_sym] = value or ''
end
@view.render :partial => partial, :locals => locals
end
end