In Windows, Ruby + Watir is used to build an automated testing environment and read data. rubywatir

Source: Internet
Author: User
Tags ruby on rails

In Windows, Ruby + Watir is used to build an automated testing environment and read data. rubywatir

Build a Watir Environment

1. watir environment tool installation package:

Ruby186-26.exe: http://files.rubyforge.vm.bytemark.co.uk/rubyinstaller/
2) watir-1.5.2.gem: http://rubyforge.org/frs? Group_id = 104 & release_id = 28016
3) rubygems-update-1.3.7.gem: http://rubyforge.org/frs? Group_id = 126

2. firewatir environment tool installation package:

1) software package in step 1
2) Firefox2.0: http://www.hacker.cn/Get/gjrj/06102608545293311.shtml.
3) firefox Plugin: firbug1.05 and JSSh. Jssh: Address
4) Firewatir-1.1: http://rubyforge.org/frs? Group_id = 104 & release_id = 28017

3. watir Installation
1. Install ruby186-26.exe;
2) update the gem, copy the gem package to the ruby installation directory, and run the following command on the command line: gem install rubygems-update 1.3.7.gem (gem package name ). Reminder: After installation, enter the ruby Directory: lib \ ruby \ gems \ 1.8 \ gems \ rubygems-update-1.3.4, click the file in this folder: setup. rb, upgrade gem
3) install the watir package, copy the watir package to the ruby installation directory, use the command line to enter the ruby installation directory, enter the command: gem install watir-1.5.2.gem.
4) run the gem list command to check whether the installation is successful.

4. Install firewatir

1) install firefox, firebug, and Jssh plug-ins. Installation Method: Open the firefox browser, click "file"/"open", select the plug-in file name, and install the plug-in.

2) install firewatir, use the command line to enter the ruby installation directory, enter the command: gem install firewatir-1.1.gem

5. Test whether firewatir is successfully installed?

Use the command line to enter the firewatir installation path (\ ruby \ lib \ ruby \ gems \ 1.8 \ gems \ firewatir-1.1), enter the unittests folder, enter the command: ruby javasilla_all_tests.rb, if you can correctly execute the program, it indicates that the installation is correct and there is no problem. Now you can start the automated test and development journey.

6. During watir installation, 'nmake' is not an internal or external command, or a program or batch file that can be run.

Use commands

gem install --local watir-1.5.2.gem 

The installation is successful!


Reading Test parameterized data from various data sources
Parameterization is a commonly used technology in automated testing. It is painful to create a testing framework that does not support parameterization. QTP already exists, But ruby-like watir and selenium are not supported because these frameworks only provide the most basic Automatic Drive class libraries, execution Management and data management beyond driver are also a subject. Most of the execution frameworks used in selenium and watir similar projects are unit test frameworks, which naturally do not support parameterization. Currently, only junit4 supports parameterization, and testNG supports multiple parameterization by default. If you start a project, you can give priority to the languages compatible with these frameworks for automation.
Although watir itself does not support parameterization, ruby does not support unit tests, parameterization is also required. There is no way to think of a work und. Therefore, a parameterized class is written separately to supplement the shortcomings of the parameterized function. The method is to provide data reading for a unified test data source. This interface is called in unit test, but no specific parameter line is specified. This Parameter Line is configured in a separate configuration file. This allows you to manage the parameter lines used for each test execution in a unified manner. The following code uses test data from various data sources.

#encoding: utf-8 require 'DBI' require 'odbc_utf8'  def generate_sql(table, what=nil, where=nil)  what="*" unless what  where="1=1" unless where  "select %s from %s where %s" % [what, table, where] end  def generate_hash(header, all_data)  t_arr = []  all_data.each do | row |  t_hash = {}  for i in 0..header.size-1 do   t_hash[header[i]] = row[i]  end  t_arr << t_hash  end  t_arr end  def select_hash_db(dsn,user,password,db,sql)  begin  dbh = DBI.connect(dsn, user, password)  dbh.do("use #{db}")  dbh.do("SET NAMES UTF8") if dsn.split(':')[1] == "Mysql"  sth = dbh.execute(sql)  arr = Array.new  sth.fetch_hash do | row |   arr << row  end  sth.finish  arr  rescue DBI::DatabaseError => e  puts "An error occurred"  puts "Error code: #{e.err}"  puts "Error message: #{e.errstr}"  ensure  dbh.disconnect if dbh  end end  class Text_Adapter   def initialize(file_path, sep=" ", col_num=nil, row_num=nil)   end   def get_pars(row=nil)   end  end  class Mysql_Adapter  def initialize(ds_connector, table_name, what=nil, where=nil)  @sql_str = generate_sql(table_name, what, where)  @ds_connector = ds_connector  end   def get_pars(row=nil)  dsc_arr = @ds_connector.split("#")  all_data = select_hash_db(dsc_arr[0],dsc_arr[1],dsc_arr[2],dsc_arr[3],@sql_str)  if row.class==Fixnum   all_data[row]  else   all_data  end  end end  class Excel_Adapter   def initialize(ds_connector, table_name, what=nil, where=nil)  @connection = WIN32OLE.new('ADODB.Connection')  @record_set = WIN32OLE.new('ADODB.Recordset')  @ds_connector = ds_connector  @sql_str = generate_sql(table_name, what, where)  end   def get_pars(row=nil)  t_arr = []  @connection.Open(@ds_connector)  @record_set.Open(@sql_str, @connection)  @record_set.Fields.count.times do | i |   t_arr << @record_set.Fields.Item(i).name  end  all_data = @record_set.GetRows.transpose  all_data = generate_hash(t_arr, all_data)  if row.class==Fixnum   all_data[row]  else   all_data  end  end end   class Parameter  def initialize(ds_connector, table_name, what=nil, where=nil)  dsc_arr = ds_connector.split("#", 2)  eval("@adp = #{dsc_arr[0]}.new dsc_arr[1], table_name, what, where")  end   def get_pars(row=nil)  @adp.get_pars(row)  end end 

Call method:

par = Parameter.new(Ds_mysql_connector, 'demo') p par.get_pars(0) 

Configuration File Configuration:

# Link the excel database string EXCEL_DSN =%{ Excel_Adapter # Provider = MicroSoft. jet. OLEDB.4.0; Data Source = % s; Extended Properties = Excel 5.0;} EXCEL_FILE =%{ D :\\ 1.xlsx }## connection string Ds_mysql_connector =%{ Mysql_Adapter # DBI: mysql: shoppingcart: 127.0.0.1 # root # password # shoppingcart} # specify the line number of the parameter for the current running side accident, starting from 0 PAR_ROW = 1

You need to modify the path, IP address, database name, user name, and password of the corresponding file.

Articles you may be interested in:
  • How to automatically deploy Ruby on Rails in Docker

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.