class RoboTest::CLI

Public Instance Methods

invoke(argv) click to toggle source
# File lib/robotest.rb, line 22
def invoke(argv)
  `gem cleanup`
  logo
  welcome_message
  if argv.empty?
    help_text
    puts "Add a path to an yaml file to start running your suite\n"
    puts "Enter a path to your yaml file:\n"
    argv = STDIN.gets
    if !argv.include? ".yml"
      puts "Need to enter the path for a configured yml file !!!"
      exit_message
      exit!
    end
  end
  at_exit {
    delete_temp_files
  }
  example if argv.include?('example')
  options, rspec_args = parse_options(argv)
  $conf = load_config_file(argv[0], options)
  modified_argv = setup_args(argv, $conf, rspec_args)
  yaml_dump('_robotest.yml', $conf)
  run(modified_argv)
end

Private Instance Methods

delete_temp_files() click to toggle source
# File lib/robotest.rb, line 153
def delete_temp_files
  File.delete('_robotest.yml') if File.exist?('_robotest.yml')
end
example() click to toggle source
# File lib/robotest.rb, line 96
def example
  begin
    source_directory      = File.dirname(__FILE__) + "/../Full_Suite/."
    destination_directory = Dir.pwd + "/Full_Suite/"
    `cp -a #{source_directory} #{destination_directory}`
    puts "\n\nInitialized sample robotest project in #{destination_directory}\n\n"
    puts "Follow the following steps to make sure everyting is setup in a right way:"
    puts "1. Now Make sure to rename the project name from `Full_Suite` to your `Custom Name`"
    puts "2. Rename the file `rspec_parallel` in Full_Suite/ folder to `.rspec_parallel`. Navigate to your project and Do `bundle install` before proceeding with the following step"
    puts "3. To test a sample run with single process try running `robotest yaml/test.yml`"
    puts "4. To test a sample run with multiple process try running `robotest yaml/test_parallel.yml`\n\n"
    exit_message
    exit!
  rescue Exception => e
    puts e.message
    puts e.backtrace
  ensure
    exit_message
    exit!
  end
end
exit_message() click to toggle source
# File lib/robotest.rb, line 67
def exit_message
  puts "\nExiting ...!!!"  
end
generate(argv) click to toggle source
# File lib/robotest.rb, line 118
def generate(argv)
  begin
    raise 'Pass module name as an argument, example: robotest generate login' unless argv.length >= 2
    argv.each do |file_name|
      next if file_name == 'generate'
      class_name = file_name.split('_').collect(&:capitalize).join
      locators_file_path = File.read(File.join(File.dirname(__FILE__), '..', 'templates', 'locators.erb'))
      pages_file_path = File.read(File.join(File.dirname(__FILE__), '..', 'templates', 'pages.erb'))
      File.write(File.join(Dir.pwd, 'locators', "#{file_name}.rb"), ERB.new(locators_file_path).result(binding))
      File.write(File.join(Dir.pwd, 'pages', "#{file_name}.rb"), ERB.new(pages_file_path).result(binding))
      puts "Template class: '#{class_name}' is created under pages and locators directory"
    end
  rescue Exception => e
    puts e.message
    puts e.backtrace
  ensure
    exit_message
    exit!
  end
end
help_text() click to toggle source
# File lib/robotest.rb, line 91
def help_text
  puts "New to RoboTest? try \n> robotest example \nto get a sample project get loaded in your directory \n \n"
  puts "Need help in running the suite? try \n> robotest -h\nfor help (or) \n \n"
end
load_config_file(file, options) click to toggle source
# File lib/robotest.rb, line 189
def load_config_file(file, options)
  raise "An yaml file needs to be" if !File.file?(file)
  configFile = YAML.ext_load_file(file)
  configFile["base_urls"] = options[:url].split(',') if options[:url]
  configFile["browser"] = options[:browser] if options[:browser]
  configFile["remote"] = options[:remote] if options[:remote]
  if configFile["parallel_process"] != nil
    configFile["count"] = "#{configFile["parallel_process"]}"
  else
    configFile["count"] = options[:count] ? options[:count] : configFile["base_urls"].length.to_s
  end
  configFile["mode"] = options[:mode] if options[:mode]
  configFile["only_failures"] = options[:only_failures] if options[:only_failures]
  configFile["tag"] = options[:tag] if options[:tag]
  configFile["dimensions"]["horizontal"] = options[:horizontal] if options[:horizontal]
  configFile["dimensions"]["vertical"] = options[:vertical] if options[:vertical]
  configFile["testrail"]["run_id"] = options[:runid].to_i if options[:runid]
  configFile["buildURL"] = options[:buildURL] if options[:buildURL]
  configFile
end
opt_parser_banner() click to toggle source
# File lib/robotest.rb, line 50
def opt_parser_banner
  puts "RoboTest is built over rspec, parallel_tests and allure.\n\n"
  help_text
  puts "1. Install Sublime, Atom, Code or Xcode based on your preference\n"
  puts "2. Install homebrew from http://brew.sh/\n"
  puts "3. Install ruby and rvm with `brew install ruby` and `curl -sSL https://get.rvm.io | bash -s stable --ruby`\n"
  puts "4. Install chrome driver using `brew cask install chromedriver`\n"
  puts "5. For sample project run `robotest example` and move to that directory in the command prompt\n"
  puts "6. Now Make sure to rename the project name from `Full_Suite` to your `Custom Name`\n"
  puts "7. Rename the file `rspec_parallel` in Full_Suite/ folder to `.rspec_parallel` to get the console logs while running the scripts !!!\n"
  puts "8. To test a sample run with single process try running `robotest yaml/test.yml`\n"
  puts "9. To test a sample run with multiple process try running `robotest yaml/test_parallel.yml`\n"
  puts "10. You can specify the additional parameters as follows,\n> robotest yaml/test.yml -t sanity||true\n"
  puts "Here test cases with sanity tag alone will run \n\n
    Other additional parameters are:"
end
parse_options(argv) click to toggle source
# File lib/robotest.rb, line 210
    def parse_options(argv)

      options = {}
      rspec_args = []

      OptionParser.new do |opts|
        opts.banner = <<-BANNER.gsub(/^          /, '')
          BANNER

        opts.on("-f", "--format FORMATTER", "Result format as specified in rspec") do |format|
          options[:format] = format
          rspec_args << '-f' << format
        end

        opts.on("-t", "--tag TAG[:VALUE]", "Run examples with the specified tag") do |tag|
          options[:tag] = tag
          rspec_args << '-t' << tag
        end

        opts.on("-e", "--example STRING", "Runs test cases with the specified string") do |example|
          options[:example] = example
          rspec_args << '-e' << example
        end

        opts.on("", "--only-failures", "Run only the failures from the last run") do |only_failures|
          options[:only_failures] = only_failures
          rspec_args << '--only-failures'
        end

        opts.on("-n", "--count COUNT", "Process count.") { |count| options[:count] = count }
        opts.on("-u", "--url URL", "Run scripts with this url.") { |url| options[:url] = url }
        opts.on("-x", "--horizontal HORIZONTAL", "Browser dimension in horizontal direction") { |horizontal| options[:horizontal] = horizontal.to_i }
        opts.on("-y", "--vertical VERTICAL", "Browser dimension in vertical direction") { |vertical| options[:vertical] = vertical.to_i }
        opts.on("-b", "--browser BROWSER", "chrome/firefox/edge/safari") { |browser| options[:browser] = browser }
        opts.on("-m", "--mode MODE", "single/parallel") { |mode| options[:mode] = mode }
        opts.on("-v", "--version", "Show version.") { puts "Version - #{RoboTest::VERSION}\n"; exit 0 }
        opts.on("-h", "--help", "Print help.") { 
          opt_parser_banner
          puts opts; 
          exit_message
          exit! }
      end.parse!

      [options, rspec_args]

    end
run(argv) click to toggle source
# File lib/robotest.rb, line 139
def run(argv)
  if $conf["mode"] == 'parallel'
    ParallelTests::CLI.new.run(["--type", "rspec"] + argv)
  else
    ParallelTests::CLI.new.run(["--type", "rspec", "--first-is-1", "-n", "1"] + argv)
  end
end
setup_args(argv, conf, rspec_args) click to toggle source
# File lib/robotest.rb, line 157
def setup_args(argv, conf, rspec_args)
  argv.shift
  conf["specs"].each { |spec| argv << spec } if argv.empty?
  argv.unshift('--')
  if conf["tag"]
    if conf["tag"].class == Hash
      conf["tag"].each do|key, value|
        argv.unshift("#{key}:#{value}")
        argv.unshift("-t")
      end
    else
      tags = conf["tag"].split("&&")
      tags.each do|tag|
        argv.unshift(tag)
        argv.unshift("-t")
      end
    end
  end
  argv.unshift(conf['exclude'].join(',')) unless conf['exclude'].nil?
  argv.unshift('--exclude-pattern') unless conf['exclude'].nil?
  argv.unshift('--only-failures') if conf['only_failures']
  argv.unshift('@sanity:true') if conf['sanity']
  argv.unshift('--tag') if conf['sanity']
  argv.unshift('--')
  argv.unshift('--first-is-1')
  if conf['mode'] == 'parallel' and conf['count']
    argv.unshift(conf['count'])
    argv.unshift('-n')
  end
  return argv
end
welcome_message() click to toggle source
# File lib/robotest.rb, line 87
def welcome_message
  puts "Welcome to RoboTest !!!\n\n"
end
yaml_dump(filename, data) click to toggle source
# File lib/robotest.rb, line 147
def yaml_dump(filename, data)
  File.open(filename, 'w') do |f|
    f.write(YAML.dump(data))
  end
end