class Bauxite::Parser

Parser class.

Parser represent different strategies for reading input files into lists of Bauxite actions.

Each custom parser is defined in a separate file in the 'parsers/' directory. These files should avoid adding public methods other than the parser method itself. Also, no attr_accessors should be added.

Parser methods can use the ctx attribute to refer to the current test Context. Parser methods receive a single action hash argument including :file and return an array of arrays or nil if the parser can't handle the file.

Each element in the output array must contain the following fields:

[
    action, # action name
    args,   # args array
    text,   # raw action text (before parsing), or nil
    line    # line in the file that defined the action
]

For example:

# === parsers/my_parser.rb ======= #
class Parser
    # :category: Parser Methods
    def my_parser(action)
        # open and read file
        [
            [ 'echo', [ 'hello world'                 ], 'echo "hello world"', 0 ],
            [ 'write',[ 'id=username', 'jdoe'         ], nil                 , 1 ],
            [ 'write',[ 'id=password', 'hello world!' ], nil                 , 2 ]
        ]
    end
end
# === end parsers/my_parser.rb === #

Parser Methods

↑ top

Public Instance Methods

csv(file) click to toggle source

Load CSV files.

# File lib/bauxite/parsers/csv.rb, line 27
def csv(file)
        return nil unless file.downcase.end_with? '.csv'
        
        File.open(file) do |f|
                f.read.each_line.each_with_index.map do |text,line|
                        text = text.sub(/\r?\n$/, '')
                        next nil if text =~ /^\s*(#|$)/
                        begin
                                data = CSV.parse_line(text)
                                [ data[0], data[1..-1].map { |a| a.strip }, nil, line ]
                        rescue StandardError => e
                                raise "#{file} (line #{line+1}): #{e.message}"
                        end
                end
        end.select { |item| item != nil }
end
default(file, io = nil) click to toggle source

Load default Bauxite files.

# File lib/bauxite/parsers/default.rb, line 27
def default(file, io = nil)
        return nil unless file == 'stdin' or file.match(/\.[tb]xt(\..*)?$/)
        
        return default(file, $stdin) if file == 'stdin' and not io
        
        return File.open(file) { |io| default(file, io) } unless io
        
        io.each_line.each_with_index.map do |text,line|
                text = text.sub(/\r?\n$/, '')
                next nil if text =~ /^\s*(#|$)/
                data = Bauxite::Context::parse_action_default(text, file, line)
                [ data[:action], data[:args], text, line ]
        end
        .select { |item| item != nil }
end
selenium_ide_html(file) click to toggle source

Load Selenium IDE HTML files.

# File lib/bauxite/parsers/html.rb, line 27
def selenium_ide_html(file)
        return nil unless file.downcase.end_with? '.html'
        
        File.open(file) do |f|
                content = f.read
                
                data = content.gsub(/[\n\r]/, '')
                selenium_base = data.sub(/.*rel="selenium.base" href="([^"]*)".*/, '\1')
                base_ends_in_slash = (selenium_base[-1] == '/')
                
                data
                .gsub('<tr>', "\naction=")
                .gsub('</tr>', "\n")
                .each_line.grep(/^action=/)
                .map { |line| line.match(/^action=\s*<td>([^<]*)<\/td>\s*<td>([^<]*)<\/td>\s*<td>([^<]*)<\/td>.*$/) }
                .select { |match| match }
                .map { |match| match.captures }
                .map do |action|
                        case action[0].downcase
                        when 'open'
                                url = action[1]
                                url = url[1..-1] if url[0] == '/' and base_ends_in_slash # remove leading '/'

                                action[1] = selenium_base + url
                        when 'type'
                                action[0] = 'write'
                                action[1] = _selenium_ide_html_parse_selector(action[1])
                        when 'verifytextpresent'
                                action[0] = 'source'
                        when 'clickandwait', 'click'
                                action[0] = 'click'
                                action[1] = _selenium_ide_html_parse_selector(action[1])
                        when 'waitforpagetoload'
                                action[0] = 'wait'
                                action[1] = (action[1].to_i / 1000).to_s
                        when 'assertvalue'
                                action[0] = 'assert'
                                action[1] = _selenium_ide_html_parse_selector(action[1])
                        when 'waitforpopup'
                                action = [] # remove

                        end
                        action = action.select { |a| a != '' }
                        [ action[0], action[1..-1], nil, 0 ]
                end
                .select { |a| a[0] }
        end
end