Windows下Ruby+Watir自动化测试的环境搭建及数据读取(win10自动化部署系统)一看就会

随心笔谈12个月前发布 admin
75 0


#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

© 版权声明

相关文章